首页 > 文章列表 > 为什么Python中的浮点计算不准确?

为什么Python中的浮点计算不准确?

Python 浮点计算 不准确
385 2023-08-20

什么是浮动?

The floating-point are also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

为什么浮点数计算不准确?

浮点计算不准确,主要是因为有理数是以二进制有限表示的近似值,而且通常是以任何进制都无法用有限位数表示的近似数。

Example

Let’s say we have a fraction −

5/3

We can write the above in base 10 as −

1.666...
1.666
1.667

如上所示,我们将1.666和1.667与分数5/3关联和考虑在一起,尽管第一个表示1.666…在数学上实际上等于该分数。

第二和第三个表示1.666和1.667的误差在0.001的数量级上,这比例如9.2和9.1999999999999993之间的误差更严重。第二个表示甚至没有正确地四舍五入!

As we don't have an issue with 0.666 as a representation of the number 2/3, therefore we shouldn't really have a problem with how 9.2 is approximated.

注意 − 有无限多个实数和无限多个有理数。浮点数表示是有限的,因此不可避免地有很多数是无法表示的。准确地说,64位只能区分出18,446,744,073,709,551,616个不同的值。

让我们来看一些转换和其结果。这将有助于我们更好地理解浮点数系统。

Convert floating to binary (base 2 number system)

We will see how to convert floating-point value to binary. Binary uses two digits, 0 and 1. Also called as base 2 number system Each position in a binary number represents a 0 power of the base (2). Last position in a binary number represents a x power of the base (2).

首先,我们从浮点数中取整数部分并将其转换为二进制,然后取小数部分并将其转换为二进制形式,最后将两者合并 -

Example

def floatoctal_convert(my_number, places = 3):
   my_whole, my_dec = str(my_number).split(".")
   my_whole = int(my_whole)
   my_dec = int (my_dec)
   res = bin(my_whole).lstrip("0b") + "."
   for x in range(places):
      my_whole, my_dec = str((my_decimal_converter(my_dec)) * 8).split(".")
      my_dec = int(my_dec)
      res += my_whole
   return res
def my_decimal_converter(num):
   while num > 1:
      num /= 10
   return num

# Driver Code
n = input("Enter floating point value : n")
p = int(input("Enter the number of decimal places of the result : n"))
print(floatoctal_convert(n, places = p))

输出

Enter floating point value :2.34
Enter the number of decimal places of the result :3
10.256

Convert floating to octal (base 8 number system)

八进制数使用八个数字,0、1、2、3、4、5、6、7。也被称为八进制数系统。八进制数中的每个位置表示基数(8)的0次幂。八进制数中的最后一个位置表示基数(8)的x次幂。

十进制数系统的基数是10,它使用从0到9的10个数字。在十进制数系统中,小数点左边的连续位置代表个位、十位、百位、千位等。

Example

Given a float decimal value and input the decimal places number, our task is to convert it to octal form −

def float_convert_octal(my_number, places = 3):
   my_whole, my_dec = str(my_number).split(".")
   my_whole = int(my_whole)
   my_dec = int (my_dec)
   res = oct(my_whole).lstrip("0o") + "."
   for x in range(places):
      my_whole, my_dec = str((decimal_converter(my_dec)) * 8).split(".")
      my_dec = int(my_dec)
      res += my_whole
   return res
def decimal_converter(num):
   while num > 1:
      num /= 10
   return num
n = input("Enter the floating point value : n")
p = int(input("Enter the number of decimal places of the result : n"))
print(float_convert_octal(n, places = p))

输出

Enter the floating point value :
6.89
Enter the number of decimal places of the result :
12
6.707534121727