在Golang中,除数为0时,常规情况下是无法编译通过的,但是如果除数是由运行时决定的,那么为除数为0时则不会panic,而是实现为IEEE 754标准,返回Inf。
The result of a floating-point or complex division by zero is not specified beyond the IEEE-754 standard; whether a run-time panic occurs is implementation-specific.
IEEE 754 specifies five arithmetic exceptions that are to be recorded in the status flags ("sticky bits"):
inexact, set if the rounded (and returned) value is different from the mathematically exact result of the operation. underflow, set if the rounded value is tiny (as specified in IEEE 754) and inexact (or maybe limited to if it has denormalization loss, as per the 1984 version of IEEE 754), returning a subnormal value including the zeros. overflow, set if the absolute value of the rounded value is too large to be represented. An infinity or maximal finite value is returned, depending on which rounding is used. divide-by-zero, set if the result is infinite given finite operands, returning an infinity, either +∞ or −∞. invalid, set if a real-valued result cannot be returned e.g. sqrt(−1) or 0/0, returning a quiet NaN.
这种情况可能导致的问题是:如果结构体中的字段是由运行时算出为Inf,当你对这个结构体进行json序列化时,json Marshal 会返回错误:
json: unsupported value: +Inf
通过一段代码说明:
package main import ( "encoding/json" "fmt" "strconv" ) func interfaceToFloat64(unk interface{}) (result float64) { switch i := unk.(type) { case float64: result = float64(i) case float32: result = float64(i) case int64: result = float64(i) case int32: result = float64(i) case int: result = float64(i) case uint32: result = float64(i) case uint64: result = float64(i) case uint: result = float64(i) case string: if i == "" || i == "--" { result = 0.0 } else { result, _ = strconv.ParseFloat(i, 64) } default: result = 0.0 } return } func main() { var ( a interface{} = 1 b interface{} = 0 ) x := interfaceToFloat64(a) / interfaceToFloat64(b) fmt.Println("x:", x) type T struct { F float64 } t := T{ F: x, } fmt.Printf("t: %+v\n", t) _, err := json.Marshal(t) fmt.Println("err:", err) }
运行后输出结果:
x: +Inf t: {F:+Inf} err: json: unsupported value: +Inf
参考文档:
对于json序列化Inf失败的解决方法可以判断其是否为Inf,将其设为特定值来解决,如:
// runtime 1.0 / 0.0 is +Inf if math.IsNaN(x) || math.IsInf(x, 0) { x = 0.0 }
网友66.*.*.212[火星]2022-05-26 22:03
网友185.*.*.40[火星]2022-05-26 22:01
网友106.*.*.108[火星]2022-05-26 21:49
网友49.*.*.31[火星]2022-05-26 21:46
发表评论
亲~ 评论内容是必须的哟! o(∩_∩)o
昵称
邮箱
主页
评论