Python Tutorial Floating point arithmetic demystified

Mondo Technology Updated on 2024-01-19

Hi guys and welcome to my python tutorial column!Today we're going to talk about a very interesting topic in Python -Floating-point arithmetic。In our daily coding, we may encounter some bothersome floating-point arithmetic, let's demystify this mystery together!

The essence of the problem: decimal vs binary

First, let's understand how floating-point numbers are represented in computers. At the hardware level, floating-point numbers are expressed as binary fractions based on 2. This means that some decimal decimals may not be accurately represented as binary fractions. For example, the decimal 01 is an infinitely looping fraction in binary. Therefore, what we store in the machine is an approximation rather than a true decimal decimal.

Here's an example

For example, we all know that the decimal 1 3 cannot be accurately represented, we can only write it down. 333, but can never write down a decimal decimal place that is exactly equal to 1 3. Again, 01 The binary machine stores an approximate value, not an exact value.

print(0.1 + 0.1 + 0.1 == 0.3) This will return false

Output

false

Problems with the display of decimals

Python usually rounds the displayed value of floating-point numbers for display convenience. As a result, there may be a slight difference between the value we see and the value we actually store when printing.

Import the math module.

import math

Define a floating-point number.

num = 0.1

Prints the binary approximation of the actual storage.

print(num)

Use formatted strings to display 40-digit significant figures.

formatted_num = format(num, '.40g')

print(formatted_num)

Prints the true value of num.

print(repr(num))

Output

How to deal with it?

To get more control over the output of floating-point numbers, we can use formatted strings to control the display of significant digits:

Method 1

Import the math module.

import math

formatted_num = format(math.pi, '.12g') displays 12 significant digits.

print(formatted_num)

Output'3.14159265359'

Output

or

Method two

Import the math module.

import math

print(''.format(math.pi)) Two decimal places are displayed.

Output'3.14'

Output

Summary

The floating-point problem is not unique to Python, it is present in all languages that support hardware floating-point arithmetic. For the vast majority of everyday applications, the error of floating-point arithmetic does not have much impact on the final result. However, in some scenarios where precision is critical, we may need to use other methods, such as using string formatting, or consider using a library that supports high-precision operations.

Finally, don't be beenFloating-point arithmetic problemsScared,As long as we deal with it consciously and use the appropriate methods, we can still get the results we want. Hope this article helps!

If you have more questions about this topic, or if you have anything else you would like to know, please let me know in the commentsThank you all for your support and we'll see you next time!

Related Pages