MOD intrinsic with the sun f77 compiler 
Author Message
 MOD intrinsic with the sun f77 compiler

Hello,

Anybody can give a explanation how the MOD intrinsic function is
computed with the Sun f77 compiler 4.0 ?

I believed that :
        MOD(R1,R2)=R1-INT(R1/R2)*R2

but this assumption is obviously false on the sun compiler.

Perhaps there is a workaround to link MOD with the IEEE Std 754-1985
compliant remainder function.

Any help is welcome...

Arnaud Desitter

Description of the problem
==========================

with the code :

      PROGRAM RNDOFF
      double precision
     *          R1, R2,
     *          RESULT
      R1 = 1.0d0
      R2 = 0.2d0
      RESULT = MOD(R1,R2)
      WRITE(unit=*,fmt=*) 'R1 : ',R1
      WRITE(unit=*,fmt=*) 'R2 : ',R2
      WRITE(unit=*,fmt=*) 'mod(r1,r2) : ',RESULT
      WRITE(unit=*,fmt=*) 'mod(r1,r2)/r2 : ',RESULT/R2
      WRITE(unit=*,fmt=*) 'R1-INT(R1/R2)*R2 : ',(R1-INT(R1/R2)*R2)
      WRITE(unit=*,fmt=*) 'R1-AINT(R1/R2)*R2 : ',(R1-AINT(R1/R2)*R2)
      write(unit=*,fmt=*) 'r2-mod',(r2-result)
      END
the results are :

solaris> f77 RNDOFF.f ; ./a.out
RNDOFF.f:
 MAIN rndoff:
 R1 :     1.0000000000000
 R2 :    0.20000000000000
 mod(r1,r2) :    0.20000000000000
 mod(r1,r2)/r2 :    1.00000000000000
 R1-INT(R1/R2)*R2 :   0.
 R1-AINT(R1/R2)*R2 :   0.
 r2-mod    5.5511151231258D-17



Sat, 09 Oct 1999 03:00:00 GMT  
 MOD intrinsic with the sun f77 compiler



Quote:
> Anybody can give a explanation how the MOD intrinsic function is
> computed with the Sun f77 compiler 4.0 ?
> I believed that :  MOD(R1,R2)=R1-INT(R1/R2)*R2
> but this assumption is obviously false on the sun compiler.
> Perhaps there is a workaround to link MOD with the IEEE Std 754-1985
> compliant remainder function.
> Description of the problem with the code :

We have discovered a very well known problem in the
computation with floating numbers (indepandtely
of the language : it is not a fortran problem
but with the floating representation of numbers in a
omputer) :

Compare MOD(1.D0,0.2D0) that gives 0.2D0
and     0.1D0*MOD(1.D1,0.2D1) that gives 0.D0

In fact whereas 1.D1 and 0.2D1 are exactely representable
in a computer (with radix 2, don't forget this), it is not
the case of 0.2D0 .....

For the same reasons,  we have same problems with Do-loops
  with real variables (never use such things.....)
                                                      BP



Sat, 09 Oct 1999 03:00:00 GMT  
 MOD intrinsic with the sun f77 compiler

Quote:

> Hello,

> Anybody can give a explanation how the MOD intrinsic function is
> computed with the Sun f77 compiler 4.0 ?

> I believed that :
>         MOD(R1,R2)=R1-INT(R1/R2)*R2

> but this assumption is obviously false on the sun compiler.

>       R1 = 1.0d0
>       R2 = 0.2d0
>       WRITE(unit=*,fmt=*) 'mod(r1,r2) : ',MOD(R1,R2)
>       WRITE(unit=*,fmt=*) 'R1-INT(R1/R2)*R2 : ',(R1-INT(R1/R2)*R2)

1 The sun compiler works correctly.
A short answer is that internal operation are computed whith more
precision that the operation performed directly by the user.

2 The other problem is that the IEEE 754 define a remainder function.
But unfortunatly, the current fortran standard gives explicitely a
different definition for the MOD function (for floating point).

There is no portable way to access the IEEE 754 remainder in Fortran.

Note that, with g77, it is possible to recompile the F77 library to link
MOD with the IEEE "drem" (or "remainder") if available.

3 Whatever the floating point definition is (with finite format length),
the remainder can _not_ give "good" results with the real numbers which
are not able to be exactly represented (like 0.2 in the IEEE 754
format).

So it is better to avoid remaindering on floating point.

        MOD is safe with INTEGER not REAL.

- Show quoted text -

Quote:
> Perhaps there is a workaround to link MOD with the IEEE Std 754-1985
> compliant remainder function.

> Any help is welcome...

> Arnaud Desitter

> Description of the problem
> ==========================

> with the code :

>       PROGRAM RNDOFF
>       double precision
>      *          R1, R2,
>      *          RESULT
>       R1 = 1.0d0
>       R2 = 0.2d0
>       RESULT = MOD(R1,R2)
>       WRITE(unit=*,fmt=*) 'R1 : ',R1
>       WRITE(unit=*,fmt=*) 'R2 : ',R2
>       WRITE(unit=*,fmt=*) 'mod(r1,r2) : ',RESULT
>       WRITE(unit=*,fmt=*) 'mod(r1,r2)/r2 : ',RESULT/R2
>       WRITE(unit=*,fmt=*) 'R1-INT(R1/R2)*R2 : ',(R1-INT(R1/R2)*R2)
>       WRITE(unit=*,fmt=*) 'R1-AINT(R1/R2)*R2 : ',(R1-AINT(R1/R2)*R2)
>       write(unit=*,fmt=*) 'r2-mod',(r2-result)
>       END
> the results are :

> solaris> f77 RNDOFF.f ; ./a.out
> RNDOFF.f:
>  MAIN rndoff:
>  R1 :     1.0000000000000
>  R2 :    0.20000000000000
>  mod(r1,r2) :    0.20000000000000
>  mod(r1,r2)/r2 :    1.00000000000000
>  R1-INT(R1/R2)*R2 :   0.
>  R1-AINT(R1/R2)*R2 :   0.
>  r2-mod    5.5511151231258D-17



Tue, 12 Oct 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Syntax for Sun f77 intrinsic fdate?

2. Problem with AMOD intrinsic function using SGI f77 compilers

3. Mod-2 and Mod-3 compiler

4. Mod-2 and Mod-3 compiler for the IBM PC and compatibles

5. Array sizes on Sun f77 compiler

6. F90 Sun compilers [Was: Migration from F77]

7. Problem with Suns F77 compiler

8. Which f77 compiler is correct (Sun, Linux)?

9. Sun F77 compiler with Vax binary file Query

10. Write to stderr: How? (Sun f77 compiler)

11. static storage compiler switch for Sun f77?

12. Sun F77 compiler

 

 
Powered by phpBB® Forum Software