Reputation: 23
I am trying to solve this question. My solution (as written) gives Runtime error
import math
from decimal import Decimal
for i in range(int(input())):
v,r=map(int,input().split())
x=9.8
ans=math.degrees(math.asin(r*x/(v*v))/2)
format_float = "{:.7f}".format(ans)
print("Case #"+str(i+1)+":",format_float)
but when I use x = Decimal(9.8)
instead - it is accepted.
What is happening here? Also when or when not to use Decimal.
Time limit: 60 seconds. Memory limit: 1 GB. 1 ≤ T ≤ 4500 , 1 ≤ V ≤ 300 , 1 ≤ D ≤ 10000 , It is guaranteed that each test case will be solvable.
Upvotes: 0
Views: 115
Reputation: 27588
You can just solve all three million allowed cases with both Decimal
and float
and show where and how they differ:
import math
from decimal import Decimal
def f(v, r):
try:
x=9.8
ans=math.degrees(math.asin(r*x/(v*v))/2)
format_float = "{:.7f}".format(ans)
return format_float
except:
return 'error'
def d(v, r):
try:
x=Decimal(9.8)
ans=math.degrees(math.asin(r*x/(v*v))/2)
format_float = "{:.7f}".format(ans)
return format_float
except:
return 'error'
for v in range(1, 301):
for r in range(1, 10001):
expect = d(v, r)
result = f(v, r)
if result != expect:
print(f'{v=} {r=} Decimal:{expect} float:{result}')
Output:
v=21 r=45 Decimal:45.0000000 float:error
v=42 r=180 Decimal:45.0000000 float:error
v=63 r=405 Decimal:45.0000000 float:error
v=84 r=720 Decimal:45.0000000 float:error
v=119 r=1445 Decimal:45.0000000 float:error
v=126 r=1620 Decimal:45.0000000 float:error
v=161 r=2645 Decimal:45.0000000 float:error
v=168 r=2880 Decimal:45.0000000 float:error
v=175 r=3125 Decimal:45.0000000 float:error
v=231 r=5445 Decimal:45.0000000 float:error
v=238 r=5780 Decimal:45.0000000 float:error
v=245 r=6125 Decimal:45.0000000 float:error
v=252 r=6480 Decimal:45.0000000 float:error
Let's look at the smallest one v=21 r=45
more closely:
x = 9.8
v = 21
r = 45
print(r*x/(v*v))
Output:
1.0000000000000002
So, due to float
imprecision, you happen to produce a number larger than 1, which asin
then crashes on.
One way to avoid that in this case is to stay in integerland until the final division:
r*98/(10*v*v)
Using this, you get the same results as with Decimal
in all allowed cases.
Upvotes: 1