- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
编辑:我在调试 session 期间犯了一个错误,导致我提出这个问题。我看到的差异实际上在于打印 double 和解析 double (strtod
)。即使在这次整改之后,斯蒂芬的回答仍然很好地涵盖了我的问题,所以我想我会单独留下这个问题,以防它对某人有用。
我有权访问的一些(大多数)C 编译平台在以下情况下不考虑 FPU 舍入模式
double
;double
。这里没有什么奇特的东西:Mac OS X Leopard、各种最新的 Linux 和 BSD 变体、Windows。
另一方面,Mac OS X Snow Leopard 在做这两件事时似乎考虑了舍入模式。当然,不同的行为让我无休止地烦恼。
以下是这两种情况的典型片段:
#if defined(__OpenBSD__) || defined(__NetBSD__)
# include <ieeefp.h>
# define FE_UPWARD FP_RP
# define fesetround(RM) fpsetround(RM)
#else
# include <fenv.h>
#endif
#include <float.h>
#include <math.h>
fesetround(FE_UPWARD);
...
double f;
long long b = 2000000001;
b = b*b;
f = b;
...
printf("%f\n", 0.1);
我的问题是:
关于 2。我在标准中找到了一个地方,据说转换为整数的 float 总是被截断(向零四舍五入),但我找不到整数 -> 浮点方向的任何内容。
最佳答案
如果没有设置舍入模式,应该是IEEE-754默认的模式,即round-to-nearest。
对于从整数到 float 的转换,C 标准规定 (§6.3.1.4):
When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined.
所以这两种行为都符合C标准。
C 标准规定 (§F.5) IEC60559 浮点格式和字符序列之间的转换应根据 IEEE-754 标准正确舍入。对于非 IEC60559 格式,建议这样做,但不是必需的。 1985 IEEE-754 标准说(第 5.4 条):
Conversions shall be correctly rounded as specified in Section 4 for operands lying within the ranges specified in Table 3. Otherwise, for rounding to nearest, the error in the converted result shall not exceed by more than 0.47 units in the destination's least significant digit the error that is incurred by the rounding specifications of Section 4, provided that exponent over/underflow does not occur. In the directed rounding modes the error shall have the correct sign and shall not exceed 1.47 units in the last place.
第 (4) 节实际上说的是操作应根据流行的舍入模式进行。 IE。如果你改变舍入模式,IEEE-754 说 float->string 转换的结果应该相应地改变。整数-> float 转换也是如此。
IEEE-754 标准的 2008 年修订版说(第 4.3 条):
The rounding-direction attribute affects all computational operations that might be inexact. Inexact numeric floating-point results always have the same sign as the unrounded result.
这两种转换在第 5 条中都被定义为计算操作,因此它们应该再次根据流行的舍入模式执行。
我认为 Snow Leopard 在这里具有正确的行为(假设它正确根据普遍的舍入模式舍入结果)。如果您想强制执行旧行为,我想您总是可以将 printf
调用包装在更改舍入模式的代码中,尽管这显然不理想。
或者,您可以在 C99 兼容平台上使用 %a
格式说明符(十六进制 float )。由于此转换的结果始终是精确的,因此它永远不会受到流行的舍入模式的影响。我认为 Windows C 库不支持 %a
,但如果需要,您可以很容易地移植 BSD 或 glibc 实现。
关于在打印、转换中考虑 FPU 舍入模式的编译平台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2595563/
我在 ASP.NET MVC 中有一个发票页面。 我正在计算 GST。 在 javascript 中这是结果。 165.45 * 0.1 = 16.544999999999998 在 C# 中我得到
在 JavaScript 中,我想将其减少到小数点后 5 位。不过,我不能简单地四舍五入并得到 0.03085,我需要进一步查看数字并将所有数字向上舍入,以便得到 0.03086。 输入:0.0308
有人可以解释为什么 R 这样做吗?在整数值上舍入最大值和最小值似乎非常有缺陷。 summary(1:1283932) Min. 1st Qu. Median Mean 3rd Qu.
所以基本上我正在做一个物理实验,在我的表格中,我希望我的数据四舍五入到与误差相同的精度,四舍五入为 1 sig fig。 例如,如果我有以下内容: angle signif(c(1.111,2.22
考虑以下 C# 代码... double x = Math.Round(72.6d, 2, MidpointRounding.ToZero); double y = Math.Round(82.6d,
我正在学习 BigDecimal,我希望它检索我输入的确切数字,以下代码正在处理该数字,我不知道为什么 public static BigDecimal parseFromNumberString(S
double y1 = 0; double y2 = 0; double i = 0.025; double n = 2; double h1 = 2000; double h2 = 4000
所以在下面的一组代码中,出于某种原因我得到了完全错误的答案...... import java.util.*; import java.io.*; import java.lang.*; import
在 Python 中,我想将两个数字相除,如果答案不是整数,我希望将数字四舍五入为上面的数字。 例如 100/30 不是给 33.3 而是给 4。谁能建议如何做到这一点?谢谢。 最佳答案 您可以使用
我需要对一个 float 进行四舍五入。例如 4.00011 。内置函数 round() 总是在数字 > .5 时向上舍入,在 = 0 val *= 10 ** precision r
我的代码: // Convert SATOSHIS to BITCOIN static double SATOSHI2BTC(const uint64_t& value) {
我想让我的 tableView 看起来像这样: 我有问题。只有在我点击单元格后,我的右角才会变圆。当 View 出现时,它看起来像这样: 点击后像这样: 这是我的代码: extension UITab
这个问题在这里已经有了答案: Precision String Format Specifier In Swift (31 个答案) 关闭 8 年前。 除了覆盖当前转换为字符串的方法之外,是否有一种
>>> a = 0.3135 >>> print("%.3f" % a) 0.314 >>> a = 0.3125 >>> print("%.3f" % a) 0.312 >>> 我期待 0.313
我有自动将输入字段加在一起的 javascript 函数,但是添加像 1.35 + 1.35 + 1.35 这样的数字会得到 4.050000000000001 的输出,这只是一个例子。如何将总数四舍
这可能是 x86 FPU 专家的问题: 我正在尝试编写一个生成范围 [min,max] 内的随机浮点值的函数。问题是我的生成器算法(浮点 Mersenne Twister,如果你好奇的话)只返回 [1
我一定错过了一些明显的东西。 select CEILING(85/30) = 2 85/30 = 2.83333 我希望该值为 3。 CEILING 函数不应该为我取整吗? 最佳答案 尝试 SELEC
我有一个关于 eclipse rcp 中的 ctabfolders 的问题。我创建了一个 e4 RCP 应用程序,其中包含一个包含堆栈部分容器的窗口,其中包含一个堆栈。该堆栈包含 1 个部分。在这一部
Closed. This question needs details or clarity。它当前不接受答案。
我读过其他一些帖子,它们似乎对其他人有用,但当我尝试它们时,它们不起作用。我刚刚开始学习Java编程,我似乎不明白如何四舍五入。 我试过了 answer = input * input; answer
我是一名优秀的程序员,十分优秀!