-3

I am trying to do some calculations in Fortran that looks like:

large number (order E40) - large number (order E40)

I should get back zero. Most of the time it works, but in a couple of cases I'm getting weird numbers. One answer Fortran gave me was -1E20. Another weird answer I got was 32768, which is 2^15, oddly enough.

Does anyone have any clue as to why this is happening?

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
naomig
  • 271
  • 1
  • 7
  • 12

1 Answers1

0

It's hard to tell without actual code, but...

This is only to be expected if the numbers are sufficiently similar. While 1e20 is pretty large compared to 1 or 2, it is pretty small compared to 1e40.

In fact, even with double precision, you only have 15-17 digits of precision. Considering that, the values you get are below the accuracy possible with numbers in the range of 1e40.

What you see is numerical noise.


[ Another possibility, of course, is that you are trying to do this in single precision. This is not possible (max. exponent ~38) and anything might happen. ]

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • Right. Sorry, I should have formulated my question a little better -- I have two very large numbers that I need to subtract. When I do the subtraction by hand, the answer is zero. Fortran, however, spits out a number that is definitely not zero--not even close. I realize that this is an issue with precision. All my variables are currently double precision, and I'm wondering if there's a way to increase that. After looking around on the internet, I've tried real*16 and real*10, but neither of those work. – naomig Jul 27 '15 at 14:43
  • 1
    @naomig You should edit the question, then. Maybe you should take some time to read [ask]... Improving the question might remove the down-votes and close-votes. – Alexander Vogt Jul 27 '15 at 14:45