View previous topic :: View next topic |
Author |
Message |
dgurok
Joined: 26 May 2011 Posts: 66
|
Posted: Mon Mar 25, 2019 10:44 am Post subject: Invalid floating point number |
|
|
Hello,
I try to read out a value from a binary data set which results in invalid floating point number. This is correct because the value is stored as null or NaN or something like that. I can't do any operations with my value because it results in invalid floating point operation.
Can I use some try catch mechanisms to recognize the problem so that I can fix the value? Perhaps something else? |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Mon Mar 25, 2019 12:14 pm Post subject: |
|
|
There is no "try/catch" mechanism.
There is a way to trap exceptions (at least for Win32) but I doubt if you can recover (i.e. proceed) after the trap in this context.
Maybe you could read in the value as a character variable and do some initial processing before using an internal READ to convert the string to a REAL.
So something like...
CHARACTER(256) str
REAL x
READ(....) str
!Preprocess str
READ(str, ...) x |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1886
|
Posted: Mon Mar 25, 2019 3:09 pm Post subject: |
|
|
Here is a slight variation on Paul's suggestion.
You may read the bytes that make up the floating point number in the file into an integer or a pair of integers. You can then check those one or two integers against a list of known values that, when considered as integer representations of floating point values, are invalid, NaN, etc. If the integer(s) does not (do not) match any of those, you can use TRANSFER to obtain the corresponding floating point values, and be confident that no exceptions will be generated.
The performance of either of the schemes will not be good, but that is the price to pay for checking every value instead of relying on interrupts being caused by invalid values. |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Mon Mar 25, 2019 8:14 pm Post subject: |
|
|
This subject is worth exploring and I will make a note to investigate.
In the meantime there is a relevant FTN95 library function illustrated here...
Code: | C_EXTERNAL invalid_double 'invalid_double'(VAL):LOGICAL*4
real(2) x
!Get the value of x
if(.NOT.invalid_double(x)) print*,x
end |
|
|
Back to top |
|
|
dgurok
Joined: 26 May 2011 Posts: 66
|
Posted: Tue Mar 26, 2019 9:44 am Post subject: |
|
|
Thank you for the feedback.
Pauls function works great =)
Yesterday we did a buffer compare to exclude the problem. |
|
Back to top |
|
|
John-Silver
Joined: 30 Jul 2013 Posts: 1520 Location: Aerospace Valley
|
Posted: Tue Mar 26, 2019 9:48 pm Post subject: |
|
|
where's the documentation for the function invalid_double ? _________________ ''Computers (HAL and MARVIN excepted) are incredibly rigid. They question nothing. Especially input data.Human beings are incredibly trusting of computers and don't check input data. Together cocking up even the simplest calculation ... " |
|
Back to top |
|
|
|