Friday 24 July 2015

Raising a negative number to a non-integer power

It is undefined to raise a negative number to a non-integer power. However, Fortran compilers differ in their handling of the case when the power is an integer represented as a floating point number. Consider the following code:

      program test
      implicit double precision(a-h,o-z)
      a=-26.5227750387574375d0
      b=1.0000000000000000d0
      write(6,*)a**(b-0.01d0)
      write(6,*)a**b
      write(6,*)a**(b+0.01d0)
      end

With Intel compiler this is the result:

> ifort test.f
> ./a.out 
                     NaN
  -26.5227750387574     
                     NaN

But with NAG compiler all of the results are NaN:

> nagfor -ieee=full test.f
> ./a.out 
                     NaN   
                     NaN
                     NaN