2019-05-14 15:03

# Golang big.Float的精度问题

I've met some interesting problem of Golang big.Float calculation.

The Problem is

10001000100010001000100010001000100010001000100015.5533 / 1000000000000000000

= 10001000100010001000100010001000.1000100010001000155533

However, big.Float gave "10001000100010001000100010001000.10001000100010001555329999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997"

The code:

``````var prec uint = 1024 // 512
dec, _ := new(big.Float).SetPrec(prec).SetString("1000000000000000000")
f, _ := new(big.Float).SetPrec(prec).SetString("10001000100010001000100010001000100010001000100015.5533")
q := f.Quo(f, dec)

fmt.Printf("Percision: %d
", prec)
fmt.Printf("Quotient: %s
", q.Text('f', -1))
``````

Result:

``````Percision: 1024
Quotient: 10001000100010001000100010001000.10001000100010001555329999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997
``````

And the more confusing part is, if I set prec = 512, a smaller precision, it produced correct result instead.

``````Percision: 512
Quotient: 10001000100010001000100010001000.1000100010001000155533
``````

Doses any one know what's wrong of my code or how to configure big.Float to get expected result?

Thanks to all!

#### 图片转代码服务由CSDN问答提供 功能建议

10001000100010001000100010001000100010001000100015.5533 / 1000000000000000000

= 10001000100010001000100010001000.1000100010001000155533

``` ``` var prec uint = 1024 // 512
dec，  _：= new（big.Float）.SetPrec（prec）.SetString（“ 1000000000000000000”）
f，_：= new（big.Float）.SetPrec（prec）.SetString（“ 100010001000100010001000100010001000100010001000100015.5533”）
q：= f  .Quo（f，dec）

fmt.Printf（“ Percision：％d
”，prec）
fmt.Pri  ntf（“商：％s
”，q.Text（'f'，-1））

结果：

精度：1024

精度：512

任何人都知道我的问题是什么 代码或如何配置big.Float以获得预期的结果？

感谢所有人！
``````
` `
``` 写回答 好问题 提建议 追加酬金 关注问题 分享 邀请回答 ```
``` 1条回答 默认 最新 dragon4587 2019-05-14 15:49 最佳回答 From go doc math/big.Float: A nonzero finite Float represents a multi-precision floating point number sign × mantissa × 2**exponent with 0.5 <= mantissa < 1.0, and MinExp <= exponent <= MaxExp. And SetPrec sets the bitwidth of the mantissa not some decimal precision. Like with float64s not every decimal number can be represented exact in a big.Float and your code shows this. The fact that you see what you expect to see with prec=512 is due to different rounding and printing. Rule of thumb: big.Floats behave like "normal" floats with all their shortcomings (here not every decimal fraction can be represented) but may show less rounding errors. 评论 解决 无用 打赏 分享 举报 报告相同问题？ 提交 关注问题 相关推荐 更多相似问题 Golang big.Float的精度问题 2019-05-14 15:03 回答 1 已采纳 From go doc math/big.Float: A nonzero finite Float represents a multi-precision floating point 如何使用Golang big.Int计算`x ^ 3 + ax + b mod p` 2019-01-05 19:04 回答 1 已采纳 I figured it out, by scouting some code in the elliptic lib func GetY(x *big.Int, curve *elliptic 将JSON解组为Golang类型Big.Float json 2016-05-01 03:35 回答 1 已采纳 It appears to me (based on the docs) that it expects the json for big.Float to be passed in as str golang big.Float与big.Int处理精度问题 2021-10-26 19:12 码出新天地的博客 big.Int(error) 入参数10 func Atto2BaseCoin(atto *big.Int) *big.Int { i := big.NewInt(0) ...func Atto2BaseCoin(atto *big.Int) *big.Float { f := new(big.Float).SetInt(atto) fSub := new(big.Float).S golang--基本数据类型,big.Int类型,类型转换 2020-07-30 15:03 宋song一的博客 1：前面的big.Int 实例小于cmp方法big.Int 参数 分支语句时,注意后面的分支不要再使用:推导 四、数据处理 不使用科学计数法输出， sprintf := fmt.Sprintf("%.18f", float) f前面的数字为你决定要保留多少位小数. Golang中的精度丢失问题及解决方案 2022-03-01 17:05 吃葡萄王者的博客 Golang中的精度丢失问题及解决方案 声明类型* big.Int溢出常量golang 2016-05-28 17:50 回答 2 已采纳 The problem is that Go interprets your literal value as an int. But, it does not really fit into 3 golang null.String解码无法正常工作 sql 2019-08-27 06:56 回答 1 已采纳 If you want to treat empty strings as nulls you have at least two options. "Extend" null.String: 使用Golang生成特定范围内的随机float64数字 2018-04-10 06:38 回答 1 已采纳 Simply use rand.Float64() to get a random number in the range of [0..1), and you can map (project) golang float浮点型精度丢失问题解决办法：使用decimal包；float与int的相互转换 2021-02-10 14:39 学亮编程手记的博客 golang中big包源码阅读——从RSA算法说起 2018-11-26 14:45 weixin_33970449的博客 ●1 Golang中RSA加密算法实现 ●1.2.1 加密 ●1.2.2 解密 ●1.2.2.1 生成私钥 ●1.2.2.2 解密 ●1.1 RSA加密算法基础 ●1.2 算法优化 ●1.3 多素数 ●1.2 Golang中实现方式●2 Golan... golang int 转float 强转和高精度转 2020-03-17 16:43 疯狂的鸭血的博客 golang int 转float 方法一： 强转： 代码如下： import "fmt" func main() { var money int money = 1 fmt.Println(float64(money) / float64(100)) } 输出结果：0.01 方法二： 高精度转： 代码如下： ... big.Float SetPrec怪异的行为 2018-02-16 03:21 回答 2 已采纳 From the fine manual: type Float [...] Each Float value also has a precision, rounding mod Golang net.Listen绑定到已使用的端口 macospython 2018-06-27 20:35 回答 2 已采纳 OK, I think I have a story to tell about why this happens: Docker on mac, when mapping a port, b Golang os.Open没有这样的文件或目录错误 2015-11-24 03:59 回答 1 已采纳 Per our discussion in the comments, it turned out that the issue was caused by the application run strconv.FormatFloat的精度处理问题 2021-01-10 17:39 yuchenfw的博客 类型 指数长度 尾数长度 有效位 字节数 float32 8 23 6-7 4 float64 11 52 15-16 8 FormatFloat decimal 小数的数据结构如下： type decimal struct { d [800]byte // digits, big-endian representation nd int ... Golang.Go语言基础 2021-06-09 10:16 qq_51102350的博客 var distance int64 = 2.33e12 3，big包 big包提供3种类型： 存储大整数big.Int 存储任意精度浮点数big.Float 存储分数big.Rat //创建int64类型 hhhhh := big.NewInt(2333333) //创建很大很大的数 hhhhhhh := new... 8.10 Go语言big包：对整数的高精度计算 2020-06-16 18:01 study go的博客 Go语言中 math/big 包实现了大数字的多精度计算，支持 Int（有符号整数）、Rat（有理数）和 Float（浮点数）等数字类型。 这些类型可以实现任意位数的数字，只要内存足够大，但缺点是需要更大的内存和处理开销，这... Golang exec.Command（）bash命令不起作用 bash 2018-09-11 17:04 回答 1 已采纳 From the docs: Unlike the "system" library call from C and other languages, the os/exec packag Golang 进阶：strconv -- 字符串和基本数据类型之间转换 2019-07-30 15:16 IT_learner_1的博客 但由于浮点数有精度的问题，精度不一样，ParseFloat 和 FormatFloat 可能达不到互逆的效果。如： s := strconv . FormatFloat ( 1234.5678 , 'g' , 6 , 64 ) strconv . ParseFloat ( s , 64 ) 特别... 没有解决我的问题, 去提问```
``` 点击登录 提问题 ◇ 欢迎建议意见◇ 用户帮助中心◇ 回答首次被采纳有奖◇ 奖惩公告 悬赏问题 ¥500 基于畸形包响应的蜜罐识别技术 ¥200 python+selenium处理腾讯滑动验证码，界面拖动和校验没问题，但是提交后提示“验证码校验失败” ¥200 C#如何实现Unity3D环境基于维特智能蓝牙低功耗BLE传感器的多节点姿态再现问题 ¥200 来一份最新的WMAC+教程（改网卡mac） ¥100 无法执行赋值，因为左侧的大小为 2-by-1，右侧的大小为 2-by-2。 ¥100 python多进程计算，有偿程序修改。 ¥100 请问如何阻止按下电源键关机时出现的结束程序弹窗 ¥100 stm32f103求一个高效率的fft算法测频 ¥80 一道大学编译原理实验题，20点交，马上要交了，帮一下 ¥60 新入门c语言学习者need your help ```
``` ```
``` window.csdn.sideToolbar = { options: { afterFinished: function () { setTimeout(function () { var helphtml = '<a class="option-box" data-type="help" href="https://ask.csdn.net/questions/7441224" target="_blank">'+'<img src="https://g.csdnimg.cn/side-toolbar/2.5/images/bangzhucopy.png" alt="" srcset="">'+ '<span class="show-txt">帮助</span></a>'; \$('.csdn-side-toolbar > a:last').before(helphtml); }, 300); }, contentEl: document.getElementsByClassName('floor')[0] } } ```