Python代码根据包含小数点的情况产生不同的结果,但数值相同。

huangapple go评论48阅读模式
英文:

Python code yielding different result for same numerical value, depending on inclusion of precision point

问题

def two_d_third_order(x, a, b, c, d):
    return a + np.multiply(b, x) + np.multiply(c, np.multiply(x, x)) + np.multiply(d, np.multiply(x, np.multiply(x, x)))

我定义了一个函数,该函数接受一个值、一个列表或一个np.array,并返回一个三次多项式函数。

我注意到的问题是,当我在以下两个输入上使用"two_d_third_order"时:
1500
1500.0
并且使用(a, b, c, d) = (1.20740028e+00, -2.93682465e-03, 2.29938078e-06, -5.09134552e-10),我得到两个不同的结果:
2.4441
0.2574
,分别。我不知道为什么会出现这种情况,对任何帮助都将不胜感激。

我尝试了几个输入,但不知何故,尽管表示相同的数值,某些值上包含浮点数会改变最终结果。

英文:

I defined a function which returns a third order polynomial function for either a value, a list or a np.array:

def two_d_third_order(x, a, b, c, d):
    return a + np.multiply(b, x) + np.multiply(c, np.multiply(x, x)) + np.multiply(d, np.multiply(x, np.multiply(x, x)))

The issue I noticed is, however, when I use "two_d_third_order" on the following two inputs:
1500
1500.0
With (a, b, c, d) = (1.20740028e+00, -2.93682465e-03, 2.29938078e-06, -5.09134552e-10), I get two different results:
2.4441
0.2574
, respectively. I don't know how this is possible, and any help would be appreciated.

I tried several inputs, and somehow the inclusion of a floating point on certain values (despite representing the same numerical value) changes the end result.

答案1

得分: 0

1500 * 2_250_000 is 3_375_000_000 exceeding the int32 range:

print(type(np.multiply(1500, 2250000)))
print(np.multiply(1500, 2250000))

结果:

<class 'numpy.int32'>
-919967296

然而,使用浮点数会有更大的容量。

print(type(np.multiply(1500.0, 2250000.0)))
print(np.multiply(1500.0, 2250000.0))

结果:

<class 'numpy.float64'>
3375000000.0

尝试将输入强制转换为更大的整数。

(a, b, c, d) = (1.20740028e+00, -2.93682465e-03, 2.29938078e-06, -5.09134552e-10)
x1 = np.int64(1500)
x2 = 1500.0
print(two_d_third_order(x1, a, b, c, d) == two_d_third_order(x2, a, b, c, d))
英文:

1500 * 2_250_000 is 3_375_000_000 overflowing the range on int32:

print(type(np.multiply(1500, 2250000)))
print(np.multiply(1500, 2250000))

giving:

&lt;class &#39;numpy.int32&#39;&gt;
-919967296

Where as floats use a much larger container.

print(type(np.multiply(1500.0, 2250000.0)))
print(np.multiply(1500.0, 2250000.0))

giving:

&lt;class &#39;numpy.float64&#39;&gt;
3375000000.0

Try casting your input to a larger int.

(a, b, c, d) = (1.20740028e+00, -2.93682465e-03, 2.29938078e-06, -5.09134552e-10)
x1 = np.int64(1500)
x2 = 1500.0
print(two_d_third_order(x1, a, b, c, d) == two_d_third_order(x2, a, b, c, d))

答案2

得分: 0

Python使用隐式数据类型转换。当你只使用整数(比如1500)时,在所有后续操作中会有精度损失。而当你传递一个浮点数或双精度数(比如1500.0),后续操作会使用相关的数据类型进行,即在这种情况下会有更高的精度。

这并不是一个“bug”,可以说,而是Python在没有显式声明数据类型的情况下通常的操作方式。像C和C++这样的语言需要显式声明数据类型和显式进行数据类型转换,以确保操作在规定的精度格式下进行。这取决于使用方式,可能是一个福音,也可能是一个诅咒。

英文:

Python uses implicit data type conversions. When you use only integers (like 1500), there is a loss of precision in all subsequent operations. Whereas when you pass it a float or double (like 1500.0), subsequent operations are performed with the associated datatype, i.e in this case with higher precision.

This is not a "bug" so to speak, but generally how Python operates without the explicit declaration of data types. Languages like C and C++ require explicit data type declarations and explicit data type casting to ensure operations are performed in the prescribed precision formats. Can be a boon or a bane depending on usage.

huangapple
  • 本文由 发表于 2023年2月6日 19:19:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定