-4
from math import cos 
x = 2
while x != cos(x):
    x = cos(x)
print (x)

This is the code I had my base level 2015 mac Air running. I waited about 30-40secs at which point my laptop began turning into a hot plate. I didn't get the answer. Is this normal? I didn't think it'd take that much effort to run it, especially after reading how people can easily run 30+ tabs , Xcode, movie streams simultaneously on their Airs.

I ran it using the terminal if it helps.

Specs are 4gb ram / 128gb sdd/ i5-5250u(?)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
bonslery
  • 3
  • 2
  • 1
    A busy CPU is a hot CPU: work requires energy, generates heat. The loop never terminates and keeps the CPU pointlessly busy. It is not that it is 'horrible' at running the loop; it is that the poor laptop is pushing its thermal envelope trying to accomplish the requested (albeit useless) task. – user2864740 Aug 25 '15 at 19:57
  • 1
    Probably running into some floating point issues. – Reinstate Monica -- notmaynard Aug 25 '15 at 19:59
  • possible duplicate of [Why Are Floating Point Numbers Inaccurate?](http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Reinstate Monica -- notmaynard Aug 25 '15 at 20:00
  • On my Ubuntu PC, this loop ran for 92 iterations before it found equality. However, the symptons described are that of an infinite loop keeping the CPU busy. – dsh Aug 25 '15 at 20:01
  • Try printing out the value of `x` at each iteration and see what it looks like. – Reinstate Monica -- notmaynard Aug 25 '15 at 20:02
  • Thanks guys! I totally understand the down votes haha.. I probably should have just brushed up on my floating point math. It's weird because I ran it on tutorial points online python IDE and the answer was provided instantaneously ah well. I'll admit, poor choice of words though. :- ( I love this macbook air. I'll change the title. – bonslery Aug 25 '15 at 20:02
  • Move your `print(x)` inside the loop, and watch to see whether it's oscillating around a few values differing only in the last few digits. – DSM Aug 25 '15 at 20:03
  • So now the question is, why does it work on some platforms and not others? – Reinstate Monica -- notmaynard Aug 25 '15 at 20:03
  • 1
    IIRC, in CPython, `math.cos` merely calls the cosine function of the underlying C platform. I expect different OSes to have different C compilers, so it doesn't surprise me that the behavior varies. – Kevin Aug 25 '15 at 20:07

2 Answers2

0

Since floating-point math is imprecise (try doing 0.1 + 0.2 in your interpreter), there's a good chance that you'll never get an equality. Even if it was, wouldn't it take a large amount of iterations for there to be an exact equality (it might be an infinite series; I don't know)? What you should do is check if the difference is bigger than some very small number, like 0.000000000001:

while abs(x - cos(x)) > 0.000000000001:

This will ensure that it eventually stops.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Thanks! That worked perfectly. Jeez there's so many nuances with this stuff it's crazy! Sorry all if this question was a bit pointless... As you can tell i'm a beginner – bonslery Aug 25 '15 at 20:08
0

Better is this:

from math import cos

def diff(lhs, rhs, delta):
    return abs(lhs - rhs) > delta

curr = 2.0
prev = 0.1    
while diff(curr, prev, 0.00000001):
    curr, prev = cos(curr), curr

This way you only compute cos() once per loop.

quamrana
  • 37,849
  • 12
  • 53
  • 71