Select to view content in your preferred language

Read float (single precision) from binary

442
2
11-09-2023 07:46 AM
AlfredBaldenweck
MVP Regular Contributor

Hi all, 

I'm trying to retrieve some float values from a blob field.

My workflow is get the blob field as text (hex) and parse it.

I can parse it no problem, but the issue is that the floats are returning what they're actually stored as, e.g. 1.2 is actually 1.2000000476837158203125, etc.
I can't round because each float has a different number of significant digits.

Weirdly enough, doubles parsed analogously come exactly as they should.

Does anyone have any ideas of how to get the correct values?

'''Float (single precision)'''
a = binVal[6:8] + binVal[4:6] + binVal[2:4] + binVal[0:2]
print(a)
# 3F99999A
domCd = struct.unpack('!f', bytes.fromhex(a))[0] 
print(domCd)
# 1.2000000476837158

'''Double (double precision)'''
a = (binVal[14:16] + binVal[12:14] + binVal[10:12] + binVal[8:10] +
     binVal[6:8] + binVal[4:6] + binVal[2:4] + binVal[0:2])
print(a)
# 3FF3333333333333
domCd = struct.unpack('!d', bytes.fromhex(a))[0] 
print(domCd)
# 1.2
0 Kudos
2 Replies
VinceAngelo
Esri Esteemed Contributor

It's not possible to represent 1.2000000000 in floating point representation. That's just how IEEE-754 works.

There's lots of  web sites that show this, but this is the one I used.

- V

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Yes, I understand that. 

However, the GUI that can read this field natively (read: arcmap) can pull the correct values, so how can I?

Also, as I understand it, neither can doubles, and yet...

0 Kudos