WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Number Conversion

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to Change a SINGLE Floating Point Number to a Winbatch Floating Point Number

Keywords: 	 floating point

This article is outdated. Use the new DataCast function found in WinBatch versions 2006B or newer.


WinBatch, internally, keeps floating post numbers in a 64-bit format that matches the IEEE 64-bit double precision standard. The math chips and compilers et al on PC tend to support this standard. Those numbers have to be stored somehow.

There is also a 32-bit IEEE standard also supported by math hardware and the such. It is possible to get a binary file that has the floating point numbers stored in the 32-bit format.

WinBatch has no built-in support for the 32-bit format. Sometimes one would like to read a floating point number in the 32 bit format and see it as a floating point number. To do this in WinBatch requires converting the 32-bit floating point number to a 64-bit floating point number

This web page has a little discussion these two floating point formats....


 http://www.csrd.uiuc.edu/~ece412/cpu/r4400/R4000.book_133.html
Good Luck


;Single precision 32-bit formst 1-8-23 ; 1 sign bit, 8 exponent bits and 32 fraction bits ;S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF ;Double precision format 1-11-52 OR 1-11-20 x-x-32 ; 1 sign bit 11 exponent bits and 52 fraction bits ;S EEEEEEEEEEE FFFFFFFFFFFFFFFFFFFF-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ; S is easy to preserve. ; E is the tricky part ; in S32 format subtract 127 to get the true exponent ; in D64 format subtract 1023 to get the true exponent ; F gets copied, left adjusted zero filled on the end ;So now we need a test case ;So ummm I think THREE in single format is ;0 10000000 100000000000000000000 ; breaking into nibbles (4 bit chunks) ; 0100 0000 0100 0000 0000 0000 0000 0000 ; 40400000 in hex ; 1077936128 in decimal ;So set test case up. Assume we did a BinaryPeek4 from a binary ;buffer to retrieve a 32 bit number. Now it it were an integer ;it would be 1077936128. But its not. So a whole bunch of bit ;bashing has to take place.... three=1077936128 ;Define some hex constants X80000000=8 << 28 X7F800000=32640 << 16 X007FFFFF=8388607 ;Figure out sign, exponent, and fraction of 32 bit number Ssign =three & X80000000 Sexp =three & X7F800000 Sfract=three & X007FFFFF ;Scoot the single precis exponent to make an integer Dexp=Sexp >> 23 ; Subtract 127 to get the true exponent, then ; add 1023 to get a double precision exponent Dexp=Dexp-127+1023 ; and scoot it back to where it is supposed to be Dexp=Dexp << 20 ; Now for the fraction. The single precision number has ; 23 bits of fraction. The double precision number has ; more, but they are not all in the first word, so we ; have to split the fraction into two parts. The top word ; of the double precision fraction has 20 bits, leaving 3 ; bits to move to the next word ; For the high word just scoot 3 bits off the end to get ; 23 bits down to 20 SFH=Sfract >> 3 ;for the low word save the bottom 3 bits and scoot them up ; to the top of the word SFL=(Sfract & 7) << 29 ;Build high and low double precision words as two integers DH=Ssign | Dexp | SFH DL=SFL ; Now store those two integers into a binary buffer as 8 bytes bb=BinaryAlloc(8) BinaryPoke4(bb,0,DL) BinaryPoke4(bb,4,DH) ;then read those 8 bytes as a WinBatch compatible floating point number. ans=BinaryPeekFlt(bb,0) BinaryFree(bb) ;Display answer. Hope its 3.0 Message("And the answer is...",ans)

Article ID:   W14227
Filename:   Change a Single Floating Point to WB Floating.txt
File Created: 2006:03:07:13:11:32
Last Updated: 2006:03:07:13:11:32