Floating point numbers in Python (7/100 Days of Python)

Martin Mirakyan
3 min readJan 8, 2023

--

Day 7 of the “100 Days of Python” blog post series covering floating-point numbers, arithmetic operations, and essential functions like abs(), sqrt(), etc.

Floating-point numbers, also known as “floats”, are numbers with decimal points. In Python, you can use floats to represent numbers that are not integers (although there are other number types as well, which we’ll cover later). Here are some examples of floats in Python:

x = 10.5
pi = 3.14
e = 2.71828

You can perform arithmetic operations with floats just like you would with integers. For instance:

x = 10.5
pi = 3.14

sum = x + pi
difference = x - pi
product = x * pi
quotient = x / pi

Essential math functions

In addition to the basic arithmetic operations, Python also provides some built-in functions for working with floats. For example, you can use the round function to round a float to a specified number of decimal places:

x = 10.56789
rounded = round(x, 2) # rounded = 10.57
print(rounded) # Will print 10.57

The Python math library provides a range of functions for performing mathematical operations. Here are some of the most commonly used functions:

abs(x): Returns the absolute value of a number. For example, abs(-10) would return 10.

ceil(x): Returns the smallest integer that is greater than or equal to a number. For example, ceil(3.2) would return 4.

floor(x): Returns the largest integer that is less than or equal to a number. For example, floor(3.8) would return 3.

round(x): Rounds a number to the nearest integer. For example, round(3.2) would return 3, and round(3.8) would return 4. You can also specify the number of decimal places to round to by passing an additional argument. For example, round(3.14159, 2) would return 3.14.

sqrt(x): Returns the square root of a number. For example, sqrt(9) would return 3.0.

exp(x): Returns e (the base of natural logarithms) raised to the power of a number. For example, exp(1) would return 2.718281828459045.

log(x): Returns the natural logarithm of a number. For example, log(1) would return 0.0. You can also specify the base of the logarithm by passing an additional argument. For example, log(100, 10) would return 2.0.

These are just a few examples of the functions that are available in the math library. There are many more that you can use for a wide range of mathematical operations.

To use these functions, you will need to import the math library in your Python code. Here’s an example of how to import the library and use the sqrt function:

import math

x = 9
y = math.sqrt(x)

print(y) # prints 3.0

Accuracy with floating-point numbers in our computers

It’s important to note that floating-point arithmetic is not always completely accurate, due to the way that computers represent decimal numbers internally. This can sometimes lead to unexpected results. For example:

x = 0.1 + 0.2
print(x) # prints 0.30000000000000004

In this case, the result is not exactly 0.3, but a very close approximation. In most cases, this level of precision is more than sufficient, but it’s something to be aware of if you need to perform very precise calculations.

Computerphile has an amazing video explaining this in more detail (Floating-Point Numbers — Computerphile):

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

No responses yet