0

While performing multiplication of three numbers with and without braces in python shows different answers.

>>>from math import sqrt
>>> a=100000000 #my input
>>> b=sqrt(3)/4
0.4330127018922193
>>> c=b*a*a   #print with format specifier 
>>> print("{:.2f}".format(c))
4330127018922193.50
>>> d=b*(a*a)
>>> print("{:.2f}".format(d))
4330127018922193.00

Can someone please explain me why the precision changes with brackets?

Deepan
  • 430
  • 4
  • 13
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – norok2 May 29 '20 at 11:17
  • No, it doesn't explain. For me the problem is why with braces the precision is **.00** and without is precision **.50** – Deepan May 29 '20 at 11:19
  • is `b = sqrt(3)/4` showing all the digits of its float representation? – norok2 May 29 '20 at 11:29
  • `print("{:.20f}".format(b))` it gives output **0.43301270189221929829** – Deepan May 29 '20 at 11:52
  • So, why do you think that the inaccuracies that accumulate over operations in floats (as the answers in the linked question nicely explain) are not causing your issues? – norok2 May 29 '20 at 13:13
  • Yeah may be it's one of the reasons. Actually I asked this question because it's a Daily Challenge in Skillrack in which it takes up my time in solving this question. The question is find the area of equilateral triangle of given input with exactly two precision. – Deepan May 30 '20 at 05:03

0 Answers0