Front end bug record – what is scientific counting?

Time:2021-3-26

Scientific counting is a method of counting.Features: precision loss, space saving

What is scientific counting

Express a number as a and aThe form of multiplication of n-th power of 10(1 ≤| a | < 10, a is not a fraction, n is an integer).19971400000000 == 1.99714×10^13 == 1.99714e13

When we want toTo mark or calculate a large or small number of digitsThe scientific counting method can save a lot of space and time.

The origin of the problem

In a group, someone asked me:The problem of keeping decimals. After some investigation, it was himThe scientific counting method was not dealt with

Front end bug record - what is scientific counting?

The range of integers that JavaScript can accurately represent is-2^53reach2^53Between (excluding two endpoints), beyond this range, can not accurately represent the integer.

Similarly, because of the storage specification, decimal also has accuracy problems.

https://www.ecma-international.org/ecma-262/#sec-ecmascript-language-types-number-type

JavascriptNumber 

JavascriptNumberObjects are encapsulated objects that allow you to process numeric values.NumberObject byNumber()Constructor creation.

JavascriptNumberType isDouble precision IEEE 754 64 bit floating pointType.

There’s something wrong recentlyBigIntAny precision number type, you can try it

Safe number

  • Number.MAX_SAFE_INTEGERThe largest safe integer in JavaScript(2**53 - 1)。
    Front end bug record - what is scientific counting?
  • Number.MAX_VALUEThe maximum positive number that can be represented in JavaScript. The smallest negative number is – max_ VALUE。
    Front end bug record - what is scientific counting?
  • Number.MIN_SAFE_INTEGERThe smallest security integer that can be represented in JavaScript (- (253 – 1)).
    Front end bug record - what is scientific counting?
  • Number.MIN_VALUEThe smallest positive number that can be represented in JavaScript is the positive number closest to 0 (it doesn’t actually become 0). The largest negative number is-MIN_VALUE
    Front end bug record - what is scientific counting?

code

function number2string(num){
    num = String(num)
    
    var eSplit = num.split('e')
    var e = +(eSplit[1] || 0)
    
    var pad = '0'.repeat(500)
    var numSplit = eSplit[0].split('.')
    
    var str
    var numSplit1 = numSplit[1] || ''
    if(e < 0){
        str = ('0.' + (pad + numSplit[0]).slice(e) + numSplit1)
    }else{
        str = (numSplit[0] + (numSplit1 + pad).slice(0,e) + '.' + numSplit1.slice(e))
    }
    return str.replace(/\.?0*$/,'').replace(/^0*/,'').replace(/^\./,'0.') || '0'
}
// number2string('1.23456789e-1')
// number2string('1.23456789e0')
// number2string('1.23456789e1')
arr = [];
for(var i = -20; i < 20; i++){
    arr.push(`1.23456789e${i}`)
    //arr.push(`0001.23456789000e${i}`)
    //arr.push(`00000.0000001e${i}`)
    //arr.push(`${i}`)
    //arr.push(`${.5 - Math.random()}`)
}
arr.forEach((v)=>{
    console.log(String(v).padEnd(30, ' '), number2string(v))
})

WeChat official account: front-end Linong

Front end bug record - what is scientific counting?

reference material

  1. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Number

Recommended Today

Paper case CMS 3.3.6 release upgrade. Net 5

.Net 5 Microsoft’s. Net 5 has been released and we adapted it for the first time. The biggest change in the new version is to upgrade to Microsoft’s latest. Net 5, and the performance has been significantly improved. So please update your running environment before upgrading to 3.3.6. Upgrade Guide:http://www.zkea.net/zkeacms/document/upgrade Convenient update Paper shell CMS […]