Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 如何修复';(1<;<;100)*0.1和(1<;<;100)/10';_Go - Fatal编程技术网

Go 如何修复';(1<;<;100)*0.1和(1<;<;100)/10';

Go 如何修复';(1<;<;100)*0.1和(1<;<;100)/10';,go,Go,在Go的“数字常量”部分中,代码是 package main import "fmt" const ( // Create a huge number by shifting a 1 bit left 100 places. // In other words, the binary number that is 1 followed by 100 zeroes. Big = 1 << 100 // Shift it right again 99

在Go的“数字常量”部分中,代码是

package main

import "fmt"

const (
    // Create a huge number by shifting a 1 bit left 100 places.
    // In other words, the binary number that is 1 followed by 100 zeroes.
    Big = 1 << 100
    // Shift it right again 99 places, so we end up with 1<<1, or 2.
    Small = Big >> 99
)

func needInt(x int) int { return x*10 + 1 }

func needFloat(x float64) float64 { return x * 0.1 }

func main() {
    fmt.Println(needInt(Small))
    fmt.Println(needFloat(Small))
    fmt.Println(needFloat(Big))

    fmt.Println(Big * 0.1) //one
    fmt.Println(Big / 10)  //two
}
主程序包
输入“fmt”
常数(
//通过将1位向左移动100个位置来创建一个巨大的数字。
//换句话说,二进制数是1后跟100个零。
Big=1来自Go博客:

数值常量存在于任意精度的数值空间中;它们 它们只是普通的数字。但是当它们被分配给一个变量时 值必须能够适应目标

举个例子可以更清楚地说明这一点:

const f = 1 << 31

x := f / 10  // what type is x?
y := f * 0.1 // what type is y?

fmt.Printf(" 10 : type = %8T\n", 10)    // int
fmt.Printf("0.1 : type = %8T\n\n", 0.1) // float64

fmt.Printf(" x  : type = %8T  value = %v\n", x, x)
fmt.Printf(" y  : type = %8T  value = %v\n", y, y)

  • x
    是一个
    int
    ,因为除法器
    10
    被解释为
    int
  • y
    是一个
    float64
    ,因为乘数
    0.1
    被解释为
    float64

在tour示例中,常量为
1请读取。求和:常量可以是任意精度/大的。但有时必须将常量转换为真正的Go类型。在您的示例中,是浮点和int。并非每个常量都可以转换,这是编译时错误。区别在于:
Big*0.1
转换为浮点,而
Big/10
在博客帖子中被转换为int.详细信息。非常想你,我知道了。@joystrong如果你想回答你的问题,请不要忘记将其设置为“已接受”(单击复选标记)以结束问题。
 10 : type =      int
0.1 : type =  float64

 x  : type =      int  value = 214748364
 y  : type =  float64  value = 2.147483648e+08