Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different calculation result using Python

Tags:

python

I've got a weird case using Python to get the result of this calculation:

11.66 * 0.98 * 1.05 + 1.7 + 0.70 * 1.03

in Python the result that I got is 14.41914

but when my customer calculate it using their calculator and iPhone the result that they got is 14.8300842

so which is the correct result ? and what caused this calculation to have different result ? thanks

like image 435
user2469629 Avatar asked Nov 30 '22 19:11

user2469629


2 Answers

The correct result is the one Python gave you. Your customer is using a calculator that doesn't account for order of operations, or has used the calculator in such a way that order of operations information was discarded.

like image 133
user2357112 supports Monica Avatar answered Dec 09 '22 19:12

user2357112 supports Monica


What your customer seems to have done is this:

>>> (11.66*0.98*1.05 + (1.7+0.7))*1.03
14.830084200000002
>>>

whereas that expression in python:

>>> 11.66*0.98*1.05 + 1.7+0.7*1.03
14.419140000000001

Does the multiplies first:

>>> (11.66*0.98*1.05) + 1.7+(0.7*1.03)
14.419140000000001 

Its a very strong convention that multiplication is done first, but desk calculators (real and appy) have to work on the numbers as they are punched in, so might do different things.

like image 27
Spacedman Avatar answered Dec 09 '22 20:12

Spacedman