#๐Ÿ”’ Overflow error

32 messages ยท Page 1 of 1 (latest)

inner prawn
#

x is a perfect square
How do i remove this error

coral vortexBOT
#

@inner prawn

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

inner prawn
round rampart
#

it's simply impossible with that data type, try using the decimal module instead

#

!d decimal.Decimal.sqrt

coral vortexBOT
chilly turtle
#

if you know that it's a perfect square, try math.isqrt

inner prawn
chilly turtle
glacial wyvern
#

Al you have to do is clarify it as a double not an int

#

You are getting error because int or float is too long

novel oracle
#

math.sqrt works on floats and so it'd not work on ints this big. Use math.isqrt.

novel oracle
#

(the biggest non-infinite float is about 1e+308, whereas this int has something like 10k digits.)

chilly turtle
#

ints can pretty much grow arbitrarily, but floats have a max that you can check with

>>> import sys
>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
>>> sys.float_info.max
1.7976931348623157e+308
>>>
glacial wyvern
#

You are just bottle necking by the variable type you are using

inner prawn
#

So if it's capped at that level
is there no other way around to print a req set of decimal points if x is not a square

glacial wyvern
#

I got it!!!

novel oracle
glacial wyvern
#

If math.sqrt does convert it to a value that too large to be stored you need to use a different module

glacial wyvern
#

decimal module

#

Correct

#
from decimal import Decimal, getcontext

# Set the precision for the decimal calculations
getcontext().prec = 50  # You can adjust the precision as needed

# Define a large double (float) value using Decimal
large_value = Decimal('1.2345678901234567890123456789012345678901234567890e+50')

# Calculate the square root of the large double value
square_root = large_value.sqrt()

# Print the result
print(f"The square root of {large_value} is {square_root}") ```
novel oracle
#

large double (float) value
decimals are not floating-point at all, that's kind of the point of them

glacial wyvern
#

Does this answer your question @inner prawn

inner prawn
#

Thanks

glacial wyvern
coral vortexBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.