dshp9580656 2015-08-27 15:12
浏览 6
已采纳

如何在Go中添加两个任意大数?

When I add two very large floats I get the wrong output. How can I do this in Go? PHP has the bcadd function.

var a float64 = 12959653081233191386469183112744623843489338314724603559902557916087872259523073406440221030943397504960564327459290759156915189196536625503825265749393408
var b float64 = 1302494993937727547864388263735304125561725318351673964024430436931705604299209078600534362879064309484886438718428990856894006118477463552
// correct value: 12959653081233192688964177050472171707877602050028729121627876267761836283953510338145825330152476105494927206523600244043353907625527482397831384226856960
// actual output: 12959653081233192875034890470147535688504496869357889916160064603715259716015001328389483087182344476999836189247073027598559438829669033382689401152208896
fmt.Println(strconv.FormatFloat(a+b, 'f', 0, 64))
  • 写回答

1条回答 默认 最新

  • dovs36921 2015-08-27 15:32
    关注

    who can help me? in php has bcadd function

    bcadd and its friends are arbitrary precision arithmetic functions (that's why they operate on strings and not floats). A float has limited precision. In effect, bcmath offers infinitely more precision than a float.

    Use math/big.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题