Gopher refers to north

Anyone with common sense knows that you can’t divide by zero in a division operation, and we may divide by zero in a large number of contexts in a practical application. So it’s important to know if dividing by zero can panic? What would you get if there was no panic?

Below, we continue reading with this question in mind. I believe that after reading this article, these two questions will disappear. At the same time, in order to enable readers to quickly understand the overall picture of the paper, the following is the outline of the paper.

Divide by zero value

In Go, there are three possible ways of dividing by zero: by constant 0, integer 0, and floating point 0. Let’s look at each of these situations in action.

Divided by the constant 0

We know from the figure above that dividing by the constant 0 is not going to compile. This point, or more reassuring.

Divided by integer zero

According to the figure above, dividing by integer 0 causes panic. This point, in the peacetime development also need to pay special attention to.

Divided by floating point 0

Divide by floating point 0, and things get slightly more complicated. Take a look at the code and the output.

var zero = float64(0)
fmt.Println(1024 / zero) // Output: +Inf
fmt.Println(- 1024. / zero) // Output: -inf
fmt.Println(0 / zero) // NaN
Copy the code

Inf in the output above is the abbreviation of the word infinity, which means infinity, so +Inf and -INF denote plus and minus infinity respectively.

NaN means not a number, that is, the result is not a number.

At this point, Hsu can’t help but lament that floating point numbers are so extensive that dividing by 0 in Go does not panic (hsu verifies that it does in Python). In addition, 0/zero in the above gives NaN, while 0 divided by 0 will still panic in integer.

Plus or minus value of Inf

Check whether it is plus or minus Inf

We can get to + infinity and -infinity by dividing the positive and negative numbers by floating point 0, respectively. The Inf function provided by the MATH package in Go can also find positive and negative infinity, as well as the IsInf function to determine whether it is positive or negative infinity.

The math.inf function, with the signature func(int) float64, returns positive infinity if the argument passed is greater than or equal to 0, and negative infinity otherwise.

Math. IsInf the signature of the function is func(float64, int) bool. Returns the value of the first argument if the second argument is greater than 0. Returns the value of the first argument if the second argument is less than 0. Returns whether the first argument is infinite.

For verification, see the code and output below.

positiveInf := math.Inf(1)
negativeInf := math.Inf(- 1)
// Check whether it is positive infinity
fmt.Println(math.IsInf(positiveInf, 1)) // Output: true
// Check if it is negative infinity
fmt.Println(math.IsInf(negativeInf, 1)) // Output: false
// Check whether it is positive infinity
fmt.Println(math.IsInf(positiveInf, - 1)) // Output: false
// Check if it is negative infinity
fmt.Println(math.IsInf(negativeInf, - 1)) // Output: true
// Check whether it is infinite
fmt.Println(math.IsInf(positiveInf, 0)) // Output: true
// Check whether it is infinite
fmt.Println(math.IsInf(negativeInf, 0)) // Output: true
// Check whether it is infinite
fmt.Println(math.IsInf(1024.0)) // Output: false

Copy the code

The comparison of + Inf

The output of + Inf and -inf is very different from what we usually see with numeric types, and it is not possible to assign ±Inf directly to a variable of floating-point type.

positiveInf := math.Inf(1)
negativeInf := math.Inf(- 1)
// Determine whether infinity can be compared to itself
fmt.Println(positiveInf == positiveInf) // Output: true
// Determine whether negative infinity can be compared to itself
fmt.Println(negativeInf == negativeInf) // Output: true
// Determine whether infinity is greater than a positive number
fmt.Println(positiveInf > math.MaxFloat64) // Output: true
// Check whether negative infinity is less than positive
fmt.Println(negativeInf < -math.MaxFloat64) // Output: true
Copy the code

According to the output results, positive and negative infinity can be compared, and the value of the representation does imply the mathematical meaning of infinity.

NaN values

Check if it’s NaN

To give you a sense of how NaN values are generated, you can divide a floating point number by zero. Similarly, the Math package provides NaN functions and IsNaN functions to return a NaN value and determine whether a floating point number is a NaN, respectively.

Take a look at the code and output below.

nan := math.NaN()
fmt.Println(math.IsNaN(nan))  // Output: true
fmt.Println(math.IsNaN(10.2)) // Output: false
Copy the code

Other cases where NaN values can be generated:

  1. Any operation with NaN results in NaN
  2. ifx < -1 || x > 1,math.Acos(x),math.Asin(x),math.Atanh(x)All returns NaN
  3. ifx < 1,math.Acosh(x)Returns NaN
  4. ifx < 0,math.Sqrt(x)Returns NaN

There should be other cases of NaN returns, which Hsu won’t summarize.

The comparison of NaN

According to the static check “no value is equal to NaN, not even NaN itself”, no value is equal to NaN or even NaN itself. For further verification, let’s look at the following code and output.

nan := math.NaN()
positiveInf := math.Inf(1)
negativeInf := math.Inf(- 1)
fmt.Println(nan == nan)

fmt.Println(nan > 1024) // Output: false
fmt.Println(nan < 1024) // Output: false
fmt.Println(nan == 1024) // Output: false
fmt.Println(nan > positiveInf) // Output: false
fmt.Println(nan < negativeInf) // Output: false
fmt.Println(nan == positiveInf) // Output: false
fmt.Println(nan == negativeInf) // Output: false
Copy the code

Indeed, as prompted, NaN is not equal to itself, and it is false when compared to any value. Because NaN is special, we must pay attention to the handling of boundary values in practical development.

Write in the last

This article focuses on the various cases of dividing by constant 0, integer 0, and floating point 0, and analyzes the comparability between ±Inf and NaN. Here is a table comparing whether the various values are comparable.

Whether comparable +Inf -Inf NaN Number
+Inf Y Y N Y
-Inf Y Y N Y
Nan N N N N
Number Y Y N Y

By not comparable, I mean that NaN returns false when compared to any value

Finally, I sincerely hope that this article can be of some help to all readers.