•      Sign In
• Create Account

# Cos is not returning 0 for 90 degrees or PI/2

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

33 replies to this topic

### #21Álvaro  Crossbones+   -  Reputation: 18614

Like
1Likes
Like

Posted 21 May 2012 - 05:43 PM

The irrationality of pi is not exactly what the problem is. There are many rational numbers that cannot be expressed exactly in floating-point formats either, like 1/3 or 0.1.

### #22BruceDawson  Members   -  Reputation: 109

Like
3Likes
Like

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)

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.

Edited by BruceDawson, 22 May 2012 - 11:12 AM.

### #23WiredCat  Crossbones+   -  Reputation: 1182

Like
-2Likes
Like

Posted 24 May 2012 - 04:27 AM

mayby you are using degrees instead of radians if you use degrees you must multiply angle by pi/180

const float imopi = 0.017453292519943295769236907684886;

so then you should write this Cos = cos( angle* imopi);

### #24TheUnbeliever  Members   -  Reputation: 962

Like
0Likes
Like

Posted 24 May 2012 - 06:39 AM

const float imopi = 0.017453292519943295769236907684886;

Not only has the problem been well-diagnosed to be not this, but (appropriately enough) that constant has far more digits of precision than are typically representable in a float.
[TheUnbeliever]

### #25sjhalayka  Members   -  Reputation: 762

Like
0Likes
Like

Posted 24 May 2012 - 09:21 AM

The irrationality of pi is not exactly what the problem is. There are many rational numbers that cannot be expressed exactly in floating-point formats either, like 1/3 or 0.1.

Edit: sicrane brought me up to speed with your logic regarding 0.1. It's all obvious now that what does not possess infinite non-zero placeholders right of the decimal point in base 10 may have infinite non-zero placeholders right of the binary point in base 2. Thanks!

I vote for the next version of IEEE fp to be in base pi. Problem solved! I'm kidding.

### #26SiCrane  Moderators   -  Reputation: 11271

Like
1Likes
Like

Posted 24 May 2012 - 09:30 AM

.1 in binary is 0.0001100110011001100110011001100110011001100110011... A repeating fraction. Stop after any finite number of bits and you don't get an exact representation.

### #27sjhalayka  Members   -  Reputation: 762

Like
0Likes
Like

Posted 24 May 2012 - 09:32 AM

.1 in binary is 0.0001100110011001100110011001100110011001100110011... A repeating fraction. Stop after any finite number of bits and you don't get an exact representation.

Thank you for filling me in. Good to know!

### #28JohnnyCode  Members   -  Reputation: 981

Like
0Likes
Like

Posted 24 May 2012 - 04:34 PM

remember that float can provide certian exactness to computations against some extend. so if you need to get to very low extends, you should linear reduce those extends..... if you want to cumpute angle of earth against its eclipse (the plane that earths orbits the sun, thus 0 degrees exactly), you would have to provide sin (0)... you will never continue with this result unless you apply special data type or special routine of fractionizing the linear space to linear space power.

if you used the minimal epsilon you would end up with earth quite missing its real place.

### #29Álvaro  Crossbones+   -  Reputation: 18614

Like
0Likes
Like

Posted 24 May 2012 - 04:40 PM

remember that float can provide certian exactness to computations against some extend. so if you need to get to very low extends, you should linear reduce those extends..... if you want to cumpute angle of earth against its eclipse (the plane that earths orbits the sun, thus 0 degrees exactly), you would have to provide sin (0)... you will never continue with this result unless you apply special data type or special routine of fractionizing the linear space to linear space power.

if you used the minimal epsilon you would end up with earth quite missing its real place.

I didn't get any of that. What's an "extend"? And what's wrong with computing sin(0)? What on Earth is "fractionizing the linear space to linear space power"?

### #30JohnnyCode  Members   -  Reputation: 981

Like
-2Likes
Like

Posted 24 May 2012 - 06:44 PM

What on Earth is "fractionizing the linear space to linear space power"?

Good question. Think about amount of angles of earths sun orbit to its eclipse(plane between earth and sun).
Perfect 0.0.
But you do not never not know exact number, whatever it seems exact...... So "if eclipse of earth is identical to eclipse of earth", it may be volating some amount, hard to measure exact. Numbers can contain many information, the most exponsive information is information about the angle (never ending)

### #31ApochPiQ  Moderators   -  Reputation: 19913

Like
0Likes
Like

Posted 24 May 2012 - 06:52 PM

I'm not sure what exactly you're on about, but I'm pretty sure it's not on topic.

Let's keep this thread to the point, shall we?
Wielder of the Sacred Wands

### #32Cornstalks  Crossbones+   -  Reputation: 7020

Like
0Likes
Like

Posted 24 May 2012 - 07:00 PM

What on Earth is "fractionizing the linear space to linear space power"?

Good question. Think about amount of angles of earths sun orbit to its eclipse(plane between earth and sun).
Perfect 0.0.
But you do not never not know exact number, whatever it seems exact...... So "if eclipse of earth is identical to eclipse of earth", it may be volating some amount, hard to measure exact. Numbers can contain many information, the most exponsive information is information about the angle (never ending)

No offense, but... YOU'RE NOT MAKING ANY SENSE.

I wrote this while ApochPiQ was posting... I'm done here.

Edited by Cornstalks, 24 May 2012 - 07:01 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #33JohnnyCode  Members   -  Reputation: 981

Like
0Likes
Like

Posted 24 May 2012 - 07:18 PM

off topic? OP wants to know and you are off topic? maybe I was not exact, ok, but I do not have right to post my 2 cents then? What restrictive policy is that for OPs to find out their solutions?

### #34ApochPiQ  Moderators   -  Reputation: 19913

Like
0Likes
Like

Posted 24 May 2012 - 07:26 PM

Dude, the question was solved - including with input from a notable expert - and there's nothing left to figure out. And now you're talking about eclipses (or, presumably, ecliptics which are hardly the same thing).

I have no idea how you consider that on topic.
Wielder of the Sacred Wands

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS