GoLang nextafter & %g?

huangapple go评论90阅读模式
英文:

GoLang nextafter & %g?

问题

我只会翻译你提供的内容,以下是翻译的结果:

我只是在学习Go语言的教程,因为我对学习一种更通用的语言很感兴趣(目前只懂JavaScript)。教程链接

我不明白%gmath.Nextafter的含义是什么?在查看了文档之后,我对此有一点了解。

问题:
有人能给我更好地解释一下%g语法和nextafter()方法吗?或许能提供一个简单易懂的用例?这里是文档链接:fmt // nextAfter

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Printf("Now you have %G problems.",
        math.Nextafter(2, 3))
}
英文:

I'm just going through tutorials of GoLang because I'm curious to learn a more general language (only know javascript). Tut

I dont understand whats happening with %g and math.Nextafter means? After reviewing the documentation it helps little.

Question:
Could anyone give me a better overview of the %g syntax and nextafter() method; perhaps a digestible usecase?? Here's links to documentation:fmt // nextAfter

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Printf("Now you have %G problems.",
        math.Nextafter(2, 3))
}

答案1

得分: 4

关于%g语法,请参阅fmt语法

%g	根据输出的紧凑程度选择%e或%f中较小的一个
%e	科学计数法,例如-1234.456e+78
%f	小数点形式,没有指数,例如123.456

NextAfter()返回的float64将使用科学计数法或小数点形式呈现,具体取决于哪种形式更紧凑。

关于NextAfter的返回值,请参阅"为什么Go中的math.Nextafter(2,3)增加0.0000000000000004而不是0.0000000000000001?"

在"Go示例:字符串格式化"中,您将看到大量的Go字符串格式化:一个参数用于格式,一个参数用于要格式化的值。
这些格式("verb")在`pkg/fmt/中列出。

英文:

Regarding the %g syntax, see fmt syntax:

%g	whichever of %e or %f produces more compact output
%e	scientific notation, e.g. -1234.456e+78
%f	decimal point but no exponent, e.g. 123.456

The float64 returned by NextAfter() will be presented using the scientific notation or decimal point, depending on which is more compact.

For the returned value of NextAfter, see "Why does math.Nextafter(2,3) in Go increment by 0.0000000000000004 instead of 0.0000000000000001?"

You will see plenty of Go string formatting in "Go by Example: String Formatting": one parameter for the format, one for the value to be formatted.
Those formats ("verb") are listed in `pkg/fmt/.

huangapple
  • 本文由 发表于 2014年12月1日 02:52:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/27216886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定