- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
当一个非常接近 1 的 float 乘以 int > 0 时,它是否可以被解释为 1。
也就是说,如果 Math.random()
返回其可能的最高结果(比 1.0 低 1 步),将
(int)(Math.random() * 8)
是 8 还是 7?
举个实际的例子,这个经常使用的构造能否给出索引越界错误:
someArray[(int)(Math.random() * someArray.length)];
我对 Java 和 ActionScript 3 的答案特别感兴趣,但我想它们都使用相同的浮点运算规则,并且任何平台的答案都会很有用。
更新:虽然我已经接受了答案,但我仍然希望确认这在 ActionScript 3 中也不会出错,因为一位同事报告说他曾经看到它出错了部分促使我提出这个问题。
最佳答案
如果将低于 1.0 的最大值乘以 someInt
(> 0),结果永远不会是someInt
.
这可以针对这样的整数进行详尽的测试:
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);
片段不产生任何输出。
( Math.ulp
返回给定 double 值和下一个更大的 double 值之间的距离。因此,断言确保 greatestLessThanOne
确实是小于 1.0 的最大值。)
换句话说,你的线
Object element = elementArray[(int)(Math.random() * elementArray.length)];
永远不会引发 ArrayIndexOutOfBoundsException。
此外,根据 Mark Dickinsons 对 here 的评论, 这在与 double 相乘时也成立。
With IEEE 754 floating-point arithmetic in round-to-nearest mode, you can show that
x * y <
y for anyx < 1.0
and any non-tiny positivey
. (It can fail ify
is either subnormal or the smallest positive normal number.)
关于java - 0.99999999999相乘时可以四舍五入到1.0吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7723746/
我有这个“科学应用程序”,其中 Single 值在显示在 UI 中之前应该四舍五入。根据this MSDN article ,由于“精度损失”,Math.Round(Double, Int32) 方法
这个问题类似于Chrome 37 calc rounding 但实际问题有点复杂,提供的解决方案不适用于这种情况: #outerDiv, #leftDiv, #middleDiv, #rightDiv
假设有一堆从 pnorm() 返回的数据,这样您就有了 .0003ish 和 .9999ish 之间的数字。 numbers <- round(rnorm(n = 10000, mean =
我想有效地将unsigneda整数除以2的任意幂,然后取整。所以我在数学上想要的是ceiling(p/q)0。在C语言中,不利用q受限域的Strawman实现可能类似于以下function1: /
我正在尝试获取 #value_box 的值以显示 100.5 但它一直在向上舍入。有谁知道我可以做些什么来让它显示小数位? jsfiddle //returns 101 $("#value_box
我有一段 JavaScript 代码 shipingcostnumber * parseInt(tax) / 100 + shipingcostnumber 返回数字为6655.866558,因此我将
我有一个关于 PostgreSQL 9.2 中 float 的新手问题。 是否有直接舍入 float 的函数,即不必先将数字转换为数字类型? 另外,我想知道是否有一个函数可以按任意度量单位舍入,例如最
这个问题已经有答案了: Rounding to nearest 100 (7 个回答) 已关闭10 年前。 我正在尝试将数字四舍五入到 100。 示例: 1340 should become 1400
我试图找出使用整数存储在列表中的其他两个数字之间的任何n在整数列表中找到最接近的值ROUNDED DOWN的最佳方法。在这种情况下,所有整数都将始终是无符号的,以防万一。 假设如下: 列表始终从0开始
我想将一个 BigDecimal 四舍五入到小数点后两位,但是当使用 round 方法时,它似乎没有双舍入: BigDecimal.new('43382.0249').round(2).to_s('F
我正在使用格式如下的财务数据进行计算: . 基本上,在我的程序中我遇到了一个浮点错误。例如,如果我有: 11.09 - (11.09 * 0.005) = 11.03455 我希望能够使用 11.03
有整型变量,电压单位为毫伏。 signed int voltage_mv = 134; //134mV 我有 2 段显示,我想显示百分之一伏特。 如何在一次操作中将毫伏转换为百分之一伏?没有 IF 语
这是我将数字四舍五入到两位小数的函数,但是当四舍五入的数字为 1.50 时,它似乎忽略尾随零并只返回 1.5 public static double roundOff(double number)
您好,我在将数字四舍五入到 -0 而不是 0 时遇到了问题 代码: 输出:-0 预期输出:0 我一直在寻找任何解决方案,但没有找到。 请解释并帮助我为什么它四舍五入为 -0 而不是 0?谢谢 最佳答
我正在使用 Java 的 Random 生成随机数:1.0、1.1 - 10 Random random = new Random(); return (double) ((random.nextIn
基本上,我有一个数字: 我基本上想做一些数学运算来创建这个数字 80。 如果数字是 62.7777777778,则数字将为 60。 我希望数字像这样四舍五入: 20, 40, 60, 80, 100
我希望显示来自 NSDate 对象的月数。 //Make Date Six Months In The Future NSCalendar *calendar = [[NSCalendar alloc
下面是一些小代码来说明我所看到的 float floater = 59.999f; DecimalFormat df = new DecimalFormat("00.0"); System.out.p
我现在开始使用 android 和 java,但遇到了问题。 I have a result x = 206.0548. And y = 206, both of type double How do
我有一个 ruby 散列数组,其中包含两个键,'tier' 和 'price'。对于给定的价格,我想退回等级。 这对于精确匹配来说已经足够简单了,但是我如何通过将我的输入值四舍五入到数组中的下一个
我是一名优秀的程序员,十分优秀!