duandanai6470 2018-12-21 15:05
浏览 78
已采纳

fmt.Println不会打印整个浮点数

i'm trying to make a program that calculates the value of pi and print it on the terminal, but only part of it shows on the screen

package main

import (
    "fmt"
)

func gregorypi() float64 {
    numerator := 1.0
    divisor := 3.0
    var result float64

    for i := 0; i <= 100000000; i++ {
            if i%2 == 0 {
                    result += numerator / divisor
                    divisor += 2
            } else {
                    result -= numerator / divisor
                    divisor += 2

            }

    }

    return (4 * (1 - result))

}

func main() {

    fmt.Sprint(gregorypi())

}

the output is: 3.1415926435897683

How do i make so the entire value appears?

If it helps answering the question: the formula that i'm using is the Gregory-Leibniz one

  • 写回答

1条回答 默认 最新

  • doukongpao0903 2018-12-21 15:09
    关注

    func Sprint

    func Sprint(a ...interface{}) string
    

    Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.


    fmt.Sprint returns a string. Don't throw it away; print it.

    For example,

    func main() {
        s := fmt.Sprint(gregorypi())
        fmt.Println(s)
    }
    

    Playground: https://play.golang.org/p/zOVtJc5HfXT

    Output:

    3.1415926435897683
    

    If you want a more precise result than IEEE 754 64-bit floating-point, use the Go math/big package.


    For example,

    package main
    
    import (
        "fmt"
        "math/big"
    )
    
    func gregorypi(iter int, prec uint) *big.Float {
        one := big.NewFloat(1)
        two := big.NewFloat(2)
        four := big.NewFloat(4)
        numerator := big.NewFloat(1).SetPrec(prec)
        divisor := big.NewFloat(3).SetPrec(prec)
        result := new(big.Float).SetPrec(prec)
    
        for i := 0; i <= iter; i++ {
            if i%2 == 0 {
                result.Add(result, new(big.Float).Quo(numerator, divisor))
            } else {
                result.Sub(result, new(big.Float).Quo(numerator, divisor))
            }
            divisor.Add(divisor, two)
        }
    
        return new(big.Float).Mul(four, (new(big.Float).Sub(one, result)))
    
    }
    
    func main() {
        pi := gregorypi(1000000, 4096)
    
        s := fmt.Sprint(pi)
        for i, j := 0, 0; i < len(s); i = j {
            j = i + 60
            if j > len(s) {
                j = len(s)
            }
            fmt.Println(s[i:j])
        }
    }
    

    Output:

    3.1415916535917932347126498832691903993221476025342620143148
    652250543253683419302770934570540444696092784515646197918416
    151531004207388567190381995115109764938931650751911723670828
    705935671760975424757204401786734958287682330276123443546400
    855990841037108771486255447137253791363986086026524209725177
    041621398938122502069099548344075841960714856836014332632487
    249750055949826053894250927777836988620276187954914335474236
    918693730264540456485214156731536507281567237095178205850787
    841887779856892311307656085287982909183503886914799719930651
    362811646060939082163336282527001630768619465916054216662233
    647867947277285894357821722722774052560440050583160360321106
    944441543917458948854721791262992799982426130041196627951982
    504267630916391384654303083557011574782570253034162966525670
    594481466474403221843818413155653700689258246423537757630787
    951957260253561222183715232613769610553278998905210089847178
    951692922815021787390361813556619177840043555176857090268624
    902791212304201317960964869615176784400855500998688041903616
    187746299057333789390884081442894661717258714989758581139759
    403617872623424695182488545758897457727485849932980050101014
    208290473579625783313281627594640164541223264510142763045401
    708614153731722941036904397534247
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Yolov5训练报错
  • ¥15 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面
  • ¥15 有没有人知道这种提示怎么关?现在不做ts项目了不知道咋关了,求解,现在我以前的js项目都是这种提示了
  • ¥15 为什么mysql做了碎片化处理data_free还是很高
  • ¥15 single positional indexer is out-of-bounds
  • ¥15 LSTM 模型数据量需要多少?
  • ¥15 mysql数据抓包开发报表
  • ¥15 linux系统下安装office打不开
  • ¥20 rsync脚本分发错误,文件不齐全
  • ¥15 labview顺序结构与时间延时