在JavaScript中添加浮点数 [英] Addition of floating point numbers in JavaScript

查看:65
本文介绍了在JavaScript中添加浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读此堆栈溢出线程此博客文章 ,但仍然无法理解为什么有些浮点数可以准确地用二进制表示,为什么有些不能.

我知道0.10.20.30.40.60.70.80.9将在二进制系统中给出无穷小数作为它们的分母,因为10和5都不能用2的幂表示. 0.5但是分母为2^1.

添加0.2 + 0.4时得到0.6000000000000001,添加0.1 + 0.5时得到0.6.我认为这是因为,在第一个总和中,我添加了两个无穷小数,而在第二个总和中,我添加了具有有限表示法的0.5和没有的有限表示法.但是,当我添加0.3 + 0.4时,由于0.30.4都没有确切的表示,并且0.7也没有确切的表示,所以我得到了没想到的0.7.

我会认为,由于0.5是从0.10.9的唯一十进制数(仅在一个小数点后一位)且具有有限的表示形式,因此与任何其他十进制小数一起使用将给出非精确的表示形式,但是情况并非如此.

为什么在二进制中加一些没有有限表示的小数点小数会得到精确的表示而有些则没有呢?

解决方案

在JavaScript中将Number值转换为字符串时,默认值为仅使用足够的数字来唯一区分Number.这意味着当一个数字显示为"0.6"时,并不意味着它正好是0.6,只是它比其他任何Number值都更接近0.6,因此仅显示"0.6"就表明您是这个唯一的Number值,即0.59999999999999997779553950749686919152736663818359375.

将Number对象设置为0.20.4时,结果实际上是0.200000000000000011102230246251565404236316680908203125和0.40000000000000002220446049250313080847263336181640625.添加这些时,结果为0.600000000000000088817841941970012523233890533447265625.这与0.599999999999999999997779553950749686919152736663818359375有所不同,因此它离0.6更远,因此JavaScript将其显示为"0.6000000000000001",以表明它与显示为"0.6"的数字不同.

将Number对象设置为0.10.5时,结果为0.1000000000000000055511151231257827021181583404541015625和0.5.添加这些时,结果为0.5999999999999999777955393950749686919152736663818359375,这是JavaScript显示为"0.6"的数字.

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.

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.5 however has a denominator of 2^1.

When I add 0.2 + 0.4, I get 0.6000000000000001, when I add 0.1 + 0.5, I get 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. 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.

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.

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. 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.

When you set Number objects to 0.2 and 0.4, the results are actually 0.200000000000000011102230246251565404236316680908203125 and 0.40000000000000002220446049250313080847263336181640625. When you add these, the result is 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".

When you set Number objects to 0.1 and 0.5, the results are 0.1000000000000000055511151231257827021181583404541015625 and 0.5. When you add these, the result is 0.59999999999999997779553950749686919152736663818359375, which is that number that JavaScript displays as "0.6".

这篇关于在JavaScript中添加浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