简体   繁体   English

在JavaScript中添加浮点数

[英]Addition of floating point numbers in JavaScript

I've read this Stack Overflow thread , and this blog post , but am still unable to understand why some floating point numbers can be represented exactly in binary, and why some can't. 我已经阅读了这个Stack Overflow线程这篇博客文章 ,但是仍然无法理解为什么有些浮点数可以用二进制精确地表示,而有些则不能。

I understand that 0.1 , 0.2 , 0.3 , 0.4 , 0.6 , 0.7 , 0.8 , 0.9 will give an infinite fraction in the binary system as their denominators, since neither 10 nor 5 can be represented with powers of two. 据我所知, 0.10.20.30.40.60.70.80.9会给无限分数在二进制系统作为其分母,因为无论10也不5可以与二的幂来表示。 0.5 however has a denominator of 2^1 . 但是0.5的分母为2^1

When I add 0.2 + 0.4 , I get 0.6000000000000001 , when I add 0.1 + 0.5 , I get 0.6 . 当我加上0.2 + 0.4 ,我得到0.6000000000000001 ,当我加上0.1 + 0.5 ,我得到0.6 I thought this was because, in the first sum, I was adding two infinite fractions, whereas, in the second sum, I was adding with 0.5 , which has a finite representation, with 0.1 , which doesn't. 我认为这是因为,在第一个和中,我添加了两个无穷小数,而在第二个和中,我添加了0.5 ,它具有有限的表示,而没有0.1 However, when I add 0.3 + 0.4 , I get 0.7 , which I didn't expect, considering both 0.3 and 0.4 do not have exact representations, and nor does 0.7 . 但是,当我加0.3 + 0.4 ,得到0.7 ,这是我没有想到的,因为0.30.4都没有精确的表示形式,而0.7也没有。

I would have thought that since 0.5 is the only decimal from 0.1 to 0.9 (at only one decimal place) with a finite representation, working with any other decimals would give a non-exact representation, but this is not the case. 我会认为,由于0.5是从0.10.9的唯一十进制数(仅在一个小数点后一位),并且具有有限的表示形式,因此与其他任何十进制小数一起使用都会给出不精确的表示形式,但是事实并非如此。

Why do adding some one point decimals with no finite representation in binary yield an exact representation and some not? 为什么在二进制中添加一些没有有限表示的小数点小数会得到精确的表示而有些却没有?

When converting Number values to strings in JavaScript, the default is to use just enough digits to uniquely distinguish the Number value . 在JavaScript中将Number值转换为字符串时,默认值是仅使用足够的数字来唯一区分Number This means that when a number is displayed as “0.6”, that does not mean it is exactly 0.6, just that it is closer to 0.6 than any other Number value is, so displaying just “0.6” tells you it is this unique Number value, which is 0.59999999999999997779553950749686919152736663818359375. 这意味着当一个数字显示为“ 0.6”时,并不意味着它正好是0.6,只是它比其他任何Number值都更接近0.6,因此仅显示“ 0.6”就表明它是这个唯一的Number值,即0.59999999999999997779553950749686919152736663818359375。

When you set Number objects to 0.2 and 0.4 , the results are actually 0.200000000000000011102230246251565404236316680908203125 and 0.40000000000000002220446049250313080847263336181640625. 当将Number对象设置为0.20.4 ,结果实际上是0.200000000000000011102230246251565404236316680908203125和0.40000000000000002220446049250313080847263336181640625。 When you add these, the result is 0.600000000000000088817841970012523233890533447265625. 当您添加这些时,结果为0.600000000000000088817841970012523233890533447265625。 That is different from 0.59999999999999997779553950749686919152736663818359375, and it is farther from 0.6, so JavaScript displays it as “0.6000000000000001” to show that it is different from the number it displays as “0.6”. 该值不同于0.599999999999999999997779553950749686919152736663818359375,并且距0.6较远,因此JavaScript将其显示为“ 0.6000000000000001”以表明它与显示为“ 0.6”的数字不同。

When you set Number objects to 0.1 and 0.5 , the results are 0.1000000000000000055511151231257827021181583404541015625 and 0.5. 将Number对象设置为0.10.5 ,结果为0.1000000000000000055511151231257827021181583404541015625和0.5。 When you add these, the result is 0.59999999999999997779553950749686919152736663818359375, which is that number that JavaScript displays as “0.6”. 当您添加这些时,结果为0.5999999999999999777955393950749686919152736663818359375,这是JavaScript显示为“ 0.6”的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM