The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 float or int?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Mike Buess

USA
3037 Posts

Posted - 01/09/2005 :  10:22:55 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5
Operating System: XP

I have some binary data files with the same format but some of the files contain 4-byte floating point values and others contain 4-byte integers. (None contain a mixture of float and int.) Is there a standard method for determining if a number is float or int? Now I read (OriginC) the first couple of values as float and decide if the results are reasonable using these criteria...

if( abs(float value)>1E36 ) too large so must be int;
if( abs(float value)<1E-36 && abs(float value) ) too small so must be int;

This works for the files I've tested but the cutoff values (1E36 and 1E-36) are rather arbitrary. I can't keep from thinking that a more reliable test exists.

Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 01/09/2005 10:37:06 AM

ML

USA
63 Posts

Posted - 01/10/2005 :  10:55:03 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Mike,

There is no reliable way to do this.

For a four-byte (=32 bit) storage, there are 2^32=4294967296 possible bit combinations. All of them are considered to be legal 4-byte integers by an x86 CPU, and it is only up to the person-program using a four-byte integer to determine whether the number makes sense or not. Note that this is true whether abs(float value) is greated than 1E36 or not or less than 1E-36 or not.

Here is one example:

The float value of 1.F (F at the end of the literal indicates that it is a float, not a double) is represented like this when looked at as a 4-byte integer (here shown in hex representation):

0x3F80 0000

In decimal this would be 66584576 . Who is to say whether 66584576 is a "bad" integer?

In general, it has got be a bad practice to stuff a value into 4-bytes without knowing at a later stage in which format the value is stored.

If you are intersted, here is from Microsoft's help the description of the float type in memory (this is IEEE standard):


http://support.microsoft.com/default.aspx?scid=kb en-us 36068


Note that not all combinations of 32bit values are considered legal float values, but most of them are (the legality can probably be inferred from Micorsoft's link above).

ML




Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 01/10/2005 :  11:30:19 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks ML,
quote:
There is no reliable way to do this.
I was afraid of that. I'll look at the MS link anyway.
quote:
In general, it has got be a bad practice to stuff a value into 4-bytes without knowing at a later stage in which format the value is stored.
The data type is specified in a separate parameter file but that file might not be available in some cases and then you have to guess. Most of the time it's obvious from the resulting data that you've guessed wrong but I was trying to avoid the necessity of re-importing.

Mike Buess
Origin WebRing Member
Go to Top of Page

Hideo Fujii

USA
1582 Posts

Posted - 01/10/2005 :  12:01:18 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Another possible "heuristics" may be to check if
there is ON bit in 2nd-9th bit (Exponent) area
assuming that your (positive) integer is always
smaller than 2^23 (fit within Mantissa area).

--Hideo Fujii
OriginLab




Edited by - Hideo Fujii on 01/10/2005 12:05:57 PM
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 01/10/2005 :  2:14:08 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks Hideo, I'll look into that.

Mike Buess
Origin WebRing Member
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000