float80
v1.1.0
Published
Read 80-bit Extended Floating Point Numbers from buffers
Downloads
3,284
Readme
float80
Read 80-bit Extended Floating Point Numbers from buffers
It turns out that there does not appear to be any way to read the extended precision floating point format in Javascript, aka "long doubles".
Until now, anyway.
Get it
Get this library with
$ npm install float80
Usage
>>> const {Float80} = require('float80')
>>> f = Float80.fromBytes([0x3f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
>>> f.toString()
1
>>> f.asNumber()
<BigNumber: [String: 1]>
The number parsed by Float80 is stored in a BigNumber instance, and exposed by the asNumber
function.
Generating 80 bit floats
Here is a trivial C program that you can use to view how 80-bit floats are represented:
#include <stdio.h>
union {
long double value;
unsigned char bytes[10];
} dc;
int main(int argc, char ** argv) {
int i;
while(1) {
printf("Enter long double: ");
scanf("%Lf", &dc.value);
for (i = 0; i < 10; ++i) {
printf("%02x ", dc.bytes[9 - i]);
}
printf("\n");
}
}
Bonus: 48-bit floats.
The weird 48-bit float parsing is available in Float48
class.
Same API as Float80
.
License
MIT