Transfer floating point data from Python to JavaScript without loss of digits

Do you ever want to bring a 64-bit floating point number created in Python to JavaScript as it is? Both are IEEE-754 64-bit (sign 1 bit, mantissa 52 bits, exponent 11 bits).

I wish I could pass it as it is, but I'm not happy that if I dump it and put it out in decimal, it will be rounded.

This entry details how to pass a floating point number as is, without error.

In reality, JavaScript is defined by the ECMAScript specification, and Python is the same as C double, so as a result, it has the same format in most existing OS environments.

Export as a floating point string in Python

float64 is a byte array using struct.pack and then a hexadecimal string in format. It is issued in network byte order.

dump.py


>>> import numpy as np
>>> import struct
>>> import binascii
>>> a = np.float64(10.12345456)
>>> binascii.hexlify(struct.pack("!d", a))
'40243f356fa37bf1'

Although it is a little different from this explanation, you can also restore it as follows.

load.py


>>> b = struct.unpack('!d', binascii.unhexlify('40243f356fa37bf1'))[0]
>>> a == b
True

Load a byte representation string in JavaScript

Use the familiar TypedArray

>  a = new ArrayBuffer(8)
> b = new Float64Array(a)
> c = new Uint32Array(a)
> d = '40243f356fa37bf1'
> c[0] = parseInt(d.slice(8, 16), 16)
> c[1] = parseInt(d.slice(0, 8), 16)
> b[0]
10.12345456

Yes. I was able to read it. I tried it with Python 3.4 on Mac OS X 10.9 and node.js 0.12.2.

Recommended Posts

Transfer floating point data from Python to JavaScript without loss of digits
How to avoid duplication of data when inputting from Python to SQLite.
[Introduction to Data Scientists] Basics of Python ♬
[Python --open3d] How to convert obj data of 3D model to point cloud
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
[Python] How to read data from CIFAR-10 and CIFAR-100
Change the decimal point of logging from, to.
[Python] Flow from web scraping to data analysis
Python> tuple> (3., 1., 4.) to 3.00000, 1.00000, 4.00000 (decimal point to 5 digits)> Use .join
Example of application of Python to engineering: Finding parameters of approximate calculation formula from actual data
The wall of changing the Django service from Python 2.7 to Python 3
Summary of tools needed to analyze data in Python
How to get the number of digits in Python
How to scrape image data from flickr with python
How to convert floating point numbers to binary numbers in Python
Send data from Python to Processing via socket communication
DataNitro, implementation of function to read data from sheet
Understand the status of data loss --Python vs. R
[Basics of data science] Collecting data from RSS with python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
SIGNATE Quest ② From creation of targeting model to creation of submitted data
I want to start a lot of processes from python
[Python] Extract text data from XML data of 10GB or more.
Build a Python environment and transfer data to the server
The story of copying data from S3 to Google's TeamDrive
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Hit REST in Python to get data from New Relic
Meteorology x Python ~ From weather data acquisition to spectrum analysis ~
Pass OpenCV data from the original C ++ library to Python
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
Post from Python to Slack
Cheating from PHP to Python
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
[Python] Try to graph from the image of Ring Fit [OCR]
Introduction to Data Analysis with Python P17-P26 [ch02 1.usa.gov data from bit.ly]
Simple code to call a python program from Javascript on EC2
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
Various ways to create an array of numbers from 1 to 10 in Python.
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.