- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在 Go 中使用 math.Round()
将正值(这里的示例:1.015)四舍五入到小数点后 2 位:
fmt.Println(math.Round(1.015*100) / 100)
我得到:1.02
。没错。
但是当我使用一个函数来完成同样的工作时:
func RoundHalfUp(x float64) float64 {
return math.Round(x*100) / 100
}
我得到了 1.01
。
RoundHalfUp
函数有什么问题?
最佳答案
The Go Programming Language Specification
Numeric constants represent exact values of arbitrary precision and do not overflow.
Implementation restriction: Although numeric constants have arbitrary precision in the language, a compiler may implement them using an internal representation with limited precision. That said, every implementation must:
- Represent floating-point constants, including the parts of a complex constant, with a mantissa of at least 256 bits and a signed
binary exponent of at least 16 bits.- Round to the nearest representable constant if unable to represent a floating-point or complex constant due to limits on precision.
These requirements apply both to literal constants and to the result of evaluating constant expressions.
Constant expressions may contain only constant operands and are evaluated at compile time.
Constant expressions are always evaluated exactly; intermediate values and the constants themselves may require precision significantly larger than supported by any predeclared type in the language.
Implementation restriction: A compiler may use rounding while computing untyped floating-point or complex constant expressions; see the implementation restriction in the section on constants. This rounding may cause a floating-point constant expression to be invalid in an integer context, even if it would be integral when calculated using infinite precision, and vice versa.
像 Go 编译器对 math.Round(1.015*100)/100
那样实现 RoundHalfUp
函数。 1.015*100
是一个无类型的浮点常量表达式。使用精度至少为 256 位的 math/big
包。 Go float64
(IEEE-754 64 位 float )具有 53 位精度。
例如,256位精度(常量表达式),
package main
import (
"fmt"
"math"
"math/big"
)
func RoundHalfUp(x string) float64 {
// math.Round(x*100) / 100
xf, _, err := big.ParseFloat(x, 10, 256, big.ToNearestEven)
if err != nil {
panic(err)
}
xf100, _ := new(big.Float).Mul(xf, big.NewFloat(100)).Float64()
return math.Round(xf100) / float64(100)
}
func main() {
fmt.Println(RoundHalfUp("1.015"))
}
Playground :https://play.golang.org/p/uqtYwP4o22B
输出:
1.02
如果我们只使用 53 位精度(float64
):
xf, _, err := big.ParseFloat(x, 10, 53, big.ToNearestEven)
Playground :https://play.golang.org/p/ejz-wkuycaU
输出:
1.01
关于go - Go中四舍五入到小数点后2位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712958/
我有这个“科学应用程序”,其中 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'。对于给定的价格,我想退回等级。 这对于精确匹配来说已经足够简单了,但是我如何通过将我的输入值四舍五入到数组中的下一个
我是一名优秀的程序员,十分优秀!