学习Go语言 — 作用域

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

Learning Go -- Scope

问题

你好,我是你的中文翻译。以下是你要翻译的内容:

在Go编程语言中,我是新手。

我正在从http://www.golang-book.com/学习。

在第4章的练习中,有一个关于将华氏度转换为摄氏度的问题。

我编写了以下答案的代码:

package main

import "fmt"

func main(){
    
    fmt.Println("Enter temperature in Farentheit ");
    
    var input float64
    
    fmt.Scanf("%f",&input)
        
    var outpu1 float64 = ( ( (input-32)* (5) ) /9)
    var outpu2 float64=  (input-32) * (5/9)
    var outpu3 float64= (input -32) * 5/9
    var outpu4 float64=  ( (input-32) * (5/9) )    

    fmt.Println("the temperature in Centigrade is ",outpu1)
    fmt.Println("the temperature in Centigrade is ",outpu2)
    fmt.Println("the temperature in Centigrade is ",outpu3)
    fmt.Println("the temperature in Centigrade is ",outpu4)    
}

输出结果如下:

sreeprasad:projectsInGo sreeprasad$ go run convertFarentheitToCentigrade.go 
Enter temperature in Farentheit 
12.234234
the temperature in Centigrade is  -10.980981111111111
the temperature in Centigrade is  -0
the temperature in Centigrade is  -10.980981111111111
the temperature in Centigrade is  -0

我的问题是关于outpu2和outpu4。括号是正确的,但为什么会打印出-0。

请问有人可以解释一下吗?

英文:

Hi I am new to Go programing language.

I am learning from http://www.golang-book.com/

In chapter 4, under Exercises, there is a question on converting from Fahrenheit to Centigrade.

I coded up the answer as follows

    package main

import "fmt"

func main(){
	
	fmt.Println("Enter temperature in Farentheit ");
	
	var input float64
	
	fmt.Scanf("%f",&input)
		
	var outpu1 float64 = ( ( (input-32)* (5) ) /9)
	var outpu2 float64=  (input-32) * (5/9)
	var outpu3 float64= (input -32) * 5/9
	var outpu4 float64=  ( (input-32) * (5/9) )	

	fmt.Println("the temperature in Centigrade is ",outpu1)
	fmt.Println("the temperature in Centigrade is ",outpu2)
	fmt.Println("the temperature in Centigrade is ",outpu3)
	fmt.Println("the temperature in Centigrade is ",outpu4)	
}

The output was as follows

sreeprasad:projectsInGo sreeprasad$ go run convertFarentheitToCentigrade.go 
Enter temperature in Farentheit 
12.234234
the temperature in Centigrade is  -10.980981111111111
the temperature in Centigrade is  -0
the temperature in Centigrade is  -10.980981111111111
the temperature in Centigrade is  -0

My question is with outpu2 and outpu4. The parenthesizes are correct but how or why does it print -0.

Could anyone please explain

答案1

得分: 7

简单来说,表达式(5/9)被计算为(int(5)/int(9)),结果为0。尝试使用(5./9)

为了澄清为什么会发生这种情况,这与表达式变量类型确定的顺序有关。

我猜测,因为(5/9)在情况2和4中存在而不考虑input,编译器将它们解释为int,并简单地将表达式替换为0,此时零被认为依赖于input,因此在最终编译之前将其视为float64类型。

一般来说,Go不会为您转换数字类型,所以这是我能理解的唯一解释。

英文:

Quite simply, the expression (5/9) is evaluated as (int(5)/int(9)) which equals 0. Try (5./9)

And to clarify why this is happening, it deals with the order in which the expression variable's types are determined.

I would guess that b/c (5/9) exists without regards to input in case 2 and 4 above, the compiler interprets them as int and simply replaces the expression with 0, at which point then the zero is considered dependent on input and thus takes on the type float64 before final compilation.

Generally speaking, Go does not convert numeric types for you, so this is the only explanation that would make sense to me.

答案2

得分: 3

根据Go语言规范float32float64是遵循IEEE-754标准的有符号浮点数。以下文字引用自Wikipedia - Signed zero

IEEE 754浮点算术标准(目前由大多数计算机和支持浮点数的编程语言使用)要求同时存在+0和-0。这些零可以被视为扩展实数线的变体,使得1/−0 = −∞和1/+0 = +∞,只有对于±0/±0和±∞/±∞的除法是未定义的。

显然,作为float64input在减去32后变成了另一个负的float645/9的计算结果为0。一个负的float64乘以0得到的是-0

有趣的是,如果你用一个整数替换input,比如1,你会得到0而不是-0。看起来在Go语言中,浮点数有+0-0,但整数没有。

编辑: PhiLho在评论中解释了为什么浮点数有这种特性而整数没有的原因:规范化的浮点数有特殊的表示形式,包括+0、-0、NaN、+Infinity和-Infinity,而你不能为整数保留一些位组合来表示这些含义。

英文:

The Go language Spec indicates that float32 and float64 are signed floating numbers that follow IEEE-754 standard. Following text is quoted from Wikipedia - Signed zero:

> The IEEE 754 standard for floating point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0. The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0 and ±∞/±∞.

Clearly, input, as a float64, when applied minus 32, turns into another float64 which is negative. 5/9 evaluates into 0. A negative float64 timed by 0 is -0.

Interestingly, if you replace input with an integer, e.g. 1, you'll get 0 instead of -0. It seems that in Go, floating numbers have both +0 and -0, but integers don't.

EDIT: PhiLho explains in comment about the reason why floating numbers have such thing while integers don't: normalized floating point numbers have special representations of +0, -0, NaN, +Infinity and -Infinity, while you cannot reserve some bit combinations of an integer number to have such meanings.

huangapple
  • 本文由 发表于 2012年11月22日 10:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/13505202.html
  • go

html/template:如何在没有` :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定