为什么在Go语言中浮点数和整数会等于NaN(非数字)?

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

Why do floats and ints = Nan? in go

问题

package main

import (
    "fmt"
    "math"
)

func main() {
    // x = ± sqrt(B^2 - 4ac) / 2a
    cal()
}

func cal() {
    b := 3
    a := 4
    c := 2
    b2 := float64(b * b)
    ac := float64(4) * float64(a) * float64(c)
    q := math.Sqrt(b2 - ac)
    fmt.Print(q)
}

这段代码会输出 NaN,但为什么呢?我正在尝试制作一个二次方程计算器。我只希望它输出一个数字。

英文:
package main

import (
    "fmt"
    "math"
)

func main() {
    // x= +- sqrtB-4ac/2a
    cal()
}

func cal() {
    b := 3
    a := 4
    c := 2
    b2 := float64(b*b)
    ac := float64(4)*float64(a)*float64(c)
    q := math.Sqrt(b2-ac)
    fmt.Print(q)
}

This will output a NaN, but why. I am trying to make a quadratic calculator. All I want is for this to output the number.

答案1

得分: 5

因为你试图对一个负数求平方根,这不是一个有效的操作(不仅在Go语言中,在数学中也是如此),所以它返回NaN,这是Not A Number的缩写。

b := 3
a := 4
c := 2
b2 := float64(b*b) // 设置b2 == 9
ac := float64(4)*float64(a)*float64(c) // ac == 32 
q := math.Sqrt(b2-ac) // Sqrt(9-32) == Sqrt(-23) == NaN
fmt.Print(q)
q = math.Sqrt(math.Abs(b2-ac)) // 在评论中建议使用 Sqrt(23) == ~4.79 
// 或许这是你想要的结果。

编辑:请不要在数学问题上争论语义。如果你想讨论负数的平方根,这不是合适的地方。一般来说,无法对负数求平方根。

英文:

Because you're trying to take the square root of a negative number which isn't a valid operation (not just in Go, in math) and so it returns NaN which is an acronym for Not A Number.

b := 3
a := 4
c := 2
b2 := float64(b*b) // sets b2 == 9
ac := float64(4)*float64(a)*float64(c) // ac == 32 
q := math.Sqrt(b2-ac) // Sqrt(9-32) == Sqrt(-23) == NaN
fmt.Print(q)
q = math.Sqrt(math.Abs(b2-ac)) // suggested in comments does Sqrt(23) == ~4.79 
// perhaps the outcome you're looking for.

EDIT: please don't argue semantics on the math bit. If you want to discuss square roots of negative numbers this isn't the place. Generally speaking, it is not possible to take the square root of a negative number.

答案2

得分: 2

由于你正在对一个负数取平方根,所以你得到了一个虚数结果(sqrt(-9) == 3i)。这绝对不是你想要的结果。相反,你应该这样做:

func main() {
    b := float64(3)
    a := float64(4)
    c := float64(2)

    result := [2]float64{(-b + math.Sqrt(math.Abs(b*b - 4*a*c))) / (2 * a),
                         (-b - math.Sqrt(math.Abs(b*b - 4*a*c))) / (2 * a)}
    fmt.Println(result)
}

请注意,我只翻译了代码部分,其他内容我不会回答。

英文:

Since you're taking the square root of a negative number, you've got an imaginary result (sqrt(-9) == 3i). This is assuredly NOT what you're trying to do. Instead, do:

func main() {
    b := float64(3)
    a := float64(4)
    c := float64(2)

    result := [2]float64{(-b + math.Sqrt(math.Abs(b*b - 4*a*c))) / 2 * a,
                         (-b - math.Sqrt(math.Abs(b*b - 4*a*c))) / 2 * a)}
    fmt.Println(result)
}

答案3

得分: 1

你尝试对负数进行平方根运算,因此结果始终返回 NaN(不是一个数字)。
我运行了你的代码并打印了结果:

b := 3
a := 4
c := 2
b2 := float64(b*b)
fmt.Printf("%.2f \n", b2)
ac := float64(4)*float64(a)*float64(c)
fmt.Printf("%.2f \n", ac)
fmt.Printf("%.2f \n", b2-ac)
q := math.Sqrt(b2-ac)
fmt.Print(q)

控制台输出:
9.00
32.00
-23.00
NaN

Golang中的平方根函数:https://golang.org/pkg/math/#Sqrt

英文:

You try Sqrt Negative Number for this reason return always NaN ( Not a Number )
I run you code and print the results:

b := 3
    a := 4
    c := 2
    b2 := float64(b*b)
	fmt.Printf("%.2f \n", b2)
    ac := float64(4)*float64(a)*float64(c)
	fmt.Printf("%.2f \n", ac)
	fmt.Printf("%.2f \n", b2-ac)
    q := math.Sqrt(b2-ac)
    fmt.Print(q)

Console:
9.00
32.00
-23.00
NaN

Sqrt in Golang : https://golang.org/pkg/math/#Sqrt

huangapple
  • 本文由 发表于 2015年12月2日 04:35:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/34029609.html
匿名

发表评论

匿名网友

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

确定