- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关于这些 java.lang.Double 的东西真的很奇怪......这是 [Clojure] 代码 - 与 Java 相同,用于所有 [与执行相关的] 意图和目的:
(doseq [x (range 100)
:let [x33 (str (+ x 0.33))
x99 (str (+ x 0.99))
tx33 (* 100 (Double/parseDouble x33))
tx99 (* 100 (Double/parseDouble x99))]]
(do (prn (str x33 "->" tx33))
(prn (str x99 "->" tx99))))
对于 Java 人员 - 这会遍历 java.lang.Long-s 的范围,通过分别向其添加 0.33 和 0.99 为每个(x33 和 x99)创建两个 java.lang.String-s。它将 Strings 精简解析为 Doubles,并将结果乘以 100,然后打印映射。观察结果(最后的nil无关紧要):
"0.33->33.0"
"0.99->99.0"
"1.33->133.0"
"1.99->199.0"
"2.33->233.0"
"2.99->299.0"
"3.33->333.0"
"3.99->399.0"
"4.33->433.0"
"4.99->499.0"
"5.33->533.0"
"5.99->599.0"
"6.33->633.0"
"6.99->699.0"
"7.33->733.0"
"7.99->799.0"
"8.33->833.0"
"8.99->899.0"
"9.33->933.0"
"9.99->999.0"
"10.33->1033.0"
"10.99->1099.0"
"11.33->1133.0"
"11.99->1199.0"
"12.33->1233.0"
"12.99->1299.0"
"13.33->1333.0"
"13.99->1399.0"
"14.33->1433.0"
"14.99->1499.0"
"15.33->1533.0"
"15.99->1599.0"
"16.33->1632.9999999999998"
"16.99->1698.9999999999998"
"17.33->1732.9999999999998"
"17.99->1798.9999999999998"
"18.33->1832.9999999999998"
"18.99->1898.9999999999998"
"19.33->1932.9999999999998"
"19.99->1998.9999999999998"
"20.33->2032.9999999999998"
"20.99->2099.0"
"21.33->2133.0"
"21.99->2199.0"
"22.33->2233.0"
"22.99->2299.0"
"23.33->2333.0"
"23.99->2399.0"
"24.33->2433.0"
"24.99->2499.0"
"25.33->2533.0"
"25.99->2599.0"
"26.33->2633.0"
"26.99->2699.0"
"27.33->2733.0"
"27.99->2799.0"
"28.33->2833.0"
"28.99->2899.0"
"29.33->2933.0"
"29.99->2999.0"
"30.33->3033.0"
"30.99->3099.0"
"31.33->3133.0"
"31.99->3199.0"
"32.33->3233.0"
"32.99->3299.0"
"33.33->3333.0"
"33.99->3399.0"
"34.33->3433.0"
"34.99->3499.0"
"35.33->3533.0"
"35.99->3599.0"
"36.33->3633.0"
"36.99->3699.0"
"37.33->3733.0"
"37.99->3799.0"
"38.33->3833.0"
"38.99->3899.0"
"39.33->3933.0"
"39.99->3999.0"
"40.33->4033.0"
"40.99->4099.0"
"41.33->4133.0"
"41.99->4199.0"
"42.33->4233.0"
"42.99->4299.0"
"43.33->4333.0"
"43.99->4399.0"
"44.33->4433.0"
"44.99->4499.0"
"45.33->4533.0"
"45.99->4599.0"
"46.33->4633.0"
"46.99->4699.0"
"47.33->4733.0"
"47.99->4799.0"
"48.33->4833.0"
"48.99->4899.0"
"49.33->4933.0"
"49.99->4999.0"
"50.33->5033.0"
"50.99->5099.0"
"51.33->5133.0"
"51.99->5199.0"
"52.33->5233.0"
"52.99->5299.0"
"53.33->5333.0"
"53.99->5399.0"
"54.33->5433.0"
"54.99->5499.0"
"55.33->5533.0"
"55.99->5599.0"
"56.33->5633.0"
"56.99->5699.0"
"57.33->5733.0"
"57.99->5799.0"
"58.33->5833.0"
"58.99->5899.0"
"59.33->5933.0"
"59.99->5999.0"
"60.33->6033.0"
"60.99->6099.0"
"61.33->6133.0"
"61.99->6199.0"
"62.33->6233.0"
"62.99->6299.0"
"63.33->6333.0"
"63.99->6399.0"
"64.33->6433.0"
"64.99->6498.999999999999"
"65.33->6533.0"
"65.99->6598.999999999999"
"66.33->6633.0"
"66.99->6698.999999999999"
"67.33->6733.0"
"67.99->6798.999999999999"
"68.33->6833.0"
"68.99->6898.999999999999"
"69.33->6933.0"
"69.99->6998.999999999999"
"70.33->7033.0"
"70.99->7098.999999999999"
"71.33->7133.0"
"71.99->7198.999999999999"
"72.33->7233.0"
"72.99->7298.999999999999"
"73.33->7333.0"
"73.99->7398.999999999999"
"74.33->7433.0"
"74.99->7498.999999999999"
"75.33->7533.0"
"75.99->7598.999999999999"
"76.33->7633.0"
"76.99->7698.999999999999"
"77.33->7733.0"
"77.99->7798.999999999999"
"78.33->7833.0"
"78.99->7898.999999999999"
"79.33->7933.0"
"79.99->7998.999999999999"
"80.33->8033.0"
"80.99->8098.999999999999"
"81.33->8133.0"
"81.99->8199.0"
"82.33->8233.0"
"82.99->8299.0"
"83.33->8333.0"
"83.99->8399.0"
"84.33->8433.0"
"84.99->8499.0"
"85.33->8533.0"
"85.99->8599.0"
"86.33->8633.0"
"86.99->8699.0"
"87.33->8733.0"
"87.99->8799.0"
"88.33->8833.0"
"88.99->8899.0"
"89.33->8933.0"
"89.99->8999.0"
"90.33->9033.0"
"90.99->9099.0"
"91.33->9133.0"
"91.99->9199.0"
"92.33->9233.0"
"92.99->9299.0"
"93.33->9333.0"
"93.99->9399.0"
"94.33->9433.0"
"94.99->9499.0"
"95.33->9533.0"
"95.99->9599.0"
"96.33->9633.0"
"96.99->9699.0"
"97.33->9733.0"
"97.99->9799.0"
"98.33->9833.0"
"98.99->9899.0"
"99.33->9933.0"
"99.99->9999.0"
nil
如您所见,有两个这样的 double 集群,它们的行为与其他 double 不同。对我来说是个谜……天知道通往无限的道路上还有多少。任何人都可以阐明这一点吗?谢谢!我知道并非所有有理数都可以用 float 表示,但我认为这是另外一回事。
最佳答案
IEEE 754 二进制 float 应该被认为是在 binades 中组织的,间隔范围从 2 的幂到下一个,其中所有数字由相同的最小间隔 (ULP) 分隔。
"15.99->1599.0""16.33->1632.9999999999998"..."20.33->2032.9999999999998""20.99->2099.0"
The above cluster corresponds to a more-inaccurate-than-usual computation when the origin value is in the 16-32 binade and the result is in the 1024-2048 binade.
"63.99->6399.0""64.33->6433.0""64.99->6498.999999999999""65.33->6533.0""65.99->6598.999999999999""66.33->6633.0""66.99->6698.999999999999"..."80.33->8033.0""80.99->8098.999999999999""81.33->8133.0""81.99->8199.0"
For some reason, there is another cluster where one computation out of two is more inaccurate than usual, when the origin value is in the 64-128 binade and the result in the 4096-8192 binade.
You can observe what happens more precisely in each of these two clusters by looking at the computations at the binary level. Computations for values where the input value and the output value are respectively in the same binades will look very similar (and that's how they are either all accurate or all inaccurate at the same time).
The C99 program below makes the computation appear as regular as it is, within the first cluster (this program should be trivial to translate to Clojure, as long as you can make use of Java's printing functions and of the %a
format for hexadecimal):
#include <stdio.h>
int main(int argc, char** argv)
{
printf("%a -> %.13a (exact result %a)\n", 15.33, 15.33 * 100.0, 1533.0);
printf("%a -> %.13a (exact result %a)\n", 16.33, 16.33 * 100.0, 1633.0);
printf("%a -> %.13a (exact result %a)\n", 17.33, 17.33 * 100.0, 1733.0);
printf("%a -> %.13a (exact result %a)\n", 18.33, 18.33 * 100.0, 1833.0);
printf("...\n");
printf("%a -> %.13a (exact result %a)\n", 20.33, 20.33 * 100.0, 2033.0);
printf("%a -> %.13a (exact result %a)\n", 21.33, 21.33 * 100.0, 2133.0);
}
输出是:
0x1.ea8f5c28f5c29p+3 -> 0x1.7f40000000000p+10 (exact result 0x1.7f4p+10)0x1.0547ae147ae14p+4 -> 0x1.983ffffffffffp+10 (exact result 0x1.984p+10)0x1.1547ae147ae14p+4 -> 0x1.b13ffffffffffp+10 (exact result 0x1.b14p+10)0x1.2547ae147ae14p+4 -> 0x1.ca3ffffffffffp+10 (exact result 0x1.ca4p+10)...0x1.4547ae147ae14p+4 -> 0x1.fc3ffffffffffp+10 (exact result 0x1.fc4p+10)0x1.5547ae147ae14p+4 -> 0x1.0aa0000000000p+11 (exact result 0x1.0aap+11)
上面,一个数字的十六进制表示中的 p+4
指数表示该数字在 16-32 二进制中。如您所见,此 binade 中的所有 X.33 数字都具有相似的表示形式。对它们的计算会得出相似的精确结果,但当结果低于 2048 时,它必须与精确结果高于 2048 时不同地四舍五入。
将以 ...ae147ae14
结尾的数字乘以 100 会产生以 ...fffffffd
结尾的精确结果,或二进制形式的 ...11111111101
。
当乘以 100 的数为16.33
时,需要截去最后两位二进制数进行四舍五入。计算的确切结果以二进制 …11111111101
结尾,与前一个可表示数比下一个可表示数更接近,因此乘法结果四舍五入为以 …二进制为 111111111
,或十六进制为 …fff
(或十进制为 …99998
)。
当乘以100的数是21.33
时,因为这次乘法的结果落在下一个二进制数中,所以最后三个二进制数需要被切断四舍五入。被截断的最后三位二进制数字是 101
。它们代表超过一半的 ULP,因此最接近的可表示值是高于精确值的可表示值。该值由 FPU 选择作为乘法结果,以 …00000
结尾,恰好是 2133.0
的浮点表示。
关于java - 关于乘法的奇怪的 java.lang.Double 簇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25630224/
我有这种来自 Google map 自动完成的奇怪行为(或者我可能错过了某事)...想法?奇怪的: 您在输入中输入某物,例如“伦敦” 您按 [ENTER] 你按下 [CLEAR] 按钮 你点击进入'输
这段代码与《Learning Java》(Oracle Press Books)一书中的代码完全一样,但它不起作用。我不明白为什么它不起作用,它应该起作用。我用 OpenJDK 和 Sun JDK 7
示例 1 中究竟发生了什么?这是如何解析的? # doesnt split on , [String]::Join(",",("aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aa
我需要获得方程式系统的解决方案。为此,我使用函数sgesv_()。 一切都很好,它使我感到解决方案的正确结果。 但是我得到一个奇怪的警告。 警告:从不兼容的指针类型传递'sgesv_'的参数3 我正在
我目前在制作动画时遇到一个奇怪的问题: [UIView animateWithDuration:3 delay:0
alert('works'); $(window).load(function () { alert('does not work'); });
我的代码: public class MyTest { public class StringSorter implements Comparator { public
我正在学习 JavaScript。尝试理解代码, function foo (){ var a = b = {name: 'Hai'}; document.write(a.name +''
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
这按预期工作: [dgorur@ted ~]$ env -i env [dgorur@ted ~]$ 这样做: [dgorur@ted ~]$ env -i which date which: no
struct BLA { int size_; int size()const{ return size_; } } int x; BLA b[ 2 ]; BLA * p = &b[
我有以下代码: #test img {vertical-align: middle;} div#test { border: 1px solid green; height: 150px; li
我想大多数使用过 C/C++ 的人都对预处理器的工作原理有一定的直觉(或多或少)。直到今天我也是这么认为的,但事实证明我的直觉是错误的。故事是这样的: 今天我尝试了一些东西,但我无法解释结果。首先考虑
我想为 TnSettings 做 mock,是的,如果通过以下方法编写代码,它就可以工作,问题是我们需要为每个案例编写 mock 代码,如果我们只 mock 一次然后执行多个案例,那么第二个将报告异常
我的项目中有以下两个结构 typedef volatile struct { unsigned char rx_buf[MAX_UART_BUF]; //Input buffer over U
Regex rx = new Regex(@"[+-]"); string[] substrings = rx.Split(expression); expression = "-9a3dcb
我的两个应用程序遇到了一个奇怪的问题。这是设置: 两个 tomcat/java 应用程序,在同一个网络中运行,连接到相同的 MS-SQL-Server。一个应用程序,恰好按顺序位于 DMZ 中可从互联
我目前正在与 Android Api Lvl 8 上的 OnLongClickListener 作斗争。 拿这段代码: this.webView.setOnLongClickListener(new
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
只是遇到了奇怪的事情。我有以下代码: -(void)ImageDownloadCompleat { [self performSelectorOnMainThread:@selector(up
我是一名优秀的程序员,十分优秀!