Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Double (Float) Data Type in Python

  • 07 Jul, 2025
  • Com 0
Double (Float) Data Type in Python

Python does not have a built-in double data type, but it does have a float type, which designates a floating-point number. It handles double precision by default, adhering to the IEEE 754 standard.

Creating a float (double) data type in Python

So, you can say that any floating-point number is stored as a double. The maximum value of a floating-point number is approximately 1.7976931348623157e+308.

In a general programming sense, A float (single precision) is 32 bits, and a double (double precision) is 64 bits.

If you need more precision, you can use the numpy.float128 or numpy.longdouble, depending on your system’s capabilities.

However, it’s important to note that numpy.float128 may not always provide more precision than numpy.float64, especially on systems where the maximum floating-point precision is 64-bit.

Declaring float

You can create a float using a decimal point, scientific notation, or converting from other types, like an integer.

decimal_float = 3.14  # Literal with decimal point
scientific_float = 2.5e3  # Scientific notation (2500.0)
conversion_float = float("6.28")  # Conversion from string

print(decimal_float)
# Output: 3.14

print(scientific_float)
# Output: 2500.0

print(conversion_float)
# Output: 6.28

# From integer
float_conversion = float(9)

print(float_conversion)
# Output: 9.0

The easiest way to declare a float is by using a literal with decimal point notation.

Use for scientific, financial, or ML tasks, but beware of precision errors.

You can see that we also used a float() function to convert an input integer to float.

Arithmetic operations

Float type supports basic arithmetic operations such as addition, subtraction, multiplication, division, modulus, and exponentiation.

floating_val1 = 1.9
floating_val2 = 2.1

# Addition
print(floating_val1 + floating_val2)
# Output: 4.0

# Subtraction
print(floating_val1 - floating_val2)
# Output: -0.20000000000000018

# Multiplication
print(floating_val1 * floating_val2)
# Output: 3.9899999999999998

# Division
print(floating_val1 / floating_val2)
# Output: 0.9047619047619047

# Exponentiation
print(floating_val1 ** 2)
# Output: 3.61

# Modulus
print(floating_val1 % floating_val2)
# Output: 1.9

# Floor Division
print(floating_val1 // floating_val2)
# Output: 0.0

Floating-point Precision

Floating-point Precision

When working with floating-point numbers, you cannot compare values directly using the “==” operator because it will not match because IEEE 754 cannot represent some decimals (e.g., 0.9) exactly.

float_one = 0.9 + 0.9 + 0.9

float_two = 0.27

print(float_one == float_two)

# Output: False

For exact comparison and precision, use decimal.Decimal.

decimal.Decimal

decimal.Decimal

If you use decimal.Decimal() function to create a floating value will precisely match with other floating values created by the same method.

from decimal import Decimal

floating_value = Decimal('0.3') + Decimal('0.3') + Decimal('0.3')

print(floating_value == Decimal('0.9'))

# Output: True

I would urge you to use decimal.Decimal for financial or exact decimal calculations.

Overflow and Underflow

Overflow and Underflow

Floating point numbers have a finite range. If you exceed the upper limit, it will become inf. If you go further below the lower limit, it will become 0.0.

import sys

print(sys.float_info.max)
# Output: 1.7976931348623157e+308

overflow_var = 1e308 * 10  # Overflow
print(overflow_var)
# Output: inf

underflow_var = 1e-324 / 10  # Underflow
print(underflow_var)
# Output: 0.0

For checking the inf value or NaN value, you can always use math.isinf() or math.isnan() methods.

Zero representation

Floats distinguish between +0.0 and -0.0, although they are compared as equal.

x = 0.0
y = -0.0

print(x == y)

# Output: True

String conversion error

You cannot convert a string into a float. If you try to do that, it will throw a ValueError.

data = float("StrangerThings")

print(data)

# ValueError: could not convert string to float: 'StrangerThings'

To handle this type of error, you can use the try/except mechanism.

try:
    x = float("StrangerThings")
except ValueError as e:
    print(e)

# Output: could not convert string to float: 'StrangerThings'

That’s all!

Post Views: 15
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Write Bytes to a File in Python
How to Delete a Variable in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend