Single precision function using C

mau at alice.CSELT.STET.IT mau at alice.CSELT.STET.IT
Sat Jan 12 03:26:34 AEST 1991


I am translating from FORTRAN to C a huge numerical program which runs on
a Sun 4 with SunOs 4.1, hoping both to reorder the code and to gain a
little speed. Unfortunately, the C version is much slower. Indeed, the
reason for the slowness lies in the heavy use of exponential and
logarithms, which are performed with single precision routines in Fortran
and double precision routines in C.  As I have no particular stability
problem, I would like to use single precision routines in the C program,
too. While RTFM, I found a series of such routines in the math package: I
tried to make a program, but it does not return the correct answers, as
you can see.

----- program pippo.c (compiled with cc pippo.c -lm) -----

#include <stdio.h>
#include <math.h>
FLOATFUNCTIONTYPE r_exp_();

main()
{
        float * y;
        float x;
        float z;
        FLOATFUNCTIONTYPE x1;

        for (z = 0.; z < 2.01; z += .2) {
        y = &z;
        x = exp(*y);
        x1 = r_exp_(y);
        printf("z = %f  ... ",z);
        printf("double: %f  ... ",x);
        printf("single: %f \n",x1);
        }
}

----- results -----

z = 0.000000  ... double: 1.000000  ... single: 0.007812
z = 0.200000  ... double: 1.221403  ... single: 0.027675
z = 0.400000  ... double: 1.491825  ... single: 0.120912
z = 0.600000  ... double: 1.822119  ... single: 0.788476
z = 0.800000  ... double: 2.225541  ... single: 3.804328
z = 1.000000  ... double: 2.718282  ... single: 14.985019
z = 1.200000  ... double: 3.320117  ... single: 81.949978
z = 1.400000  ... double: 4.055201  ... single: 568.525625
z = 1.600000  ... double: 4.953033  ... single: 1951.812144
z = 1.800000  ... double: 6.049649  ... single: 9005.450408
z = 2.000000  ... double: 7.389058  ... single: 58265.295935

I guess that there is some problem in the stack return, but I do not
absolutely know which error I make ...

Regards,    .mau.



More information about the Comp.sys.sun mailing list