problem with floats and calculations
breamoreboy at yahoo.co.uk
Sun Feb 14 12:41:45 CET 2010
[fix top posting]
> Karsten Goen <karsten.goen at googlemail.com> wrote:
>>> hey all,
>>> I got a problem with floats and calculations. I made an mini-application
>>> you get random questions with some science calculations in it
>>> So the user can type in his result with the values given by random
>>> And the user value is compared against the computer value... the problem
>>> that the user input is only 2 numbers behind the '.' so like 1.42, 1.75
>>> here is the example:
>>> without decimal it would be very inaccurate. decimal is very accurate
>> when I
>>> have to compare d with users calculations from a,b,c,var.
>>> But when I ask the user what is "a" the result gets inaccurate when
>>> with the same values given before (b,c,d,var).
>>> Maybe anyone can help me with this problem, I don't want to generate for
>>> possible user input a single formula. And also it should be possible for
>>> computer, my calculator at home does the same and is much smaller and
>> d = (a * b)/ (c * var)
>> d = Decimal(d).quantize(Decimal('0.01'))
>> By quantizing d, the above equality does not hold any longer. You've got
>> to drop that line (your calculator doesn't quantize either).
>> Stefan Krah
Karsten Goen wrote:
> also this doesn't help, there are still errors in the accuracy. Isn't
> a perfect way to do such calculations?
Please read the following and see if it helps. Also search the python
users mailing list for something like floating point accuracy, you'll
get plenty of hits.
More information about the Python-list