常数截断为整数

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

Constant truncated to integer

问题

以下是翻译好的部分:

以下GO程序出现错误:

./fft.go:13: 常量-6.28319截断为整数
./fft.go:13: 无法将类型为int的-7 * k / N用作类型为float64的赋值

程序:

package main

import (
    "math"
    "fmt"
)

func main() {
    fmt.Println("Hello world ",math.E)

    var k, N int = 1, 10
    var ans float64 = 0
    var c float64 = (-2.0 * math.Pi * k) / N
    x := make([]float64,N)
    for i := 0; i < len(x); i++ {
        x[i] = 1
    }
    ans = 0
    for i := 0; i < N; i++ {
        ans += x[i] * math.E
    }
    fmt.Println(ans)
}

为什么我不能在float64类型中使用int类型?

英文:

The following GO program gives the error:

./fft.go:13: constant -6.28319 truncated to integer
./fft.go:13: cannot use -7 * k / N (type int) as type float64 in assignment

Program:

package main

import (
	&quot;math&quot;
	&quot;fmt&quot;
)

func main() {
	fmt.Println(&quot;Hello world &quot;,math.E)

	var k, N int = 1, 10
	var ans float64 = 0
	var c float64 = (-2.0 * math.Pi * k) / N
	x := make([]float64,N)
	for i := 0; i &lt; len(x); i++ {
		x[i] = 1
	}
	ans = 0
	for i := 0; i &lt; N; i++ {
		ans += x[i] * math.E
	}
	fmt.Println(ans)
}

Why cant I use an int in a type of float64 ?

答案1

得分: 17

Replace

var c float64 = (-2.0 * math.Pi * k) / N

by

var c float64 = (-2.0 * math.Pi * float64(k)) / float64(N)

To quote the spec:

> Conversions are required when different numeric types are mixed in an
> expression or assignment. For instance, int32 and int are not the same
> type even though they may have the same size on a particular
> architecture.

Go uses static typing and doesn't automatically convert between numeric types. The reason is probably to avoid some errors. For instance, what value and what type should float64(2.5) * int(2) yield? Should the result be int(5)? int(4)? float64(5.0)? In Go, this isn't an issue. The Go FAQ has more to say on this.


@jnml points out that, in this case, the following is enough:

var c float64 = -2 * math.Pi / float64(N)
英文:

Replace

var c float64 = (-2.0 * math.Pi * k) / N

by

var c float64 = (-2.0 * math.Pi * float64(k)) / float64(N)

To quote the spec:

> Conversions are required when different numeric types are mixed in an
> expression or assignment. For instance, int32 and int are not the same
> type even though they may have the same size on a particular
> architecture.

Go uses static typing and doesn't automatically convert between numeric types. The reason is probably to avoid some errors. For instance, what value and what type should float64(2.5) * int(2) yield? Should the result be int(5)? int(4)? float64(5.0)? In Go, this isn't an issue. The Go FAQ has more to say on this.


@jnml points out that, in this case, the following is enough:

var c float64 = -2 * math.Pi / float64(N)

huangapple
  • 本文由 发表于 2013年4月22日 23:45:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/16151199.html
匿名

发表评论

匿名网友

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

确定