[FORTRAN] Division by zero allowed?

Click For Summary

Discussion Overview

The discussion revolves around handling a division by zero error in FORTRAN when squaring a very small complex number and subsequently dividing by it. Participants explore different compiler behaviors, precision settings, and potential workarounds to avoid the error.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an issue with dividing by a very small complex number (1e-20) that results in a floating point exception.
  • Another suggests performing calculations in double precision to mitigate the issue.
  • There are inquiries about how to implement double precision in the code.
  • Some participants note that different compilers handle floating point exceptions differently and suggest checking the compiler's user manual for options to control behavior on division by zero.
  • One participant expresses concern about ignoring the error and emphasizes the importance of addressing the underlying issue instead.
  • Another participant speculates that the older compiler may use higher precision floating point math, which could explain the difference in behavior.
  • A suggestion is made to rearrange the calculation to avoid division by the small number directly.
  • One participant reports successfully resolving the issue by switching to a different compiler (g95).

Areas of Agreement / Disagreement

Participants express differing opinions on whether it is acceptable to ignore the division by zero error. There is no consensus on the best approach to handle the situation, with multiple suggestions and concerns raised.

Contextual Notes

Participants mention the potential for different compilers to have varying default behaviors regarding floating point calculations and error handling, which may affect the results and error reporting.

Who May Find This Useful

This discussion may be useful for FORTRAN programmers dealing with floating point exceptions, particularly in the context of complex number calculations and compiler-specific behaviors.

zstratto
Messages
6
Reaction score
0
Hi, I have an issue where I am squaring a very small complex number 1e-20 (FALFA2) which seems to make the value zero (both the real and imag part) and then dividing by it, the problem line is (all the numbers are complex):
GALFA = ( FBETA * FAA / FALFA2 ) - ( FAB / FALFA )

I get the error 'unknown floating point exception'. However, when another compiler compiled this code the executable works fine and I don't get an error for this (this is an old code that I am making some edits to now). Is there a way to just let GALFA=INF or something and continue, or do whatever the other compiler did (whatever that is)? Thank you!
 
Technology news on Phys.org
Do the calculation in double precision.
 
How exactly do I do that?
 
double precision or quadruple!

Different compilers do behave slightly different, by default, and there may be a way to control their behavior, too...you may want to read the user's manual for your compiler. I know that with g95, for example, one can set an environment variable to let the program continue or terminate on a "divide by zero" error.

I seem to recall seeing IEEE related function calls to control reporting this problems and behavior.

You can turn a blind eye and let the program continue, but what's the point? are you going to believe the results on a run like that?.

Or

...fix the program and guard against things like this...what is the order of magnitude of your numbers? when is a zero, zero?

Can you scale your problem and work with larger numbers?
 
tell which compiler you are using

post the commands you are using to compile the program
 
Hi Gsal,

I am using silverfrost (ftn95), and I am not really using any commands I just click 'compile'. And yes, I would just like to turn a blind eye. As I said, I have the executable that is the exact same code and it works fine, I just need to add a few write/print statements, but due to this issue I get the error I described.
 
Oh, boy...that is dangerous.

First you need a bit of help and you need to be willing to be a bit more diligent about this...nothing good can come out of turning a blind eye...maybe this task is in the wrong hands.

Second, I don't use Silverfrost, so, I cannot give details. Hopefully somebody will come along and give you step-by-step instructions on how to find out what the compilation command being used is and how to tweak it...OR, you could investigate yourself.

Good luck.
 
zstratto said:
How exactly do I do that?

Check your FORTRAN manual.
 
zstratto said:
I get the error 'unknown floating point exception'. However, when another compiler compiled this code the executable works fine ...
My guess is that the older compiler uses higher precision floating point math, ending up dividing by a small number instead of zero. A similar thing happened with microsoft c. The old 16 bit compilers supported "long double" using the 80 bit format, but the 32/64 bit compilers changed this so that "long double" is the same as "double" at 64 bit format (perhaps an issue related to a win32 console mode program, but assembly code can still use the 80 bit format).
 
Last edited:
  • #10
zstratto said:
Hi, I have an issue where I am squaring a very small complex number 1e-20 (FALFA2) which seems to make the value zero (both the real and imag part) and then dividing by it, the problem line is (all the numbers are complex):
GALFA = ( FBETA * FAA / FALFA2 ) - ( FAB / FALFA )

Just guessing here, but FALFA2 = FALFA*FALFA. Is that right? In that case you'd be much better off rearranging the calculation as :

GALFA = ( FBETA * FAA / FALFA - FAB ) / FALFA
 
  • #11
I was able to get around this by using a different compiler (g95), thank you everyone for the help!
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 17 ·
Replies
17
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K