当前位置:网站首页>Precision loss problem
Precision loss problem
2022-06-23 02:34:00 【Programming samadhi】
background
- BFF Client The use of npm package
request-promise-nativeRequest the microservice interface to return ID Precision loss
1713166949059674112 => 1713166949059674000
Why is it lost ?
- When storing binary, the maximum offset of the decimal point is 52 position , What the computer stores is binary , The binary that can be stored is 62 position , If it exceeds, there will be rounding operation , therefore JS The largest integer that can be represented accurately in is Math.pow(2, 53), The decimal system is 9007199254740992 Greater than 9007199254740992 May lose accuracy
- Reference resources :https://zhuanlan.zhihu.com/p/100353781
request-promise-nativeWhen making a request , Whenoptions.jsonNot forfalseWill useJSON.parseanalysis body
if (self._json) {
try {
response.body = JSON.parse(response.body, self._jsonReviver)
} catch (e) {
debug('invalid JSON received', self.uri.href)
}
}Minimum demo
Build services API
One 、 build Java Web Api:
- Reference resources :Building a RESTful Web Service
- modify service Layer make id The minimum value is greater than js Precision limits
public long getId() {
return id + 1713166949059674112L;
}
* modify controller Layer add post request
@PostMapping("/greeting_create")
public Greeting createGreeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}Two 、 request
- GET request :
curl http://localhost:8080/greeting - POST request :
curl -X POST http://localhost:8080/greeting_create
{"id":1713166949059674120,"content":"Hello, World!"}Solution
1. Get the string of the response body , Use JSONbig take id Convert to string
- advantage : Only the current request is affected
- shortcoming : I won't support it POST Request mode ,
- adopt json Parameter transfer is not supported
- adopt form + json: false Back end interface support is required for parameter transmission
- GET request
const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');
const getOptions = {
'method': 'GET',
json: false,
'url': 'http://localhost:8080/greeting',
};
const getRes = await rp(getOptions);
console.log('get result: ', jsonBigInt.parse(getRes));- POST request : I won't support it ,json occupied , It will be carried out
JSON.parse
const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');
const postOptions = {
'method': 'POST',
'url': 'http://localhost:8080/greeting_create',
json: { name: 'test' },
};
const postRes = await rp(postOptions);
console.log('post result: ', jsonBigInt.parse(postRes));2. Use JSONbig.parse() Replace JSON.parse()
- advantage : Implement a simple , Support POST
- shortcoming : Affect all JSON.parse() analysis
const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');
async function jsonBigReplaceParse() {
const oldParse = JSON.parse;
JSON.parse = jsonBigInt.parse;
const postOptions = {
'method': 'POST',
'url': 'http://localhost:8080/greeting_create',
json: { name: 'test' },
};
const postRes = await rp(postOptions);
console.log('post result: ', postRes);
JSON.parse = oldParse;
}~ The end of this paper , Thank you for reading !
边栏推荐
- Aikuai multi dialing + load balancing overlay bandwidth
- Log a log4j2 vulnerability handling
- Apache Druid's engineering practice in shopee
- CSDN browser assistant for online translation, calculation, learning and removal of all advertisements
- Pywebio to quickly build web applications
- Unity official case nightmare shooter development summary < I > realization of the role's attack function
- Easygbs adds websocket message push, which can quickly locate video playback faults
- Ansible practice of Nepal graph
- Reinforcement learning series (III) -gym introduction and examples
- Evolution history of mobile communication
猜你喜欢

You must know the type and method of urllib

Application and challenge of ten billion level map data in Kwai security intelligence

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

Nebula operator cloud practice

Lying in the trough, write it yourself if you can't grab it. Use code to realize a Bing Dwen Dwen. It's so beautiful

Small knowledge points of asset

pd. read_ CSV and np Differences between loadtext

JS to realize the rotation chart (riding light). Pictures can be switched left and right. Moving the mouse will stop the rotation

5g spectrum

Pywebio to quickly build web applications
随机推荐
6. template for integer and real number dichotomy
The practice of traffic and data isolation in vivo Reviews
[CodeWars] Convert Decimal Degrees to Degrees, Minutes, Seconds
Biological psychiatry: defining individualized functional neuroanatomy for precision psychiatry
The commercial s2b2b e-commerce platform of aquatic industry improves the competitiveness of enterprises and creates a strong engine for industrial development
Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027
Mongodb aggregate query implements multi table associated query, type conversion, and returns specified parameters.
Capture passwords of all chrome versions
Deep analysis of time complexity
Cmake configuration error, error configuration process, Preject files may be invalid
The difference between script in head and body
Analysis of resolv Conf common parameters
How to generate DataMatrix code in batch through TXT file
Push RTMP stream using ffmpeg
Reptile lesson 1
Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
Error in OpenCV image operation: error: (-215:assertion failed)_ src. empty() in function ‘cv::cvtColor‘
Salesforce fileUpload (I) how to configure the file upload function
Pychart installation instructions
method