View previous topic :: View next topic |
Author |
Message |
KennyT
Joined: 02 Aug 2005 Posts: 318
|
Posted: Thu May 31, 2012 9:04 am Post subject: Floating point exception - using equivalence |
|
|
Hi,
We get datafiles from external sources, supposedly in a standard format. Occasionally, these files contain some small amounts of "rubbish". What we do to try and decode them is to read the data into an INTEGER buffer, equivalence that to a REAL buffer, and read the values from whichever buffer is appropriate for the expected data type:
INTEGER IBUFF(NVALS), ITYPES(NVALS)
REAL RBUFF(NVALS)
EQUIVALENCE (IBUFF, RBUFF)
READ(LU) (IBUFF(I),I=1,NVALS)
DO I=1,NVALS
IF(ITYPES(I).EQ.1 ) THEN !data value is integer
RVAL = IBUFF(I)
ELSE IF( ITYPES(I).EQ.2) THEN !data value is real
RVAL = RBUFF(I)
END IF
..
..
END DO
The problem is that, when the data is corrupted, the value for RBUFF(I) can be an illegal floating point value and the assignment to RVAL causes a crash.
What I would like to do is to be able to test the value of IBUFF(I) to see if it would be an invalid real when equivalenced to RBUFF(I) and to report accordingly, rather than "fall over".
So, is there a "simple" range of integer values that correlate to "valid" reals or an internal function that can do this test? (Presumably, the debugger knows, so it can print "Illegal floating value")
TIA
K |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8210 Location: Salford, UK
|
Posted: Thu May 31, 2012 10:58 am Post subject: |
|
|
For single precision...
Code: | program test
C_EXTERNAL INVALID_FLOAT@ 'invalid_float'(VAL):LOGICAL
real val
if(INVALID_FLOAT@(val)) print*, "Bad number"
end
|
|
|
Back to top |
|
 |
KennyT
Joined: 02 Aug 2005 Posts: 318
|
Posted: Thu May 31, 2012 11:16 am Post subject: |
|
|
Excellent! Thanks!
K |
|
Back to top |
|
 |
KennyT
Joined: 02 Aug 2005 Posts: 318
|
Posted: Wed Aug 01, 2012 12:51 pm Post subject: |
|
|
Hi Paul,
is there an equivalent call for real*8? (invalid_double??)
K |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8210 Location: Salford, UK
|
Posted: Wed Aug 01, 2012 3:03 pm Post subject: |
|
|
Yes, and it is called invalid_double. Just change the name. |
|
Back to top |
|
 |
KennyT
Joined: 02 Aug 2005 Posts: 318
|
Posted: Wed Aug 01, 2012 3:28 pm Post subject: |
|
|
Tks. Is there a list of all these useful functions? I've tried googling for "invalid_double" with Windows API but nothing obvious comes up!
K |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8210 Location: Salford, UK
|
Posted: Thu Aug 02, 2012 8:02 am Post subject: |
|
|
The only other one in this class is invalid_long_double. |
|
Back to top |
|
 |
|