Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28701 Discussions

Determination of data type based on value

mjfinney
Beginner
446 Views
Has anyone seen an algorithm which can reliably determine the data type (float or integer) of a variable based on its value. For example, a utility subroutine which outputs/prints array values. The arrays are of both Int*4 and Real*8 and some of the arrays of type Real*8 contain integer values in some, but not all, locations. For instance, the real*8 array may contain decimal 281 in the form Z"00000119". An added complexity is that the code should ideally run on DEC ALPHA running OpenVMS as well as PC running DVF 6.6 (UNIX portability would be nice as well). I have looked at the EXPONENT and FRACTION intrinsics. The current algorithm involves equivalencing a Real*8 and a Integer*4 and then evaluating the Integer's bit patterns and making some assumptions such as a float will contain TRUE bits in the exponent field. The problem is that the 2 platforms have different float data representations and the PC holds the exponent field in the Most Significant 32 Bits while the DEC holds them in the 32 LSB. Therefore, on the PC, the equivalenced integer does not capture the exponent fields from the Real*8.
0 Kudos
3 Replies
Steven_L_Intel1
Employee
446 Views
If you compile on Alpha with /FLOAT=IEEE, your life will be much easier, as then the same float representation will be used as on the PC.

Steve
0 Kudos
Intel_C_Intel
Employee
446 Views
I don't think a general determination is possible. Most
any set of 4 bytes would make a perfectly good integer
OR real number. However, if you know the range that
your real or integer variable should fall within, you
can use that to make a determination. I have a case
where early versions of a binary file contained a 4 byte
integer, and later versions of the same file type have
a 4 byte real. Knowing that the variable should fall
in the range 0 to 1000, I read the ambiguous data into a 4 byte integer and check the range. If the data is actually the real version, the value of the integer variable will be huge, due to the placement of the exponent bits in the IEEE format, and I can use an equivalenced real variable instead to get the correct value.
0 Kudos
mjfinney
Beginner
446 Views
Thanks for the replies,

I agree with your point ( /FLOAT=IEEE ), however, the current problem arises from a port to the PC. This code has been running on the DEC ALPHA for years and many post-processing tools have been developed which assume /FLOAT=VAXG or /FLOAT=VAXD.

I am currently writing platform specific logic and implementing it with the IF Directive Construct
cDEC$ IF DEFINED ( _WIN32 )
PC code
cDEC$ ELSE
DEC ALPHA code
cDEC$ ENDIF

0 Kudos
Reply