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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序