﻿ Golang 运行时的除数为0的情况返回Inf | 阿小信的博客

# 阿小信的博客 Where there is a Python, there is a way. 阿小信大人

## Golang 运行时的除数为0的情况返回Inf2021-11-21 14:46

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.

```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
```

```//  runtime 1.0 / 0.0 is +Inf
if math.IsNaN(x) || math.IsInf(x, 0) {
x = 0.0
}
```

#Golang#   阅读 评论