+ 22

Why are there such inaccuracies in cosine and sinus?

I do not know if anyone but me noticed that the cosinus and sinus function makes rounding errors. Is this problem only with Python? And how can you handle this? https://code.sololearn.com/cmqG6prORUvu/?ref=app

16th Apr 2019, 5:10 PM
N. Grpp
4 Réponses
+ 10
Some people say that you should never check floating point types for equality with ==. Instead you can check if they are "almost" equal: almost_equal = lambda m, n: abs(m-n)<=1e-6 print(cos(pi/4) == sin(pi/4)) # False print(almost_equal(cos(pi/4), sin(pi/4))) # True
16th Apr 2019, 5:34 PM
Anna
Anna - avatar
+ 16
Thanks, Anna, for your answer. I searched a bit on the internet. With round() there is also a suitable solution: https://code.sololearn.com/cZIckb6D9gU1/?ref=app
16th Apr 2019, 5:46 PM
N. Grpp
+ 9
Floating points numbers ain't 100% accurate. You can check out this code https://code.sololearn.com/cvKUsg91j1vQ/?ref=app
24th Oct 2019, 6:34 PM
BlackRose Mike
BlackRose Mike - avatar
+ 2
Anna You never said yourselves, just said some people.😀 Are you among them? BTW, you've openly said you never understood the purpose of named lambdas before and you used them here…😂 (Just to type less here and different in actual code?)
16th Apr 2019, 6:11 PM
Emoji FanBoy
Emoji FanBoy - avatar