Long Double Calculation on Intel

  • Below is a small program which calculates the cosine of 45 degrees in
    double and long double precision. The result should be 0.707...

    It works fine on PowerPC but the Intel long double produces garbage.
    Does anyone know what is happening here?

    I know the PowerPC long double precision is 128 bits and the Intel
    long double precision is 80 bits. But it still should work.

    #include <stdio.h>
    #include <math.h>

    int main ()
    {
        double result1 = cos(45.0 * M_PI / 180.0);
        printf("cos 45 degrees %16.14e\n", result1);

        long double result2 = cosl(45.0 * M_PI / 180.0);
        printf("cosl 45 degrees %16.14e\n", result2);

        return 0;
    }

    PowerPC Output
    cos 45 degrees 7.07106781186547e-01
    cosl 45 degrees 7.07106781186548e-01

    Intel Output
    cos 45 degrees 7.07106781186548e-01
    cosl 45 degrees -2.73410411748968e-53

    Regards, Richard
  • On Oct 9, 2007, at 9:00 AM, Richard Somers wrote:

    > Below is a small program which calculates the cosine of 45 degrees
    > in double and long double precision. The result should be 0.707...
    >
    > It works fine on PowerPC but the Intel long double produces
    > garbage. Does anyone know what is happening here?

    The values of result1 and result2 are correct; the output you're
    seeing isn't, because you're not using the correct printf specifier
    for long double. Change the second printf to:

    printf("cosl 45 degrees %16.14Le\n", result2);

    (note the 'Le' instead of just 'e').

    pg
  • Thank you so much Paul! That fixed the problem.

    Second related question. Does anyone know how to get 128 bit floating
    point precision on Intel like what is available on PowerPC?

    Richard

    On Oct 9, 2007, at 10:52 AM, Paul Goracke wrote:

    > The values of result1 and result2 are correct; the output you're
    > seeing isn't, because you're not using the correct printf specifier
    > for long double. Change the second printf to:
    >
    > printf("cosl 45 degrees %16.14Le\n", result2);
    >
    > (note the 'Le' instead of just 'e').
    >
  • I found the answer to my own question. Apple's "Advanced Computation
    Group" recently issued a technical paper on the subject titled
    "Multiprecision Floating-point Arithmetic on Apple Systems" dated
    February 2007.

    Richard

    On Oct 9, 2007, at 1:43 PM, Richard Somers wrote:

    > Second related question. Does anyone know how to get 128 bit
    > floating point precision on Intel like what is available on PowerPC?
    >
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today