In Python, numbers can be of different types. One important type is floating-point numbers, or floats. A float is a number that has a decimal point. This type of number can represent values like 3.14, 0.99, or even -5.5.

What is a Floating-Point Number?

A floating-point number is any number that includes a decimal point. For example:

  • 3.14 is a floating-point number.
  • -7.5 is also a floating-point number.
  • 0.99 is another example of a floating-point number.

Why Do We Use Floats?

Floats are used when you need to represent numbers that are not whole (i.e., numbers with fractions). For example:

  • When you want to calculate distances in meters or feet.
  • When dealing with money or prices that require decimal points.
  • When measuring time, weight, or temperature.

Basic Floating-Point Numbers in Python

In Python, you can create a floating-point number just by writing a number with a decimal point:

# Example of float numbers
number1 = 3.14
number2 = -7.5
number3 = 0.99

print(number1)  # Output: 3.14
print(number2)  # Output: -7.5
print(number3)  # Output: 0.99

How to Create a Float

To create a float in Python, simply add a decimal point to a number:

my_float = 10.5  # This is a floating-point number
print(my_float)  # Output: 10.5

If you don’t include a decimal point, the number will be treated as an integer:

my_integer = 10  # This is an integer, not a float
print(my_integer)  # Output: 10

Mathematical Operations with Floats

You can use floats in mathematical operations just like integers. For example:

# Adding two floats
result = 3.5 + 2.5
print(result)  # Output: 6.0

# Subtracting two floats
result = 5.0 - 2.0
print(result)  # Output: 3.0

# Multiplying two floats
result = 4.0 * 2.5
print(result)  # Output: 10.0

# Dividing two floats
result = 10.0 / 4.0
print(result)  # Output: 2.5

Comparing Floats

You can compare floats using operators like ==, !=, <, >, <=, and >=:

# Comparing float numbers
print(3.14 == 3.14)  # Output: True
print(3.14 != 2.5)   # Output: True
print(2.5 < 5.0)     # Output: True

Special Floats

Python has some special floating-point values:

  • Infinity (float('inf')) is a very large number.
  • Negative Infinity (float('-inf')) is a very small number.
  • NaN (Not a Number) represents undefined or unrepresentable values (like 0/0).

Here is how you use them:

# Using Infinity and NaN
positive_infinity = float('inf')
negative_infinity = float('-inf')
nan_value = float('nan')

print(positive_infinity)  # Output: inf
print(negative_infinity)  # Output: -inf
print(nan_value)          # Output: nan

How Floats Are Stored in Memory

Internally, floating-point numbers are stored using a method called IEEE 754. This method is very efficient but sometimes leads to small errors when comparing numbers, especially after many operations. This is why you might find tiny differences in some calculations.

For example:

# A common issue with floating-point precision
result = 0.1 + 0.2
print(result)  # Output: 0.30000000000000004 (not exactly 0.3)

This happens due to how computers represent numbers in binary.

Type Casting: Converting Between Types

If you have a float and you want to convert it into an integer (a number without a decimal point), you can use the int() function. Similarly, if you want to convert an integer into a float, you can use the float() function.

# Converting float to integer
my_float = 7.9
my_integer = int(my_float)  # Removes the decimal part
print(my_integer)  # Output: 7

# Converting integer to float
my_integer = 7
my_float = float(my_integer)
print(my_float)  # Output: 7.0

Important Notes

  1. Rounding: You can round a float to a specific number of decimal places using the round() function. my_float = 5.6789 rounded_value = round(my_float, 2) # Rounds to 2 decimal places print(rounded_value) # Output: 5.68
  2. Large Floats: Python can handle very large floats, but they may be represented in scientific notation when they are too big. large_float = 1234567890.1234567 print(large_float) # Output: 1234567890.1234567