serso / android-calculatorpp

Android Calculator
421 stars 201 forks source link

Calculation gives wrong output #181

Open abrahammurciano opened 7 years ago

abrahammurciano commented 7 years ago

When performing this calculation, −10.5+18.4 Whose real answer is 7.9, the calculator returns this: 7.899999999999999

GuilhermeRossato commented 7 years ago

This calculator saves numbers in binary data.

You cannot store 7.9 in today's IEEE794 Standard for Floating-Point accurately:

7.9 in base 10 is 111.11100110011001100110011... (repeating endlessly) in base 2

Assuming a x32 processor, your mobile stores the following data: 0 10000001 111110011001100110011001100110011001100110011001101 which in base 2 (binary), means: 1.111110011001100110011001100110011001100110011001101*10^(-10) (1 signal bit, 8 expoent bits, 23 floating point precision binary digits = 32 bits)

This is a rounded number, if you try to normalize it back to decimal, you get 7.899999....

The further you are from zero, the bigger this deviation.

Fixing this is possible and relatively easy, but would mean much slower operations, which is not ideal, nor is it the objective of this tool.

Source: am computer engineering student.

wischi-chr commented 5 years ago

but would mean much slower operations,

It wouldn't be much slower, even a nokia 3310 could do that calculation without noticeable delay.

which is not ideal, nor is it the objective of this tool.

You mean a calculator don't have to be mathematically accurate for simple rationals. The user shouldn't have to worry about implementation details. This calculator has a great UI but a pretty bad and sloppy implementation. Not only is it mathematically wrong, many cases are not even correct according to IEEE754 (see issue #170)

Sadly it also looks like that this project has been abandoned.

(by the way you've a typo in your comment it's not IEEE794 but 754)