Jack Lantern
Jack Lantern

Reputation: 11

Cant add 1 to Exponential number in python

I have a number in exponential format and want to add 1 to the number. Why is python printing the original number?

num = 1.1920928955078124e+16
print(num)     # == 1.1920928955078124e+16
print(num + 1) # == 1.1920928955078124e+16
print(num + 2) # == 1.1920928955078126e+16

However when I add 2, it works and returns original number + 2

Thanks for help

Upvotes: 1

Views: 98

Answers (2)

bitflip
bitflip

Reputation: 3684

Your number is represented by a float

type(1.1920928955078124e+16)  # <class 'float'>

You can cast it to int using int(num).

Floating point numbers also have limited precision. If you need more precision, you can use decimal.Decimal

from decimal import Decimal
Decimal('1.1920928955078124e+16') + 1  # Decimal('11920928955078125')

Or a library like numpy

import numpy as np
np.int64(1.1920928955078124e+16) + 1  # 11920928955078125

Upvotes: 1

Khaled Koubaa
Khaled Koubaa

Reputation: 547

scientific notation is always represented as a float. try:

num = 1.1920928955078124e+16 #float

print(int(num))  #Convert that float to int
# 11920928955078124
print(int(num) + 1)
# 11920928955078125

Upvotes: 1

Related Questions