1

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

  • 1
    Do the answers to this [question](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) help at all? – quamrana Oct 01 '22 at 12:10

2 Answers2

1

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
bitflip
  • 3,436
  • 1
  • 3
  • 22
1

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

khaled koubaa
  • 836
  • 3
  • 14