#๐ Overflow error
32 messages ยท Page 1 of 1 (latest)
@inner prawn
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.
it's simply impossible with that data type, try using the decimal module instead
!d decimal.Decimal.sqrt
sqrt(context=None)```
Return the square root of the argument to full precision.
if you know that it's a perfect square, try math.isqrt
why does it work only for perfect squares?
well it'd also work for non-perfect squares (isqrt is just 'integer sqrt')
you'd just get some numerical imprecision
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
math.sqrt works on floats and so it'd not work on ints this big. Use math.isqrt.
That's precisely the problem, actually - this integer is too big to be represented as a float, so the conversion fails.
(the biggest non-infinite float is about 1e+308, whereas this int has something like 10k digits.)
Correct
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
>>>
You are just bottle necking by the variable type you are using
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
I got it!!!
print a req set of decimal points if x is not a square
You'd need to use variable-precision arithmetic - so, thedecimalmodule.
If math.sqrt does convert it to a value that too large to be stored you need to use a different module
Yess
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}") ```
large double (float) value
decimals are not floating-point at all, that's kind of the point of them
Does this answer your question @inner prawn
No problem, happy we could help!
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.