0

I have some expressions on a spreadsheet and trying to convert them to python, but I am having different results on a particular one:

Spreadsheet =0+(1-0)*(1-COS(0.08726646259971647))^5 == 0.0

Python 0 + (1-0) * (1-math.cos(0.08726646259971647))**5 == 7.978947260671464e-13 (???)

I know that the values should be 0, what am i doing wrong in python? Thank you

Python 2.7.3(in an host application)

user1765661
  • 585
  • 1
  • 7
  • 21
  • 2
    `7.978947260671464e-13` is 0.0000000000007978947260671464, which is very close to zero. It is possible that the spreadsheet value is not really zero either, but is just displayed as zero due to the cell formatting. – BrenBarn Nov 11 '14 at 23:24
  • Long story short: Based on the available precision to represent floating point values, your answer is approximately zero. – Cory Kramer Nov 11 '14 at 23:26
  • 1
    `cos(0.08726646259971647)` is sufficiently far from `1.0` that the non-zero answer is likely to be the correct one. I don't think this is due to rounding error. (At least not in Python) – Mark Ransom Nov 11 '14 at 23:28
  • While that other question will tell you why this happens, note that you cannot solve it with decimals as cosinus has no infinite precision (while decimals have). So you cannot get a decimal result for this equation. At some point, you need to accept the imprecise cosinus result and round. – poke Nov 11 '14 at 23:28
  • Thanks guys, how can i display/transform those values into 0.00001... ? – user1765661 Nov 11 '14 at 23:29
  • 1
    You can use [round](https://docs.python.org/3/library/functions.html#round). – poke Nov 11 '14 at 23:30
  • What makes you think the true answer should be 0.00001? It's actually much smaller than that. – Mark Ransom Nov 11 '14 at 23:30
  • hey Mark, for example, in the spreadsheet if i change the cos number to 0.29670597 the result is rounded to 0.00000028. How can I have the same "rounded" behavior in python? – user1765661 Nov 11 '14 at 23:44

0 Answers0