21 January 2006
long double datatype on MacOS
I recently found out from a scitech@listsapple.com posting that the way long double is defined on MacOS PPC hardware does not increase the range of available numbers; it only addings to the precision of the calculation.
“Long double on Tiger is formatted as a 128 bit head to tail pair of doubles. The first double holds the exponent and the first 53 bits of the mantissa. Then there is a stretch of 0 or more bits all set to zero, then the tail double holds the rest of the number. The full number is essentially calculated as the infinite precision result of head + tail. The range for long double on PowerPC is *almost* exactly the same as for a regular double. If you need more range, you should probably look at one of the open source arbitrary precision floating point libraries. The Apple PowerPC 128-bit long double format pretty much provides enhanced precision only. Please note that the current libm implementation of long double on PowerPC is not IEEE-754 or C99 compliant. Various edge cases are incorrect. The intent is to deliver more precision with good performance.”
If you need to use long double math.h functions then you will have to install gmp and mpfr (or alternatively use and Intel mac) I used darwinports to do this. Once installed you will need to change a few properties of the target.
(i) highlight the exec under the target menu,
(ii) add the paths to the header files and libraries just installed,
(iii) add the compiler linker instructions for gmp and mpfr,
(iv) un-check zero link.
Now compile and run this example code (note if you run this without un-checking zerolink you will have to clear your project which removes all precompiled headers),
#import stdio.h
#import gmp.h
#import mpfr.h
int main(int argc, char *argv[])
{
mpfr_t x;
mpfr_init(x);
mpfr_set_d(x, 1000000, GMP_RNDD);
mpfr_sinh(x, x, GMP_RNDD);
mpfr_out_str(stdout, 10, 0, x, GMP_RNDD);
mpfr_clear(x);
return 0;
//Result should be 1.5166076984010437e434294
}

