Fixing NaN Problem in f90 Do Loop

  • Thread starter fun
  • Start date
In summary, Fun encountered a problem in f90 today. The problem is that somewhere in middle of the do loop, a variable goes larger than exp(-307). I have to invert that variable, then I am getting NaN. So f90 is working until the variable reaches exp(-307), but after that it is becoming infinity, so I am not able to invert it. Could you please able to help in fixing this issue.
  • #1
fun
2
0
Hi, I encountered a problem in f90 today. The problem is that somewhere in middle of the do loop, a variable goes larger than exp(-307). I have to invert that variable, then I am getting NaN. So f90 is working until the variable reaches exp(-307), but after that it is becoming infinity, so I am not able to invert it. Could you please able to help in fixing this issue.
 
Technology news on Phys.org
  • #2
usually when you hit something like this you jump to the next higher precision datatype like from real to double but in this case it looks like you've hit that limit leaving three choices:

1) shift to logs

2) rework your calculations some that your numbers are in a more reasonable range such as +/-7 ie factor out 10^300 and put it back in later at the end.

3) use a big number library if one is available, but this is usually much slower than working with doubles.
 
Last edited:
  • #3
I am already using the double precision. The third way, I do not know how to implement it. Could you please give some source links or examples. The second way I tried, but its a scientific calculation, so i do not have any control over that. Actually i need to multiply a matrix 20 times. Thats whre i got this error.

Regards,
Fun.
 
  • #5
another thought is to intersperse the multiply and divide operations in your calculations if possible ie

x * y * z / (a * b * c) = x / a * y / b * z / c

this prevents any intermediate result from getting too large.
 
  • #6
Hey Fun and welcome to the forums.

If you can read C++, you will find plenty of examples of big integer libraries where you can get the source code.

Ultimately it will depend on what operations you wish to use. If you are using only arithmetic then you will find plenty, but if you are using exponentials, logarithms or something else then you might have to end up buying a decent commercial library like MIRACL if you want to use it commercially.

What operations (arithmetic, exponentiation, logarithms, etc) are you using?
 
  • #8
since you're doing matrix multiplies can't you factor out a 10^300 from all elements of the matrices and then factor it back in at the end or wherever convenient?
 
  • #9
Well, nobody seemed to have bothered asked the very first question!

fun:

Before I asked, though, will you please clarify something for me...in your posting, you say that a variable goes "larger than exp(-307)"...can you write this number in scientific notation...I am not 100 percent sure what it is.

Here are my questions:

Question 1.- Are the numbers supposed to get this large?

and then we continue:

Do you know the solution to your problem?
Have you tried your algorithm with a smaller problem you know the solution to?

before you go out on a wild goose chase looking for libraries, etc.

Although the solution suggested about reducing your number in the first place is not a bad idea...maybe, if you do not want to reduce your number right up front, you can do it once in a while and keep track how many times you have divided by 10...maybe you can divide by 10 (or 100 or whatever necessary) after every multiplication.

But, first, verify your algorithm is working correctly!

my 2 cents
 
  • #10
In fortran double precision limits are in the order of 1 e -308 to 1 e 308

Double-precision real floating-point values in IEEE T_float format ranging from 2.2250738585072013D-308 to 1.7976931348623158D308.

One other thing to check in your program is where a variable is the result of a sqrt or other math function that can't accept a negative number as an argument as fortran will set the result to nan.
 
  • #11
Good to know.

But I just wanted fun to tell us more.

First, I thought fun's statement sounded contradictory in saying that something 'goes larger than exp(-307)'...shouldn't it be smaller than?

Also, I was not 100% sure of what fun meant with his choice of 'words'; after all, exp() is a Fortran intrinsic function and, so, when fun said exp(-307)...I did not know if he meant 2.71**-307...which, I guess, is still pretty bad.

Anyway, I am still looking forward to the answers to my questions. fun?
 
  • #12
By the way, taking one step back...having something like 1.0e-307 ...shouldn't this simply taken as a zero?
 
  • #13
Or did fun meant that something went so large (larger than 1.0e308) that it went negative?
 
  • #14
i looked at some f90 comments on the web and they said you get a NaN when trying to do the square root of a negative number and that he should backtrack thru his code to see where that could happen.

With respect to the 1E-308, I think there would have to be an implicit check in each calculation to readjust it to zero and that that would be unacceptable to many programmers both from efficiency and from accuracy of computation.

Instead, the burden is on the programmer to use the math rounding functions to explicitly change the number to zero when it is so close within some tolerance to zero.
 

FAQ: Fixing NaN Problem in f90 Do Loop

1. How do I identify NaN values in a f90 do loop?

To identify NaN values in a f90 do loop, you can use the isnan function, which returns .TRUE. if a value is NaN and .FALSE. otherwise.

2. What causes NaN values to appear in a f90 do loop?

NaN values can appear in a f90 do loop when a mathematical operation results in an undefined or invalid value, such as dividing by zero, taking the square root of a negative number, or raising a negative number to a non-integer power.

3. How do I handle NaN values in a f90 do loop?

To handle NaN values in a f90 do loop, you can use an if statement to check for NaN values and handle them accordingly, such as skipping over them or assigning a specific value to them.

4. Can NaN values affect the results of my f90 do loop?

Yes, NaN values can affect the results of your f90 do loop if they are included in any calculations. This is because NaN values are considered "contaminants" and can produce unexpected results in subsequent calculations.

5. How can I prevent NaN values from appearing in my f90 do loop?

To prevent NaN values from appearing in your f90 do loop, you can check for potential sources of NaN values, such as division by zero, and handle them appropriately. You can also use built-in functions, such as max and min, to ensure that your loop does not produce invalid values.

Similar threads

Replies
4
Views
4K
Replies
2
Views
5K
Replies
11
Views
2K
Replies
5
Views
2K
Replies
8
Views
1K
Replies
1
Views
28K
Replies
8
Views
2K
Replies
3
Views
917
Back
Top