Compute the double value nearest preferred decimal result

前端 未结 3 1210
渐次进展
渐次进展 2021-01-21 19:28

Let N(x) be the value of the decimal numeral with the fewest significant digits such that x is the double value nearest the value of the numeral.

Given

3条回答
  •  误落风尘
    2021-01-21 19:45

    You can convert to 'integers' by multiplying then dividing by a power of ten:

    (10*.3 - 10*.2)/10 == 0.1000000000000000055511151231257827021181583404541015625
    

    It may be possible to work out the appropriate power of ten from the string representation of the number. @PatriciaShanahan suggests looking for repeated 0's or 9's.

    Consider using a BigDecimal library such as javascript-bignum instead.

提交回复
热议问题