Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to read a 32bit float from disk
Message
De
07/07/2004 08:29:27
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00569467
Message ID:
00921337
Vues:
14
>Dragan,
>Sergey passed this on to me. I need to do 4 byte floating point. You mention below that it can be done with some "tweaking". Well, what do I tweak?

The principle should be the same, but since you need 32 bits and not 64, it would have different sizes, and maybe a different offset for the exponent. I assume a there'd one byte of exponent and 3 bytes of mantissa. This function returns a numeric value for VFP's B format of 64-bit floating point number when passed as a string.

Since I didn't translate the variable names, here's their meaning:
bajt - byte
znak - sign
eksp(onent) - exponent
rez - res(ult)

I don't know the exact format of 32 bit floats - and I've seen a number of variations. The exponent may be offset by 128 (i.e. 128 would stand for zero, 129 for 1, 126 for -2 etc) or not; the bit used for the sign of the number itself may be in a different position etc. While working on this, it would help if you could write the numbers you want into strings (or a binary file) and then check whether your function returns proper values. Since I was doing this for Fox fields, it was no problem creating the file - I simply created a dbf with a B type field and filled it with numbers of my choice, repeating the same numbers in the next column as a string, so I could check my results.

>>This converts VFP's B format, which is 64-bit float. Tweaking it you can get the 32 bit version:
>>
>>
Function bton
>>*+ douBle string --> numeric
>>* takes a 8 byte string with IEEE representation of a long real
>>* structure:
>>* 7. byte:  7 bit sign
>>*	6-0 bit exponent upper
>>* 6. byte, bits 7-4 exponent lower. Exponent has 0x400 added
>>*    bits 3-0 mantissa. Mantissa is normalised, ie. assume 1 for first bit
>>* bytes 5-0 rest of mantissa
>>* returns a number.
>>*-
>>Lpara _s
>>Dime bajt(8)
>>For j=1 to 8
>>	bajt[j]=asc(subs(_s,j))
>>Endf
>>znak=iif(bittest(bajt[8], 7), -1, 1)
>>eksp=bitand(bajt[8],127)*16+bitrshift(bajt[7], 4)
>>*eksp=bitand(eksp+1025, 1023)
>>eksp=eksp-1023
>>mant=1+bitand(bajt[7], 15)/16
>>dv=4096
>>For j=6 to 1 step -1
>>	mant=mant +bajt(j)/dv
>>	dv=dv*256
>>Endf
>>rez=mant*2**eksp*znak
>>Retu rez

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform