Math Experts Question

Just starting out? Need help? Post your questions and find answers here.
Dr_Pixel
User
User
Posts: 36
Joined: Fri Oct 24, 2003 1:36 pm

Math Experts Question

Post by Dr_Pixel »

I am trying to convert some files from binary, to plain text.

I have no documentation on the file format of the binary files, but I do have examples to use of both the binary file, and it's text equavalent.

OK, the first filetype went fine - by comparing the two files, I was able to work out the file format, what parts converted to asci equavalents, what parts were integer numbers, and which were floats.

Now, I'm looking at the second file type, but in addition to asci text, integers, and floats, I find some numbers like these:

-2.62766e-012
-5.88894e-005

What the heck are those? More importantly, how would I convert them from a hex number in the file to text output like that?

I load the whole binary file into memory, then I read the floats by PeekF into a float variable, the integers by PeekL into a long variable.

How would I read this type of number? And print it in that format as above?
Dr Pixel
User avatar
waffle
Enthusiast
Enthusiast
Posts: 129
Joined: Mon May 12, 2003 1:34 pm
Location: USA
Contact:

Post by waffle »

not sure what your file format is,
but those numbers are floats, although very small numbers.

-2.62766e-012 could be translated to

-0.00000000000262766
Code is good... Its an international language.
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

Yeah, waffle's right. Sometimes an e is used like that to mean *10^. So -2.62766e-012 means -2.62766*(10^-12). I think the reason for this is that a float cant store enough decimal places to represent numbers like these. But if you were wanting to display them as decimals you could use strings and just shift the decimal place along.
User avatar
geoff
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Apr 27, 2003 12:01 am
Location: Cornwall UK
Contact:

Post by geoff »

-2.62766e-012
-5.88894e-005

What the heck are those? More importantly, how would I convert them from a hex number in the file to text output like that?
They are stored like this so that number accuracy is independent of number size.

You need to know what format these floats are stored in, for example IEEE double and IEEE single are 2 such formats.

These formats specify which bits in the binary (or hex) number are used for storing the sign, the mantissa (the 5.88894 bit) and the exponent (the e-012 bit)

If the format is the same 4 byte float format used by Purebasic then you are lucky and can use the PB command ReadFloat() to read these bytes into a float variable. If the format is different then you must separate out the different parts of the number in your code and assemble the float number accordingly.

If the format is IEEE 8 byte float (ie double) then you will have to wait a short time until this format in incorporated into pureBasic.
Post Reply