I try to store one float
value through Buffer
in Node.js
> f = 3.3
3.3
> var buf = new Buffer(32)
> buf.writeFloatBE(f);
4
> g = buf.readFloatBE();
3.299999952316284
Then I found the stored value g
after readFloatBE()
is NOT equal to the original f
.
After further investigation, those two buffer values stored g
and f
are same.
> var buf1 = new Buffer(4); buf1.writeFloatBE(f); buf1
<Buffer 40 53 33 33>
> var buf2 = new Buffer(4); buf2.writeFloatBE(g); buf2
<Buffer 40 53 33 33>
According to this Buffer reading and writing floats, we know the writeDoulbeBE
should be used here.
> var buf3 = new Buffer(8);
> buf3.writeDoubleBE(f);
8
> h = buf3.readDoubleBE();
3.3
> h === f
true
I want to know why the float
type is not used in Node.js
or V8
? Refer to the code from V8
// Fast primitive setters
V8_INLINE void Set(bool value);
V8_INLINE void Set(double i);
V8_INLINE void Set(int32_t i);
V8_INLINE void Set(uint32_t i);
It seems there is NO float
type in V8
, any reason of this design or am I missing something? In which case, should this function writeFloatBE()
be used?