IEEE-754 and Science

In the Bad Old Days, different computers performed arithmetic differently, and got different answers. Now almost everything follows IEEE-754 (also known as ISO/IEC 60559) for real arithmetic, and most things give the same answers. Some people think that this is Good, but here I argue that it is Very Bad.

Consider code for printing the first sixty values from the Logistic Map

xn+1=4xn(1-xn)

with x0=1/8.

#include<stdio.h>

double logistic(double x){
  return 4*x*(1-x);
}

int main(){
  double x;
  int i;

  x=1.0/8.0;
  for(i=0;i<61;i++){
    printf("%2d %f\n",i,x);
    x=logistic(x);
  }
  return 0;
}
function logistic(x)
  double precision :: logistic,x
  logistic=4*x*(1-x)
end function logistic

program log
  double precision :: x,logistic
  integer :: i

  x=1d0/8d0
  do i=0,60
     print *,i,x
     x=logistic(x)
  end do
end program log
def logistic(x):
    return 4*x*(1-x)

x=1/8
for i in range(61):
    print("%2d %f"%(i,x))
    x=logistic(x)
 
function logistic(x)
   return 4*x*(1-x)
end

let x=1/8
   for i in 0:60
      print(i,"  ",x,"\n")
      x=logistic(x)
   end
end

Four different languages (C, Fortran, Python and Julia), and all produce a final line of

60 0.059503

Try a different compiler, and the result is unchanged. Switch from an Intel PC to an ARM-based Raspberry Pi, and the answer is still 0.059503. So we can be sure that the correct answer is 0.059503?

No. Try

from decimal import *
getcontext().prec = 100

def logistic(x):
    return 4*x*(1-x)

x=Decimal('1')/Decimal('8')
for i in range(61):
    print("%2d %f"%(i,float(x)))
    x=logistic(x)
scale=60

define l(x){ return (4*x*(1-x)); }

x=1/8
for(i=0;i<61;i++){
	print i,"  ",x,"\n"
	x=l(x)
}
quit

These two versions, python with 100 decimal digits of precision, and bc with 60 decimal digits of precision, agree with each other, and give an answer of 0.948602. This is the correct answer, and the previous answer was wrong by a factor of fifteen.

The Problem

This calculation is numerically unstable. Rounding errors increase exponentially at each step. But because IEEE-754 defines the precision, and the precise rounding, a computer obeying it will get the same answer as any other computer obeying it. So nothing alerts us to the fact that the code is not very meaningful.

Switch to rarer, and much slower, languages which allow one to adjust the precision of arithmetic, and if one sets the precision high enough, they agree. At lower precisions, they disagree, for they do not work quite the same way internally. Here setting the precision to 17 digits gave 0.999941 from python, and 0.734634 from bc. Setting the precision to 24 gave the correct answer (to six decimal places) from both. The precision of IEEE 754 (double precision) floating point arithmetic is approximately 16 decimal digits (53 binary digits).

If I am using an algorithm which is numerically unstable, I want to be alerted to this problem by getting different results on different computers (or with different languages). Having everything make precisely the same mistakes, and thus ending up at identical wrong answers, is not helpful.

Sometimes modern computers can be fooled into breaking IEEE-754 with compiler options which enable the "FMA" instruction. This is a Fused Multiply Add, i.e. (a*b)+c, which rounds just once, not twice, so can produce different results to using a separate multiply and add. However, for this code the compiler used did not issue any FMA instructions, so this trick did not work.

When I was much younger, there were many different floating-point arithmetic conventions in use, each with its own precision and rounding rules: Cray, IBM, VAX and IEEE-754 were the major contendors. If one's code gave results which were not significantly different on all of these, then one's algorithms were probably stable. If not, then it was time for a hard think about whether any of the answers was in any useful sense correct.

Most scientists are not very good at analysing the stability of their algorithms without strong hints such as different computers disagreeing. Here we have a function with three elementary operations per iteration, and we call it just sixty times. Fewer than two hundred operations, all addition, subtraction or multiplication. What could go wrong? Yet it did go wrong.

Many scientists love running simulations which sustain hundreds of GFLOPS for many hours, without much thought as to whether their 1015 operations are safe, when sometimes two hundred are not.

Quadruple Precision

Some C compilers support quadruple precision, _Float128. Currently gcc does, and clang does not. Some Fortran compilers support a 128 bit floating point data type too. For the above example, this is sufficient to get the correct answer. Iterate the sequence somewhat further, and it will not be.

But quad precision has no hardware support on Intel or ARM, so needs to be emulated in software. This tends to be very significantly slower, perhaps as much as a hundred times slower. Coupled with poor support by compilers and libraries, quad precision is very little used, and often effectively unusable. If a calculation in double precision takes a few days of machine time, the same calculation in quad precision might take a year!

Currently I believe that IBM's Power range of CPUs are the only ones to have hardware support for 128 bit floats, and they are rather rare in the real world. Some use IBM's extended double format (105 binary bits of precision), and some IEEE's quad format (113 binary bits of precision).

#include<stdio.h>

_Float128 logistic(_Float128 x){
  return 4*x*(1-x);
}

int main(){
  _Float128 x;
  int i;

  x=1.0/8.0;
  for(i=0;i<61;i++){
    printf("%2d %f\n",i,(double)x);
    x=logistic(x);
  }
  return 0;
}
function logistic(x)
  use iso_fortran_env
  real(real128) :: logistic,x
  logistic=4*x*(1-x)
end function logistic

program log
  use iso_fortran_env
  real(real128) :: x,logistic
  integer :: i

  x=1d0/8d0
  do i=0,60
     print *,i,x
     x=logistic(x)
  end do
end program log

With the above examples, one has to wait until about n=120 before their identical answers are also completely wrong. A problem delayed, not a problem solved.