• Create Account

### #ActualBruceDawson

Posted 22 May 2012 - 11:12 AM

You're not calculating the sin of PI... you're calculating the sin of (float)pi, or (double)pi. PI, as a whole, can't be represented as a float or even a double.

That's correct, that he is not calculating cos(pi/2), he is calculating cos((float)(pi/2)). But what I think is really cool (and what my post on randomascii discusses) is exactly what *is* being calculated, because it turns out to be very meaningful.

When you calculate sin((float)pi)) or cos((float)(pi/2)) then a bit of calculus or geometric reasoning shows that what you are actually calculating is the error in the value that you are passing in! Thus:

sin((float)pi); equals pi-float(pi) -- the error in (float)pi
cos((float)(pi/2)); equals pi/2-(float)(pi/2) - the error in (float)(pi/2)

The Cos and sin functions are extremely accurate and the result is typically accurate to about seven significant digits, so the real meaning of the result is that (float)(pi/2) is about -4.37114e-008 away from pi/2. This happens to be close to FLT_EPSILON, but while that is probable it is not, in general, necessarily true.

Cool!

This claim is not generically true, It is true for cos/sin around these values because the derivative at those points is 1 and because the 'expected' value is zero.

### #1BruceDawson

Posted 22 May 2012 - 10:52 AM

You're not calculating the sin of PI... you're calculating the sin of (float)pi, or (double)pi. PI, as a whole, can't be represented as a float or even a double.

That's correct, that he is not calculating cos(pi/2), he is calculating cos((float)(pi/2)). But what I think is really cool (and what my post on randomascii discusses) is exactly what *is* being calculated, because it turns out to be very meaningful.

When you calculate sin((float)pi)) or cos((float)(pi/2)) then a bit of calculus or geometric reasoning shows that what you are actually calculating is the error in the value that you are passing in! Thus:

sin((float)pi); equals pi-float(pi) -- the error in (float)pi
cos((float)(pi/2)); equals pi/2-(float)(pi/2) - the error in (float)(pi/2)

Cos and sin are extremely accurate and the result is typically accurate to about seven significant digits, so the real meaning of the result is that (float)(pi/2) is about -4.37114e-008 away from pi/2.

Cool!

This claim is not generically true, It is true for cos/sin around these values because the derivative at those points is 1 and because the 'expected' value is zero.

PARTNERS