Solving Negative Infinity in Fortran Calculation

  • Context: Fortran 
  • Thread starter Thread starter nguyenthanhlam
  • Start date Start date
  • Tags Tags
    Fortran Infinity
Click For Summary

Discussion Overview

The discussion revolves around the issue of obtaining negative infinity in output from a Fortran calculation involving the computation of maximum values from arrays. Participants explore potential reasons for this unexpected result, focusing on the handling of array variables and the behavior of the MAXVAL function in the context of floating-point arithmetic.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Lam describes the calculation for FAMAX and FPBPMAX and expresses confusion over receiving negative infinity instead of NaN or positive infinity.
  • Some participants point out the potential confusion arising from the use of FAMAG as both a single variable and an array, questioning whether it was originally dimensioned correctly.
  • There is a suggestion that assignments to FAMAG should specify an index, as direct assignment may not work as intended in Fortran.
  • One participant proposes that the MAXVAL function might initialize its return value to negative infinity, which could lead to this result if all elements are NaN, due to the properties of IEEE floating-point arithmetic.
  • Lam confirms that FAMAG, FX, FY, and FZ are indeed of the same dimensions and acknowledges the possibility that MAXVAL returns negative infinity when all elements are NaN.

Areas of Agreement / Disagreement

Participants express varying views on the handling of array assignments and the behavior of the MAXVAL function. While some agree on the potential issues with array indexing, there is no consensus on the exact cause of the negative infinity output.

Contextual Notes

Participants note the complexities of floating-point arithmetic and the specific behavior of the MAXVAL function in relation to NaN values, which may affect the results of the calculations.

nguyenthanhlam
Messages
9
Reaction score
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
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?
 
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
 
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.
 
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).
 
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
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
9K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 24 ·
Replies
24
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K