乘时可以0.99999999999四舍五入到1.0吗? [英] Can 0.99999999999 be rounded to 1.0 when multiplying?

查看:274
本文介绍了乘时可以0.99999999999四舍五入到1.0吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在相乘,这是非常接近1用int> 0浮点数,可以将它永远不会PTED为1间$ P $

When multiplying a floating point number that is very close to 1 with an int > 0, can it ever be interpreted as 1.

也就是说,如果的Math.random()返回其最高可能的结果(这是1步低于1.0),将

That is, if Math.random() returns its highest possible result (which is 1 step below 1.0), will

(int)(Math.random() * 8)

8或7?

对于一个实际的例子,可以在此经常使用的结构给出的指标超出范围的错误:

For a practical example, can this often-used construct give an index out of bounds error:

someArray[(int)(Math.random() * someArray.length)];

我在回答对Java和ActionScript 3特别感兴趣,但我想他们都使用了浮点算法相同的规则,并回答任何平台将是有益的。

I'm specifically interested in answers for Java and ActionScript 3, but I suppose they all use the same rules for floating point arithmetic, and answers for any platform would be useful.

更新的:虽然我已经接受了一个答案,我还是AP preciate确认这个错不了在ActionScript 3,因为相关同事报告说,他看见了它去错一次是部分促使我问这个问题。

Update: Though I already accepted an answer, I'd still appreciate confirmation that this can't go wrong in ActionScript 3 either, since a colleague reporting that he saw it go wrong once is what partly prompted me to ask this question.

推荐答案

如果您 someInt 乘以小于1.0的最大价值(> 0),结果将永远是 someInt

If you multiply the greatest value below 1.0 with someInt (> 0), the result will never be someInt.

这可以详尽地测试了这样的整数:

This can be exhaustively tested for integers like this:

Double greatestLessThanOne = Double.longBitsToDouble(4607182418800017407L);

// Assert that greatestLessThanOne is indeed the largest double less than 1.
//assert 1.0 == greatestLessThanOne + Math.ulp(greatestLessThanOne);

for (int i = 1; i >= 0; i++)
    if ((int) (greatestLessThanOne * i) == i)
        System.out.println("Exception found: " + i);

该片段不产生输出。

The snippet produces no output.

(<一href="http://download.oracle.com/javase/6/docs/api/java/lang/Math.html#ulp%28double%29"><$c$c>Math.ulp返回给定的双人和双值下一个数值较大的断言从而确保了 greatestLessThanOne 之间的距离的确是最大的价值小于1.0。)

(Math.ulp returns the distance between the given double and the double value next larger in magnitude. The assertion thus ensures that greatestLessThanOne is indeed the greatest value less than 1.0.)

换句话说,你的行

Object element = elementArray[(int)(Math.random() * elementArray.length)];

绝不会产生一个ArrayIndexOutOfBoundsException。

will never give rise to an ArrayIndexOutOfBoundsException.

此外,根据马克狄金森评论过<一href="http://stackoverflow.com/questions/3037952/could-random-randint1-10-ever-return-11/3037976#3037976">here,这也成立了双倍增时。

Furthermore, according to Mark Dickinsons comment over here, this holds also when multiplying with a double.

使用IEEE 754浮点运算的舍入到最近的模式,你也可以说 X * Y&LT; y表示任何 X &LT; 1.0 及任何非微小的正。 (它可能会失败,如果为低于正常涨幅最小正正规数。)

With IEEE 754 floating-point arithmetic in round-to-nearest mode, you can show that x * y < y for any x < 1.0 and any non-tiny positive y. (It can fail if y is either subnormal or the smallest positive normal number.)

这篇关于乘时可以0.99999999999四舍五入到1.0吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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