dongliang1865 2017-09-08 15:16
浏览 796
已采纳

转到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?

  • 写回答

1条回答 默认 最新

  • dongren9739 2017-09-08 15:27
    关注
    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)))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大