Python Numbers

Exploring floating numbers

1. Representing decimal numbers in Python

Unlike integers, floats (short for floating-point numbers) are used to represent decimal numbers in Python. Examples include 3.14159 (pi), 1.234, and -98.7.

Python uses a binary floating-point system to store these numbers internally. This means they are represented using a combination of zeros and ones, similar to how integers are stored, but with a special allocation for the decimal part.


2. Understanding floating-point precision limitations

It's important to understand that computers can't represent all possible decimal numbers perfectly using a finite number of bits. This can lead to slight inaccuracies in calculations with floats. Here's why:

The number of bits used to store the decimal part determines the level of precision. While Python uses double-precision floats offering a good range and precision, some decimal numbers might be approximated slightly.


3. Performing arithmetic operations with floats

Python supports most arithmetic operations with floats, similar to integers:

  • Addition (+), Subtraction (-), Multiplication (*): These work as expected, but keep in mind potential precision limitations.
  • Division (/): This usually results in a float, even if dividing two integers.

Example:

radius = 10.0
pi = 3.14159

# Slight precision loss might occur here due to float representation
area = pi * radius ** 2
print(area)  # This might not print exactly 314.159... due to precision limitations


4. Common functions for working with floats

  • round(number, ndigits=0): Rounds a float to a specified number of decimal places (ndigits). Useful for controlling the displayed precision.
  • abs(number): Returns the absolute value (positive version) of a float.

Example:

# Rounding to 2 decimal places
rounded_area = round(area, 2)
print(rounded_area)  # This might print 314.16 (more precise display)

# Getting absolute value
distance = abs(-5.2)
print(distance)  # distance will be 5.2


Remember:
While floating-point precision limitations exist, they are usually negligible for most everyday calculations. However, if you require very high precision, consider using libraries like decimal that offer more accurate decimal number handling.

It's time to take a quiz!

Test your knowledge and see what you've just learned.

Which of the following is a valid float representation in Python?

all of the above
3.14159
1.234
-98.7
Check Answer

What can cause inaccuracies in floating-point calculations in Python?

Finite number of bits used to store decimal numbers
All decimals can be represented exactly
Integer division only
None of the above
Check Answer

What is the result of the following operation: 10.0 / 2?

10
5.0
5
2
Check Answer

What does the round() function do in Python?

Converts float to int
Rounds a float to a specified number of decimal places
Returns the float's absolute value
None of the above
Check Answer

What will abs(-5.2) return in Python?

5.2
None
5.2
5.0
Check Answer