Re: problem with cos ( pi/2 )
Re: problem with cos ( pi/2 )
- Subject: Re: problem with cos ( pi/2 )
- From: Howard Hinnant <email@hidden>
- Date: Fri, 16 Jun 2006 14:41:29 -0400
On Jun 16, 2006, at 12:55 PM, billmonk wrote:
Just to clarify two points...
1. cos( M_PI_2 ) == 6.123233995736766e-17, not
2.6484540326036093e-14 as I posted earlier. That value is the
cosine an app-calculated pi/2 having less precision than M_PI_2.
2. It's true, these numbers -are- very close to zero. My questions
are:
A) Is the '-llibm' compiler flag sufficient to use the IEEE math lib?
Yes.
B) If it is, how come the results of cos( pi/2 ) don't agree with
the behavior documented in PowerPC Numerics, page 148? Am I doing
something else wrong? Googling, I see that some other math libs
(Java, for instance) document that one should -not- expect cos( pi/
2 ) to equal exactly zero. This is at variance with what PowerPC
Numerics says OS X does, however.
C) If the behavior documented in PowerPC Numerics doesn't actually
occur, what's the proper thing to do? The app has some fairly heavy
arithmetic going on, with the results displayed to the user both
graphically in text form. Should it round before drawing? Round
before displaying text? Both? What have others done in this situation?
M_PI_2 (converted to double) is only an approximation to pi/2. It is
actually (and approximately) 6.1e-17 smaller than pi/2. The next
higher value which a 64 bit IEEE number can represent is
approximately 1.6e-16 bigger than pi/2.
It turns out that in the neighborhood of pi/2, cos(x) can be very
closely approximated by this simple linear function:
cos(x) =~ pi/2 - x
(derivation available upon request)
Plugging M_PI_2 into this approximation to cos(x) yields the result
you report: 6.1e-17. Using nextafter() to find the next
representable number above M_PI_2 and calling cos() on that number
yields approximately -1.6e-16 (again predicted by the linear
approximation to cos(x)).
The answer you're getting is a more accurate answer than 0 would be.
If you require more precision than that (in Tiger on PPC), you can
use 1.57079632679489661923132169163975144L (a long double) instead.
This will get you to within 1.e-32 (of pi/2 and of cos(pi/2)).
-Howard
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden