Solving Negative Infinity in Fortran Calculation

In summary, the author is having trouble with negative infinity being returned in their calculations. They ask for help, and explain that the problem is with the function MAXVAL, which sets its return value to negative infinity if all of the inputs are NaN.
  • #1
nguyenthanhlam
9
0
Dear all,

I can not figure out why I got negative infinity in my output, so please help. Here is my calculation for FAMAX:

FAMAG=SQRT(FX**2+FY**2+FZ**2)

FAMAX=MAXVAL(FAMAG(1:NATOM))

and for FPBPMAX:

FMAGPBP(1:NSYN)=SQRT(FXGTASE(1:NSYN)**2+FYGTASE(1:NSYN)**2+FZGTASE(1:NSYN)**2)

FPBPMAX=MAXVAL(FMAGPBP(1:NSYN))

FMAGPBP(1:NSYN)=SQRT(FXTPASE(1:NSYN)**2+FYTPASE(1:NSYN)**2+FZTPASE(1:NSYN)**2)

FPBPMAX=MAX(MAXVAL(FMAGPBP(1:NSYN)),FPBPMAX)

FMAGPBP(1:NSYN)=SQRT(FXEDASE(1:NSYN)**2+FYEDASE(1:NSYN)**2+FZEDASE(1:NSYN)**2)

FPBPMAX=MAX(MAXVAL(FMAGPBP(1:NSYN)),FPBPMAX)

I expected it crash and got either NaN or Infinity for FAMAX and FPBPMAX at some point. But what I did not expect was FAMAX and FPBPMAX got NEGATIVE infinity when it crashed. Does anyone know why?

Thanks,

Lam
 
Last edited:
Technology news on Phys.org
  • #2
In the first line, FAMAG is used as a single variable; in the next line, FAMAG all of a sudden has become an array variable. This sudden change can be very confusing. Was FAMAG originally dimensioned as an array variable?
 
  • #3
SteamKing said:
In the first line, FAMAG is used as a single variable; in the next line, FAMAG all of a sudden has become an array variable. This sudden change can be very confusing. Was FAMAG originally dimensioned as an array variable?

FX,FY,FZ, FAMAG are arrays, but FAMAX is dimensionless. I know I should have used clearly different names to avoid confusion.

Again, FMAGPBP is an array, FPBPMAX is dimensionless.

Lam
 
  • #4
I think when you assign a value to FAMAG in the first line, you have to assign it to a particular location in the array. You can't write FAMAG = some expression, when you need to write FAMAG (array_index) = some expression.
 
  • #5
SteamKing said:
I think when you assign a value to FAMAG in the first line, you have to assign it to a particular location in the array.

A statement like FAMAG =SQRT(FX**2+FY**2+FZ**2) should work in Fortran 90/95 , so long as FAMAG, FX, FY, FZ are all arrays with the same dimensions. it is equivalent to a loop (or nested loops fur multi-dimension arrays) to operate on each element of the arrays.

The only thing I can think of is something weird like:
Your implementation of the MAXvAL function starts by setting its return value to negative infinity, and then scans through the array to find larger numbers. But because of the curiosities of IEEE floating point arithmetic, comparisons involving NaN tend to always return "false" (in fact NaN is not even equal to NaN!) So the maximum value of an array consisting entirely of NaNs would be returned as negative infinity. (But if that is the case, it sounds like a bug to me).
 
  • #6
AlephZero said:
A statement like FAMAG =SQRT(FX**2+FY**2+FZ**2) should work in Fortran 90/95 , so long as FAMAG, FX, FY, FZ are all arrays with the same dimensions. it is equivalent to a loop (or nested loops fur multi-dimension arrays) to operate on each element of the arrays.

The only thing I can think of is something weird like:
Your implementation of the MAXvAL function starts by setting its return value to negative infinity, and then scans through the array to find larger numbers. But because of the curiosities of IEEE floating point arithmetic, comparisons involving NaN tend to always return "false" (in fact NaN is not even equal to NaN!) So the maximum value of an array consisting entirely of NaNs would be returned as negative infinity. (But if that is the case, it sounds like a bug to me).

Yes FAMAG, FX, FY, FZ are in the same dimensions.

And your guessing is right. Maxval(array(1:n)) gives -Infinity if all the elements are NaN. I just did a simple test to confirm this. Thanks a lot.

Lam
 

1. How do I handle negative infinity when performing calculations in Fortran?

One way to handle negative infinity in Fortran is to use the IEEE_ARITHMETIC option in the Compiler Options menu. This will enable the compiler to generate code that checks for and handles negative infinity during calculations.

2. What are the potential causes of negative infinity in Fortran calculations?

Negative infinity can occur in Fortran calculations due to several reasons, such as division by zero, taking the logarithm or square root of a negative number, or underflow/overflow of floating-point numbers.

3. How can I prevent negative infinity from occurring in my Fortran calculations?

To prevent negative infinity from occurring, it is important to check for potential errors in the code and handle them appropriately. This can include using conditional statements to avoid division by zero, ensuring that the input values are within the valid range for mathematical operations, and using IF statements to handle underflow/overflow conditions.

4. What are some strategies for debugging negative infinity errors in Fortran calculations?

If you encounter negative infinity errors in your Fortran calculations, some strategies for debugging include using WRITE statements to print out the values of variables at different stages of the calculation, using the DEBUG option to trace the execution of the program, and using a debugger tool to step through the code line by line.

5. Are there any built-in functions in Fortran that can handle negative infinity?

Yes, Fortran has several built-in functions that can handle negative infinity, such as HUGE() and TINY(), which return the maximum and minimum representable floating-point numbers, respectively. Additionally, the IEEE_ARITHMETIC option mentioned earlier can also handle negative infinity in calculations.

Similar threads

  • Programming and Computer Science
Replies
7
Views
8K
Replies
2
Views
1K
  • Quantum Physics
Replies
1
Views
947
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Special and General Relativity
Replies
24
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Introductory Physics Homework Help
Replies
11
Views
764
Replies
19
Views
2K
  • Precalculus Mathematics Homework Help
Replies
10
Views
2K
Back
Top