Re: problem with cos ( pi/2 )
Re: problem with cos ( pi/2 )
- Subject: Re: problem with cos ( pi/2 )
- From: Greg Guerin <email@hidden>
- Date: Fri, 16 Jun 2006 19:51:44 -0700
billmonk wrote:
>And the app even uses functions from <fp.h>. But interestingly, if I
>Cmd-double-click on cos(), <math.h> opens. Xcode doesn't pop up the little
>disambiguator list it normally shows when something is defined in multiple
>places. Yet both <fp.h> and <math.h> are included (via <Carbon/Carbon.h>).
>That's strange - I'd expect it to pop up cos() in both files and ask for a
>choice...
>
>Since the app uses some <fp.h>-only functions, there's no question that
>it's included...I wonder how to tell which cos() is actually getting used?
It may be they're the same, i.e. only a single function. The problem may
have nothing to do with cos(), but with the value you're using for "pi".
If <fp.h> is being included, I suggest a simple test:
if ( pi != M_PI )
printf( "Well, isn't that interesting." );
if ( pi/2.0 != M_PI_2 )
printf( "Approximately half as interesting." );
where 'pi' is the extern const declared in <fp.h>.
As strange as it sounds, there is no guarantee that the two values are
bit-for-bit identical. In particular:
** M_PI is #defined to be 3.14159265358979323846264338327950288. The
compiler will convert this decimal digit-string into a specific IEEE-754
bit-pattern, perhaps by rounding.
** 'pi' is declared as an extern const double, with an unspecified initial
value. We have no way of knowing what its value is except by using it.
Assuming the "PowerPC Numerics" doc is accurate, what we *DO* know about
'pi' is that it's the exact value the circular functions use to reduce
input values into the primary range. We also know that cos( pi/2) should
be -0.0, not 6.123e-17.
Howard Hinnant is correct, the value of M_PI (or the value stored in 'pi')
will only be an approximation of TheRealIrrationalValueOfPi. However, this
difference is irrelevant to the problem. If the docs are correct, the
value in 'pi' is EXACTLY the value that the circular functions use, so the
results should be exact. However, how 'pi' relates to M_PI is an open
question.
If the test code says nothing, then the fault is likely with cos(), and
it's worth trying to find out which library is really being used. (I
suggest 'otool -L' on the executable to see which libraries are actually
being referenced.)
However, if the test code says that the representations differ, then you
need to use the extern const 'pi' rather than the M_PI* series of #defined
constants. It might be interesting to display the value of 'pi', but I
would still use the declared const, not its reconverted literal value.
-- GG
_______________________________________________
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