Float Problem
Closed this issue · 1 comments
huseyinkozan commented
Hi,
I encounter a float problem. Is this normal ?
Code :
var _ = require("struct-fu");
var ds = _.float32le();
var b = ds.pack(3.2);
var c = ds.unpack(b);
console.log("b:", b);
console.log("3.2:", c);
Output:
b: <Buffer cd cc 4c 40>
3.2: 3.200000047683716
Env :
- Node : 6.9.1
- OS : Ubuntu 16.04 x86_64
natevw commented
Sorry for dropping the ball on this, but yes this is normal. Floating point numbers encode in base-2, so even if a number is "round" in base-10 it may need to round to a nearby base-2 number that's not as clean when later converted back into base-10. Try this with plain buffers in node.js:
var b = Buffer(4);
void b.writeFloatLE(3.2);
b.readFloatLE();
…or take a look at the underlying representation by plugging 3.2
into the Decimal Representation field at https://www.h-schmidt.net/FloatConverter/IEEE754.html