Wrong (strange outputs) of real numbers

@seme4ka008 ,

Please note given binary arithmetic, the numbers are essentially the same. You can consider adding the following:

    .
    print *, "z1 = ", z1
    print "(g0,/,b0)", "z1 (Bit representation):", z1
    .
  • With IFORT:
C:\temp>p.exe
 y1 =  -0.527732334160114
 y2 =  -0.527732334160114
 z1 =  0.700000000000000
 z1 (Bit representation):
11111111100110011001100110011001100110011001100110011001100110
  • With GCC gfortran:
C:\temp>gfortran -ffree-form p.f -o p.exe

C:\temp>p.exe
 y1 =  -0.52773233416011411
 y2 =  -0.52773233416011400
 z1 =   0.69999999999999996
 z1 (Bit representation):
11111111100110011001100110011001100110011001100110011001100110
1 Like

Different compilers may give different list-directed output. Note that Gfortran outputs 17 significant digits, whereas Ifort outputs 15. You should take this into account when comparing.

If you want a specific number of significant digits to be output, you need to specify an appropriate output format, such as ES23.15, instead of using list-directed output.

1 Like

It was not stated explicitly, but this is allowed by the fortran standard. The standard gives much leeway to the processor in list-directed output. It is not just the numbers of digits that are printed, but also the line lengths and record structure that can change with compiler options, different versions of the same compilers, and different compilers.

If you want to compare outputs, say with something like a diff command, then programmers should avoid list-directed i/o and use explicit formats instead. Even some explicit formats, such as g0 and e0, can vary from compiler to compiler. It is unfortunate that the standard was not more specific when these formats were introduced, and now, because of backwards compatibility, it would be problematic to change the standard.

One more big thanks for everyone in this thread. I think the last replies answered on all of my questions

2 Likes

One thing that @seme4ka008 needed to know is that computers cannot do the mathematics that people learn at school. Every computer is of finite size, and their innards are made of atoms that have finite sizes. So they cannot store infinitely many digits. However many digits they stop after, there will always be problems with real (or complex) numbers.

1 Like

And supposing we could store an infinite number of digits, a mere addition would take an infinite time! :grimacing: We are limited by space and time.

Only symbolic computing can do such computations. We can manipulate numbers like \pi without knowing all their infinite digits because we know their properties and use them in our computations.

All that can be resumed telling that machine numbers (real) are not real numbers (\mathbb{R}). Just a finite subset.

Floating point numbers are a finite subset of the mathematical rational numbers, which of course in turn are a countably infinite subset of the mathematical real numbers, which themselves form an uncountable set.

2 Likes

A possibly better version can be found in the successor to Sun Microsystems Their floating point group fixed a variety of minor issues in the original ACM paper