转到uint8到float32

转到uint8到float32

问题描述:

I'm trying to learn Go and working on a rain intensity tool. For this tool I have to make a calculation like this:

var intensity float32
intensity = 10^((value−109)÷32)

The value is an uint8, ranging from 0 to 255. The intensity variable is a float.

However, Go tells me that

cannot use 10 ^ (value - 109) / 32 (type uint8) as type float32 in assignment

How can I solve this?

我正在尝试学习Go语言并致力于雨强度工具。 对于此工具,我必须进行如下计算: p>

  var强度float32 
intensity = 10 ^(((value−109)÷32)
  code>   pre> 
 
 

值是 uint8 code>,范围是0到255。强度变量是float。 p>

但是,Go告诉我 p>

不能使用10 ^(值-109)/ 32(类型uint8)作为类型 分配中的float32 p> blockquote>

我该如何解决? p> div>

  1. There is no ÷ operator in Go and ^ is a bitwise XOR, you need to use Pow functions from math package
  2. Go is very strict about type conversions, so it disallows implicit type conversions in many cases (so unsigned integer to floating point is not valid), so you need explicitly convert it with type(expr), i.e. float32(1)

That said:

intensity = float32(math.Pow(10, float64((value - 109) / 32)))
// - OR -
intensity = float32(math.Pow10(int((value - 109) / 32)))