Quantcast
Channel: Recent posts
Viewing all articles
Browse latest Browse all 415

MODULO of positive arguments returns negative value

$
0
0

When MODULO is called with two positive arguments, it should always produce a nonnegative value.  However, the following code, when compiled with "-xCORE-AVX2 -O1" using ifort 17.0.2, produces a negative value!

SUBROUTINE modchk(x,n)
  REAL*4    :: x, z
  INTEGER*4 :: n
  z = x*n
  z = MODULO(z,1.e0)
  IF (z < 0.e0) THEN
     PRINT '("MODULO(v,1.e0) is negative! z=",E17.10)', z
  ELSE
     PRINT '("MODULO(v,1.e0) is nonnegative, z=",E17.10)', z
  END IF
  RETURN
END SUBROUTINE modchk

PROGRAM drv
  INTEGER*4 :: n
  REAL*4    :: x
  n = 42
  x = 2.3809524e0
  CALL modchk(x,n)
END PROGRAM drv

Example output:

$ ifort -xCORE-AVX2 -O1 modulobug.f90 -o modulobug

$ ./modulobug 
MODULO(v,1.e0) is negative! z=-0.9536743164E-06

Here is some info about my processor and compiler:

$ uname -a
Linux orpheus 4.4.0-53-generic #74-Ubuntu SMP Fri Dec 2 15:59:10 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
$ ifort -v
ifort version 17.0.2

This is quite a serious problem because it directly violates the MODULO documentation.  Many numerical codes (e.g. table based interpolation) use MODULO and do not check the sign of the return value when it is known that the arguments will be positive.


Viewing all articles
Browse latest Browse all 415

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>