Issue
I wrote this function to avoir typing on a calculator and make my homework faster but something very strange happened :
def G(x):
return 1 - x ** 2 - (1-x) ** 2
G(1/5) gives 0.31999999999999984
But it is clearly 25/25 - 1/25 - 16/25 with is 8/25 = 0.32
Solution
You can use the decimal
module as a workaround.
The decimal module provides support for fast correctly-rounded decimal floating point arithmetic. It offers several advantages over the float datatype.
from decimal import Decimal;
print(float(G(Decimal(1)/Decimal(5))))
0.32
Answered By - Corralien Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.