Am Donnerstag, 15. Februar 2001 13:40 schrieb Jean Claude Berges:
> Hello all ,
>
> I am currently doing some work on mathematical functions (sin,cos,...)
>
> with floating types REAL*8(i.e.double precision) and REAL*16 (i.e.,
> quadruple precision) on a SUN SPARC station with the FORTRAN WorkShop
> Compilers 5.0.
> I will like to make the same thing with the C language (floating types :
>
> double and long double), but with the C WorkShop Compilers 5.0 there are
> not
> specific mathematical functions for long double type (same results for
> sin(x) with double x and long double x).
The following program works on my system, a SuSE Linux on i386 with GNU cc
and GNU libc / GNU libm:
#include <math.h>
#include <stdio.h>
int main (void)
{
printf ("float: %.50g\n", sinf(1));
printf ("double: %.50g\n", sin (1));
printf ("long double: %.50Lg\n", sinl(1));
return 0;
}
Note that in C you have to explicely use a function name indicating the
precision; there are no generic intrinsics as there are in Fortran. I don't
know in how far the "sinf" and "sinl" are standardised, but I have seen them
on several systems. (Your compiler might need special options to recognise
sinl.) Different from Fortran, C does convert the argument of the sine
functions to the required precision, which is why writing the integer 1 in
all cases works fine.
The result of the above program is
float: 0.84147098480789650487565722869476303458213806152344
double: 0.84147098480789650487565722869476303458213806152344
long double: 0.84147098480789650666459081329584535069443518295884
showing that there is a difference between double and long double, but no
difference between float and double. I don't know in how far these
differences come from different precisions in the sine routine (as I hope),
or in the different argument passing to the printf routine.
The GNU system (cc, libc, and libm) is available for all major platforms.
-erik
|