Re: using long double math functions like sinl() with Cocoa
Re: using long double math functions like sinl() with Cocoa
- Subject: Re: using long double math functions like sinl() with Cocoa
- From: "E. Wing" <email@hidden>
- Date: Wed, 3 Jan 2007 18:59:06 -0800
Wow, I'm curious about this one too. I wouldn't expect this behavior.
So playing with your example, if I change your Cocoa program to a pure
Objective-C program (just remove the #import <Cocoa/Cocoa.h> and
NSApplicationMain, but still compile as Objective-C), the program
produces the correct results.
If I add a #import <Foundation/Foundation.h> (but don't link to
Foundation or Cocoa since I don't use anything in it), then the
program produces the wrong results.
So I'm speculating that there is something in the Foundation headers
that causes sinl to be mapped to sin (perhaps some kind of #define).
Anyway, I tried wrapping the sinl calls into a separate pure-C dynamic
library and then had an Objective-C program call my custom wrapper
function from my dynamic library. This seemed to produce the correct
results. So this might be a potential workaround. I would be curious
to know the cause of this and if there is a better workaround.
-Eric
<quote>
PowerBook G4, Mac OS X 10.4.8, Xcode 2.4.1
I tried to use long double math functions in my cocoa application and
got the program compiling and running, but the function results were
double and not long double.
Here is a test case built directly from the Cocoa Application
template. sin(pi) should give zero, of course within the limits of the
expected math precision.
// main.m
// CocoaSinTest
#import <Cocoa/Cocoa.h>
#include <math.h>
int main(int argc, char *argv[])
{
printf("%-.16f\n%-.33Lf", sin(3.1415926535897932),
sinl(3.1415926535897932384626433832795L));
return NSApplicationMain(argc, (const char **) argv);
}
[Session started at 2007-01-03 21:38:48 -0200.]
0.0000000000000001
0.000000000000000122464679914735321
Both results are 1e-16, wich is the precision of 64bit double math.
Digging into the assembly code, I found out, that in both cases the
sin function is called and not the sinl function.
A pure C program builded from the Standard Tool template works as expected:
// main.c
// SinTest
#include <stdio.h>
#include <math.h>
int main (int argc, const char * argv[])
{
printf("%-.16f\n%-.33Lf", sin(3.1415926535897932),
sinl(3.1415926535897932384626433832795L));
return 0;
}
[Session started at 2007-01-03 21:43:39 -0200.]
0.0000000000000001
0.000000000000000000000000000000022
The second result seems to be indeed zero within the limits of 128bit
long double precision.
QUESTIONS:
- are long double functions supposed to give in Cocoa applications
long double results as they do in pure C?
- is there a compiler switch, which activates the
expected behaviour?
- am I missing something obvious?
Best regards
Rolf Jansen
</quote>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden