how can I force division to be floating point in Go?

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

how can I force division to be floating point in Go?

问题

我有以下代码片段:

package main

import (
    "fmt"
    "flag"
)

func main() {
    var a = flag.Int("a", 0, "divident")
    var b = flag.Int("b", 1, "divisor")
    flag.Parse()

    fmt.Printf("%f", float64(*a) / float64(*b))
}

对于命令行参数 -a 3-b 2,输出结果是:1.500000

要将这个除法强制转换为浮点数,最好/最优雅的方法是使用 float64() 函数将整数转换为浮点数。在上面的代码中,我已经使用 float64()ab 的值转换为浮点数,以确保除法结果为浮点数。

英文:

I have the following code snippet:

package main

import("fmt";"flag")

func main() {
    var a = flag.Int("a",0,"divident")
    var b = flag.Int("b",1,"divisor")
    flag.Parse()

    fmt.Printf("%f",*a / *b )
}

For -a 3 and -b 2 command line arguments, the output is: %!f(int=1)

What is the best / most elegant way to force this division to be floating point?

答案1

得分: 22

在Go语言中,变量之间没有隐式类型转换,所以你必须进行类型转换来实现。你可以使用以下方式将变量转换为浮点数:

fmt.Printf("%f", float32(a)/float32(b))

或者

fmt.Printf("%f", float32(a/b))

具体取决于你的需求。另外,你还可以查看float64类型,如果它符合你的需求的话。

英文:

There are no implicit type casts for variables in Go, so you must convert to float:

fmt.Printf("%f", float32(a)/float32(b))

or

fmt.Printf("%f", float32(a/b))

Depending upon what you want. Also check out float64 -- if that floats your boat.

答案2

得分: 6

你首先需要将类型转换为浮点数。

一般来说,如果你有一些非浮点数类型(比如int)的变量ab,为了进行浮点数除法,你可以使用float32(a)/float32(b)(或者根据情况使用float64)。对于任何其他数值类型,如果你想将浮点数视为整数或将整数视为复数,都需要转换操作数。在这种情况下,如果a是3,b是2,那么float32(a)/float32(b)的结果将是1.5。

如果你想进行整数除法,但结果是浮点数,那么可以将结果转换为浮点数,例如float32(a/b)。在这种情况下,如果a是3,b是2,那么float32(a/b)的结果将是1.0。

英文:

You have to convert the types to floats first.

In general, if you have some non-float numeric types (such as ints) a and b, in order to get a float division you use float32(a)/ float32(b) (or float64 as the case may be). This applies to any other numeric type too, if you want to treat floats as integers or integers as complex numbers convert the operands. In this case, if a is 3 and b is 2, float32(a)/float32(b) will be 1.5.

If you want integer division to be done, but the result to be a float, then covert the result as in float32(a/b). In this case, if a is 3 and b is 2, then float32(a/b) will get you 1.0.

答案3

得分: 0

回复建议使用fmt.Printf("%f", float32(a)/float32(b))的版本。对于ab的大值,我对此持怀疑态度,因为将它们分别转换为float32可能会溢出,即使它们的商不会溢出。

然而,在这里进行测试时,Golang编译器似乎足够聪明,使得float32(a)/float32(b)可以“正常工作”,因此在这种情况下不需要像下面这样的构造:

f, _ := big.NewRat(a, b).Float32()
英文:

The replies suggest versions of fmt.Printf("%f", float32(a)/float32(b)). I was sceptical about this for large values of a or b, as the individual conversions to float32 might overflow even if their quotient wouldn't.

However, testing this here, the golang compiler seems to be clever enough to make float32(a)/float32(b) "just work", and hence constructs like

f, _ := big.NewRat(a, b).Float32()

unnecessary in this case.

答案4

得分: -7

你应该将除法结果转换为浮点数。

英文:

well you should cast your division result as float

huangapple
  • 本文由 发表于 2013年12月14日 00:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/20571280.html
匿名

发表评论

匿名网友

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

确定