-2

I am using the function math.modf which separates the integer and decimal part of a number as follows:

decimal_part, integer_part = math.modf(x)

Where x is a decimal number.

An example for a small number is as follows:

x = 1993.0787353515625
decimal_part = 0.0787353515625, integer_part = 1993.0

But when I work with very large numbers the following happens:

x = 6.797731511223558e+44
decimal_part = 0.0, integer_part = 6.797731511223558e+44

In this case it doesn't save the result in the decimal part and appears 0.0. And the same happens for numbers up to 300 digits. But when the number x has at least 360 digits, the following error appears:

OverflowError: int too large to convert to float.

I would like to save the decimal part of large numbers of at least 300 digits without overflowing the register where the decimal part is stored. And I would like to avoid the error in numbers with more than 360 digits: "OverflowError: int too large to convert to float".

How can I solve it?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Tucker
  • 1
  • 1
    But `6.797731511223558e+44` doesn't have decimal part, it works correctly – Gamopo Apr 06 '21 at 11:52
  • When you convert an integer to a float, why do you want to split it again to its integer part and its decimal part? An integer didn't have a decimal part to begin with. – mkrieger1 Apr 06 '21 at 11:56

2 Answers2

1

Due to the extra information it has to save, float needs more space than int. But let's break this down:

The number 6.797731511223558e+44 is an integer, which means it has no decimal part, so it will always return 0.0 as decimal.

If you are providing an integer with 300+ digits, it will still be an integer, so the decimal part will still be 0.0, so there's no need to use the function. You are getting that error because you are passing a very large int that is converted to float to give you the result, but this is not necessary since you already know the result.

On the other hand, if you use the function with a float, the function doesn't have problems casting float to float, so it won't show the error.

Gamopo
  • 1,600
  • 1
  • 14
  • 22
  • The number 6.797731511223558e+44 should be a number with a decimal part because it is the result of dividing a number by another number. But python doesn't save the decimal result and 0.0 appears. When we introduce small numbers in the function, it saves the decimal part. – Tucker Apr 09 '21 at 18:20
-1

The number 6.797731511223558e+44 should be a number with a decimal part because it is the result of dividing a number by another number. But python doesn't save the decimal result and 0.0 appears. When we introduce small numbers in the function, it saves the decimal part.

Tucker
  • 1