Python: Floating Point vs. Decimal

Share on facebook
Share on google
Share on twitter
Share on linkedin
A VPN is an essential component of IT security, whether you’re just starting a business or are already up and running. Most business interactions and transactions happen online and VPN

In our Arduino projects (programmed in C/C++), we often use a Raspberry Pi for collecting data, and the Python programming language.

Math with floating point is inherently risky, because a decimal number does not store exactly in binary.

in python,
1.1+2.2 = 3.3000000000000003

not the 3.3 you might expect.

we can force the result we are looking for by importing the Decimal module, and setting the desired precision (2 in this example):

>>>from decimal import *
>>> getcontext().prec = 2
>>> Decimal(1.1)+Decimal(2.2)

Arduino doesn’t have a Decimal function, it uses floating point, but you can work around this by using Fixed Point Arithmetic

Steve Spence

Steve Spence

Leave a Reply

Leave a Comment

Your email address will not be published. Required fields are marked *