GO语言"math/big"包中"RoundingMode"类型的用法及代码示例。
RoundingMode 确定如何将浮点值四舍五入到所需的精度。舍入可能会改变 Float 值;舍入误差由浮点精度说明。
用法:
type RoundingMode byte
这些常量定义了支持的舍入模式。
const ( ToNearestEven RoundingMode = iota // == IEEE 754-2008 roundTiesToEven ToNearestAway // == IEEE 754-2008 roundTiesToAway ToZero // == IEEE 754-2008 roundTowardZero AwayFromZero // no IEEE 754-2008 equivalent ToNegativeInf // == IEEE 754-2008 roundTowardNegative ToPositiveInf // == IEEE 754-2008 roundTowardPositive )
例子:
package main
import (
"fmt"
"math/big"
)
func main() {
operands := []float64{2.6, 2.5, 2.1, -2.1, -2.5, -2.6}
fmt.Print(" x")
for mode := big.ToNearestEven; mode <= big.ToPositiveInf; mode++ {
fmt.Printf(" %s", mode)
}
fmt.Println()
for _, f64 := range operands {
fmt.Printf("%4g", f64)
for mode := big.ToNearestEven; mode <= big.ToPositiveInf; mode++ {
// sample operands above require 2 bits to represent mantissa
// set binary precision to 2 to round them to integer values
f := new(big.Float).SetPrec(2).SetMode(mode).SetFloat64(f64)
fmt.Printf(" %*g", len(mode.String()), f)
}
fmt.Println()
}
}
输出:
x ToNearestEven ToNearestAway ToZero AwayFromZero ToNegativeInf ToPositiveInf 2.6 3 3 2 3 2 3 2.5 2 3 2 3 2 3 2.1 2 2 2 3 2 3 -2.1 -2 -2 -2 -3 -3 -2 -2.5 -2 -3 -2 -3 -3 -2 -2.6 -3 -3 -2 -3 -3 -2
相关用法
- GO RoundToEven用法及代码示例
- GO Round用法及代码示例
- GO Rows用法及代码示例
- GO RotateLeft32用法及代码示例
- GO RotateLeft64用法及代码示例
- GO RotateLeft8用法及代码示例
- GO RotateLeft16用法及代码示例
- GO Regexp.FindString用法及代码示例
- GO Regexp.FindAllIndex用法及代码示例
- GO ResponseRecorder用法及代码示例
- GO ReverseBytes64用法及代码示例
- GO ReverseBytes16用法及代码示例
- GO Regexp.ReplaceAllLiteralString用法及代码示例
- GO Regexp.FindStringSubmatch用法及代码示例
- GO Regexp.FindAllString用法及代码示例
- GO ReadMessage用法及代码示例
- GO Regexp.ExpandString用法及代码示例
- GO ResponseWriter用法及代码示例
- GO Regexp.FindAllStringSubmatch用法及代码示例
- GO Reverse用法及代码示例
注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 RoundingMode。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。