# sin, cos...

I just play with sin() and cos() function and I got result that is not very
clear to me.
→ sample code:
---------------------------------------------- code begin -----
include <math.h>
#define PI 3.1415927

void main()
{
int a;
float b;

a = 100 * sin( PI / 2 );
b = 100 * sin( PI / 2 );

printf( “a = %d, b = %f\n”, a, b );
}
---------------------------------------------- code end -----
I’ve got result:
a=99, b=100.000000

I examined every conversion that I know. Unsuccessfully… What did I do
wrong?
Any help will be appreciated.

“Martin Michalek” <michalek@procaut.sk> wrote in message
news:9i49tb\$7j9\$1@inn.qnx.com

I just play with sin() and cos() function and I got result that is not
very
clear to me.
→ sample code:
---------------------------------------------- code begin -----
include <math.h
#define PI 3.1415927

void main()
{
int a;
float b;

a = 100 * sin( PI / 2 );
b = 100 * sin( PI / 2 );

printf( “a = %d, b = %f\n”, a, b );
}
---------------------------------------------- code end -----
I’ve got result:
a=99, b=100.000000

I examined every conversion that I know. Unsuccessfully… What did I do
wrong?

PI is not precise enough, sin ( PI/2) is equal to .9999999999999997.
This value is to requires to much precision to be represent by a float
you need double, hence it gets rounded to a float as 1.0. If you turn
b into a float b will show 99.999…

As for the integer the reason you see 99 is because of automatic promotion
rules.
sin() is prototypes as being a double hence the multiplication operation is
done as a double and the internale is result is 99.99999… But when the
result is moved into an integer the result is truncated.

One solution to get 100 for the interger is to increase the value of PI.
I tried it with 3.14159265358979323846 (as found under QNX6.0 match.h).
a and b where then both equal to 100.

Any help will be appreciated.

“Mario Charest” <mcharest@zinformatic.com> wrote in message
news:9i4bpg\$8lo\$1@inn.qnx.com

“Martin Michalek” <> michalek@procaut.sk> > wrote in message
news:9i49tb\$7j9\$> 1@inn.qnx.com> …
I just play with sin() and cos() function and I got result that is not
very
clear to me.
→ sample code:
---------------------------------------------- code begin -----
include <math.h
#define PI 3.1415927

void main()
{
int a;
float b;

a = 100 * sin( PI / 2 );
b = 100 * sin( PI / 2 );

printf( “a = %d, b = %f\n”, a, b );
}
---------------------------------------------- code end -----
I’ve got result:
a=99, b=100.000000

I examined every conversion that I know. Unsuccessfully… What did I do
wrong?

PI is not precise enough, sin ( PI/2) is equal to .9999999999999997.
This value is to requires to much precision to be represent by a float
you need double, hence it gets rounded to a float as 1.0. If you turn
b into a float b will show 99.999…

As for the integer the reason you see 99 is because of automatic promotion
rules.
sin() is prototypes as being a double hence the multiplication operation
is
done as a double and the internale is result is 99.99999… But when the
result is moved into an integer the result is truncated.

One solution to get 100 for the interger is to increase the value of PI.
I tried it with 3.14159265358979323846 (as found under QNX6.0 match.h).
a and b where then both equal to 100.

Isn’t that a bit hit-and-miss? After all, sin() is never going to return
anything greater than 1.0, so the exact value of PI/2 that returns 1.000…
in the avaialable resolution will be implementation dependent.

If you want to round a float to an integer, why not simply add 0.5 before
the truncation? That is:

printf( “a = %d, b = %f\n”, (int)( a + 0.5 ), b );

Bert Menkveld
Engineer
Corman Technologies Inc.
bertATcormantechDOTcom

Isn’t that a bit hit-and-miss?

Yes indeed that could be. The behavior is probably predicatable
once would just have to get to know the floating point standard.

After all, sin() is never going to return
anything greater than 1.0, so the exact value of PI/2 that returns
1.000…
in the avaialable resolution will be implementation dependent.

If you want to round a float to an integer, why not simply add 0.5 before
the truncation? That is:

printf( “a = %d, b = %f\n”, (int)( a + 0.5 ), b );

I guess one would have to check what the real value of .05 is internaly. It

may be .4444444444449

Bert Menkveld
Engineer
Corman Technologies Inc.
bertATcormantechDOTcom
\

Mario Charest <mcharest@zinformatic.com> wrote:

If you want to round a float to an integer, why not simply add 0.5 before
the truncation? That is:

printf( “a = %d, b = %f\n”, (int)( a + 0.5 ), b );

I guess one would have to check what the real value of .5 is internaly. It
may be .4444444444449 >

Actually, I’d expect that .5 would be represented exactly – it is
1x2^-1, right?

-David