查看 decimal 的源码,下面是其注释,发现若 value 为 NaN 或者 +/- inf 时,会 panic。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// NewFromFloat32 converts a float32 to Decimal. // // The converted number will contain the number of significant digits that can be // represented in a float with reliable roundtrip. // This is typically 6-8 digits depending on the input. // See https://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/ for more information. // // For slightly faster conversion, use NewFromFloatWithExponent where you can specify the precision in absolute terms. // // NOTE: this will panic on NaN, +/-inf funcNewFromFloat32(value float32) Decimal { if value == 0 { return New(0, 0) } // XOR is workaround for https://github.com/golang/go/issues/26285 a := math.Float32bits(value) ^ 0x80808080 return newFromFloat(float64(value), uint64(a)^0x80808080, &float32info) }
显然我们不可能传递一个非法的 p,在我们的认知里面,X 不应该有 NaN 这种数值,即使 p 没有初始化,那也应该不是 NaN。
fnmain() { leta = 0; letb = 0; println!("{}", a/b); // attempt to divide `0_i32` by zero leta = 3; letb = 3; println!("{}", 9/(a-b)); // attempt to divide `9_i32` by zero println!("{}", 0.0f64/0f64); // NaN println!("{}", 0.1f64/0f64); // inf println!("{}", -0.1f64/0f64); // -inf println!("{}", 0.0f32/0f32); // NaN println!("{}", 0.1f32/0f32); // inf println!("{}", -0.1f32/0f32); // -inf }
Go
go version go1.17 windows/amd64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
package main
funcmain() { // var a int32 = 3 // var b int32 = 3 // var c int32 = 9 // println(c / (a - b)) // 运行出错 panic: runtime error: integer divide by zero // println(c / 0) // 编译出错 division by zero // println(float64(0) / float64(0)) // 编译出错 division by zero var a float64 = 0.0 println(float64(0) / a) // NaN println(float64(0.1) / a) // +Inf println(float64(-0.1) / a) // -Inf var b float32 = 0.0 println(float32(0) / b) // NaN println(float32(0.1) / b) // +Inf println(float32(-0.1) / b) // -Inf }
C
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<stdio.h>
intmain() { // printf("%d\n", 0/0); // 编译警告 warning: division by zero [-Wdiv-by-zero] int a = 0; int b = 0; // printf("%d\n", a/b); // 运行出错 Floating point exception printf("%f\n", (float)0/(float)0); // -nan printf("%f\n", (float)0.1/(float)0); // +inf printf("%f\n", (float)-0.1/(float)0); // -inf printf("%f\n", (double)0/(double)0); // -nan printf("%f\n", (double)0.1/(double)0); // +inf printf("%f\n", (double)-0.1/(double)0); // -inf }
C++
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license"for more information. >>> 0/0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: division by zero >>> 0.1/0.0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: float division by zero >>> 0.0/0.0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: float division by zero >>> -0.1/0.0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: float division by zero
python 2.7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2 Type "help", "copyright", "credits" or "license"for more information. >>> 0/0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero >>> 0.1/0.0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: float division by zero >>> 0.0/0.0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: float division by zero >>> -0.1/0.0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: float division by zero