TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Floating Point Numbers in Python
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python BasicPython Tutorials

Floating Point Numbers in Python

Last updated: Sep 28, 2023 6:28 pm
By Harsh S.
Share
11 Min Read
Floating Point Numbers in Python
SHARE

Floating-point numbers are a fundamental data type in Python used to represent real numbers. They represent a wide range of values, from very small numbers to very large numbers. For example, the number 3.14159265359 is a floating-point number.

Contents
1. What Are Floating-Point Numbers?Representation in Python2. Floating-Point PrecisionLimited PrecisionMachine Epsilon3. Floating-Point ArithmeticBasic OperationsSpecial Values (NaN and Infinity)Handling Rounding Errors4. Common Issues and PitfallsLoss of PrecisionComparing Floating-Point NumbersAvoiding Common Pitfalls5. Best Practices for Working with Floating-Point NumbersUsing Decimal Data TypeNumerical LibrariesFloating-Point Number ErrorsError Handling Strategies6. Practical ExamplesFinancial CalculationsScientific ComputationsGeospatial ApplicationsConclusion

Floating-point numbers in Python are available as the float type. To create a floating-point number, you can simply type in the number, including the decimal place.

Introduction to Floating-Point Numbers

In this comprehensive guide, we’ll explore floating-point numbers in Python, covering topics such as their representation, precision, arithmetic, common issues, and best practices.

1. What Are Floating-Point Numbers?

Floating-point numbers are a way to represent real numbers in a binary format. They are essential for performing mathematical operations involving real-world quantities with fractional parts. Floating-point numbers consist of three components: sign, exponent, and mantissa (or coefficient).

Example:

# Floating-point representation in Python
float_number = 3.14159
print(float_number)  # Output: 3.14159

Representation in Python

Python uses the IEEE 754 standard to represent floating-point numbers. The float data type in Python represents a 64-bit double-precision floating-point number. It provides a wide range of representable values but has limitations in precision.

2. Floating-Point Precision

Limited Precision

It is important to note that floating-point numbers have limited precision. This means that we can not represent all real numbers precisely in the binary floating-point format.

Subsequently, some decimal fractions cannot be represented exactly, leading to rounding errors.

Example:

# Limited precision example
result = 1.0 / 3.0
print(result)  # Output: 0.3333333333333333

Machine Epsilon

Machine epsilon (eps) is the smallest positive number that, when added to 1.0, results in a value greater than 1.0. It measures the precision of the floating-point system. In Python, you can access the machine epsilon using sys.float_info.epsilon.

Example:

import sys

# Machine epsilon in Python
epsilon = sys.float_info.epsilon
print(epsilon)  # Output: 2.220446049250313e-16

3. Floating-Point Arithmetic

Basic Operations

Python supports basic arithmetic operations with floating-point numbers, such as addition, subtraction, multiplication, and division. Understanding how these operations work and their limitations is crucial for correct computations.

Example:

# Basic floating-point arithmetic
a = 0.1
b = 0.2
c = a + b
print(c)  # Output: 0.30000000000000004

The above result shows that the number 0.1 cannot be represented exactly in the computer’s floating-point format. The computer has to round the number slightly, which results in a small rounding error.q

Special Values (NaN and Infinity)

Floating-point numbers can represent special values like NaN (Not-a-Number) and infinity. NaN is used to represent undefined or unrepresentable results, while infinity represents overflow or division by zero.

Example:

# Special values in floating-point
infinity_pos = float('inf')
infinity_neg = float('-inf')
nan = float('nan')

print(infinity_pos)  # Output: inf
print(infinity_neg)  # Output: -inf
print(nan)           # Output: nan

Below is an example of a float overflow.

# Overflowing a float to infinity
large_number = 1.7e308  # A very large positive floating-point number

result = large_number * 2  # Doubling the already large number

print(result)  # Output: inf

In this code, we start with a large positive floating-point number (large_number) and then multiply it by 2. The result exceeds the maximum representable finite floating-point value, so Python represents it as “inf,” indicating positive infinity.

This demonstrates how a floating-point operation can result in “inf” due to overflow when the value exceeds the representational capacity of the floating-point format.

Handling Rounding Errors

Rounding errors can occur when doing operations with floating-point numbers. It’s important to be aware of these errors and implement strategies to address them.

Example:

# Rounding error example
result = 0.1 + 0.2 - 0.3
print(result) 

# Output: 5.551115123125783e-17 (Close to zero but not exactly zero)

4. Common Issues and Pitfalls

Loss of Precision

Loss of precision can occur in the following cases:

  • When converting between different number data types or
  • When performing operations that involve large and small numbers with significantly different magnitudes.

Example:

# Loss of precision when adding large and small numbers
large_number = 1.0e20
small_number = 1.0
result = large_number + small_number
print(result)  # Output: 1.0e20 (Small number effectively lost)

Comparing Floating-Point Numbers

Comparing floating-point numbers for equality can be challenging due to rounding errors. It’s important to use tolerance-based comparisons instead of exact equality.

Example:

