0

Is there a way to returning nan values instead of an error (compile or runtime) in invalid operations such as: 'sqrt(-1.)' or '1./0.'? (I mean without 'if' conditional and -finit-real flag) I've tried this example

Program test
use, intrinsic :: ieee_features
use, intrinsic :: ieee_arithmetic
use, intrinsic :: ieee_exceptions
implicit none
real:: a 
a = 1./0.
print*, a

end program test

,and I get this error:

4 | a = 1./0. | 1 Error: Division by zero at (1) (I'm a newbie in this subject, maybe I've made some mistakes)

user
  • 309
  • 1
  • 9
  • 1
    I'm afraid I don't find your question to be clear. It's also a little broad, as you seem to be asking at least three different questions. Please focus on just one thing and clarify a little. However, are you asking about getting a (IEEE) NaN instead of a compile-time complaint with `sqrt(-1.)` to use as a constant? (Also, `1./0.` isn't an IEEE invalid operation where a NaN would be the expected result.) – francescalus Jan 27 '21 at 11:40
  • 1
    Thanks, but there's still the aspect of: _why_ do you want to do a literal `1./0.` in your program? If you want a constant "positive infinity" there are better ways to do that. Otherwise, the suggestion would be to "not do that". If you're worried about "what happens if I do `x/y` when `y` may be zero", then you can experiment to see that in the case of variables you can avoid this compile-time error. – francescalus Jan 27 '21 at 11:55
  • For the case of NaN see [this other question](https://stackoverflow.com/q/31971836/3157076); you can do the same for infinities with minimal changes. – francescalus Jan 27 '21 at 12:00
  • @ francescalus Thank you, I tried to assign a variable for each value and that solve my question. Anyway, if someone is not a software developer, does s/he need to use IEEE intrinsic modules? – user Jan 27 '21 at 12:08
  • That's an interesting question, but well beyond the scope of a comment. It's quite involved: often one can say "you aren't using any entity from the module so it doesn't matter", however in the case of one IEEE module simply having things in scope (potentially) changes the behaviour of the program. I use the modules (for numerical computation), but in only a small fraction of my lumps of code. – francescalus Jan 27 '21 at 12:20
  • Ok, I understand. And what are the changes? In general terms, does it improve the compiling and/or running speed? – user Jan 27 '21 at 12:39
  • 1
    Conformance with IEEE standard, potentially determining behaviour (accuracy, exceptions, performance, etc.). – francescalus Jan 27 '21 at 12:42

0 Answers0