Javascript-轻微的划分错误 [英] Javascript - slight division error

查看:68
本文介绍了Javascript-轻微的划分错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
JavaScript数学错误:浮点数不正确

Possible Duplicate:
Javascript Math Error: Inexact Floats

我做到了

this.cues = []
for(var i, i = 1; i <= 8; i++){
    cue = this.length * (i/8.0)
   this.cues.push(cue)

其中this.length是双精度型.我检查了除法,发现直到第四个数组单元为止,我都偏离了.000002.

where this.length is a double. I checked the division and found that I was off by .000002 up to about the fourth array cell.

  1. 任何人都可以想到的特定于javascript的原因吗?可能只是环境,所以就算没什么大不了
  2. 如果可以,我可以解决这个问题吗?

非常感谢

推荐答案

您应阅读:"什么每个程序员都应该了解浮点运算" .这就是在计算机上如何表示浮点数.

You should read: "What Every Programmer Should Know About Floating-Point Arithmetic". This is simply how floating point numbers are represented on a computer.

这篇关于Javascript-轻微的划分错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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