# Comparing floating-point numbers with tolerance
a = 0.1 + 0.2
b = 0.3
tolerance = 1e-10
are_equal = abs(a - b) < tolerance
print(are_equal)  # Output: True (within tolerance)

Avoiding Common Pitfalls

To write robust code, you should be aware of common challenges related to floating-point arithmetic, such as associativity issues and unexpected behavior in corner cases.

Example:

# Associativity issue
result1 = (0.1 + 0.2) + 0.3
result2 = 0.1 + (0.2 + 0.3)
print(result1 == result2)  # Output: False (Associativity issue)

5. Best Practices for Working with Floating-Point Numbers

Using Decimal Data Type

Python’s decimal module provides arbitrary-precision decimal arithmetic. It’s a valuable tool for applications that require high precision, such as financial calculations.

Example:

from decimal import Decimal, getcontext

# Using the Decimal data type
getcontext().prec = 28  # Set precision to 28 decimal places
a = Decimal('0.1')
b = Decimal('0.2')
c = a + b
print(c)  # Output: 0.3

Numerical Libraries

Consider using dedicated numerical libraries like NumPy or SciPy for scientific and engineering computations. These modules offer greater support for floating-point operations and numerical algorithms.

Example:

import numpy as np

# Using NumPy for efficient array operations
array1 = np.array([0.1, 0.2, 0.3])
array2 = np.array([0.3, 0.2, 0.1])
result = np.dot(array1, array2)
print(result)  # Output: 0.1

Floating-Point Number Errors

There are a few common errors that can occur when working with floating-point numbers in Python. One common error is division by zero. If you try to divide a floating-point number by zero, you will get a ZeroDivisionError.

Another common error is overflow. Overflow occurs when the result of a floating-point operation is too large to be represented by the float type. If overflow occurs, you will get an OverflowError.

Finally, you can also get a ValueError if you try to convert a string that does not represent a valid floating-point number to a floating-point number using the float () function.

Error Handling Strategies

Implement error-handling techniques to detect and handle exceptional cases, such as division by zero or NaN results, to prevent unexpected program behavior.

Example:

# Error handling for division by zero
numerator = 1.0
denominator = 0.0
try:
    result = numerator / denominator
except ZeroDivisionError:
    result = float('inf')  # Handle division by zero
print(result)  # Output: inf

6. Practical Examples

Financial Calculations

Floating-point numbers are commonly used in financial applications for interest calculations, currency conversions, and risk assessments. Understanding precision and avoiding rounding errors is crucial in this domain.

Example:

# Financial calculation example
principal = 1000.0
interest_rate = 0.05
years = 5
future_value = principal * (1 + interest_rate) ** years
print(future_value)  # Output: 1276.2820372317057

Scientific Computations

Scientific simulations and data analysis often rely on floating-point arithmetic. Ensuring correct results requires careful consideration of precision and error handling.

Example:

# Scientific computation example
import math

result = math.sqrt(2.0)
print(result)  # Output: 1.4142135623730951

Geospatial Applications

Geospatial applications, such as navigation, mapping, and surveying, rely heavily on floating-point numbers to represent coordinates and geometric data.

For example, the Global Positioning System (GPS) uses floating-point numbers to represent the latitude and longitude of a position on Earth.

Example:

# Calculate the distance between two GPS coordinates
from geopy.distance import vincenty

# Define the two coordinates
start_point = (52.5200, 13.4050)
end_point = (48.8566, 2.3522)

# Calculate the distance using the Vincenty formula
distance = vincenty(start_point, end_point).meters

# Print the distance in kilometers
print(distance / 1000)

Floating-point numbers are also used in geospatial calculations to find the area of a polygon and the distance between two points on a curved surface.

Here are some more tips:

  • Use a high-precision library, such as NumPy or SciPy.
  • Be aware of the limitations of floating-point arithmetic and take steps to avoid errors.
  • Use a stable coordinate system throughout your application.

By following these tips, you can ensure that the results are accurate and authentic.

Conclusion

Floating-point numbers are the basic building block of arithmetic in Python. Understanding their representation, precision limitations, best practices, and error handling is crucial for writing reliable and accurate code.

They are a powerful tool for representing a wide range of values. However, it is important to be aware of the potential errors that can occur when working with floating-point numbers in Python.

We hope that by following the guidelines in this tutorial, you can avoid these errors and use floating-point numbers effectively in your Python programs.

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

Selenium Python Extent Report Guide

10 Python Tricky Coding Exercises

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
Loading
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Harsh S. Avatar
By Harsh S.
Follow:
Hello, I'm Harsh, I hold a degree in Masters of Computer Applications. I have worked in different IT companies as a development lead on many large-scale projects. My skills include coding in multiple programming languages, application development, unit testing, automation, supporting CI/CD, and doing DevOps. I value Knowledge sharing and want to help others with my tutorials, quizzes, and exercises. I love to read about emerging technologies like AI and Data Science.
Previous Article How to split a sting in Python Python Split String By a Delimiter, Newline, RegEx, Or Character Count
Next Article Get the last element of a list in Python Python Get Last Element in List

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x