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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符