For Programmers: Free Programming Magazines  


Home > Archive > C > March 2004 > Trouble with types!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Trouble with types!
fdunne2

2004-03-26, 11:09 pm

Observe the following function definition:

void function(int fsamp, struct1 *s1, struct1 *s2)
{
// snippet from actual function
s1->length[0] = fsamp * (s2->delay[0]);

printf("\nlength = %d", s1->length[0]);
}

where 'length' is an integer array and 'delay' is a float array both
defined in two seperate structures, 'struct1' and 'struct2'. The first
element of 'delay' has the value 0.0110. It was assigned this value in the
main function.

When I call this function from main(), with 'fsamp' equal to 8000 the
value 87 is printed on screen. Should this value not be 88 as:

0.0110 * 8000 = 88;

How can I alter my function to get the correct result?





Martin Dickopp

2004-03-26, 11:09 pm

"fdunne2" <fdunne2@nospam.yahoo.ie> writes:

> Observe the following function definition:
>
> void function(int fsamp, struct1 *s1, struct1 *s2)
> {
> // snippet from actual function
> s1->length[0] = fsamp * (s2->delay[0]);
>
> printf("\nlength = %d", s1->length[0]);
> }
>
> where 'length' is an integer array and 'delay' is a float array both
> defined in two seperate structures, 'struct1' and 'struct2'. The first
> element of 'delay' has the value 0.0110. It was assigned this value in the
> main function.
>
> When I call this function from main(), with 'fsamp' equal to 8000 the
> value 87 is printed on screen. Should this value not be 88 as:
>
> 0.0110 * 8000 = 88;
>
> How can I alter my function to get the correct result?


Please read the FAQ <http://www.eskimo.com/~scs/C-faq/faq.html>, in
particular the floating point chapter.

Your computer cannot represent 0.011 exactly, just like one-third cannot
be represented exactly with a finite number of decimal digits.

The best you can do in this case is probably to round the expression to
the nearest integer instead of rounding it down:

s1->length [0] = fsamp * (s2->delay [0]) + 0.5;

Martin


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Barry Schwarz

2004-03-27, 11:50 pm

On Fri, 26 Mar 2004 18:49:06 -0600, "fdunne2"
<fdunne2@nospam.yahoo.ie> wrote:

>Observe the following function definition:
>
>void function(int fsamp, struct1 *s1, struct1 *s2)
>{
> // snippet from actual function
> s1->length[0] = fsamp * (s2->delay[0]);
>
> printf("\nlength = %d", s1->length[0]);
>}
>
>where 'length' is an integer array and 'delay' is a float array both
>defined in two seperate structures, 'struct1' and 'struct2'. The first
>element of 'delay' has the value 0.0110. It was assigned this value in the
>main function.
>
>When I call this function from main(), with 'fsamp' equal to 8000 the
>value 87 is printed on screen. Should this value not be 88 as:
>
> 0.0110 * 8000 = 88;
>
>How can I alter my function to get the correct result?
>

..0110 cannot be represented exactly in binary. Depending on the
system, the actual value will be slightly more or less (obviously less
on your system). Multiplying by 8000 on your system will produce a
value slightly less than 88. Converting that value to int will result
in 87.


<<Remove the del for email>>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com