简体   繁体   中英

Python behaviour when approaching 0.0

I have written a Python program that needs to run until the initial value gets to 0 or until another case is met. I wanted to say anything useful about the amount of loops the program would go through until it would reach 0 as this is necessary to find the solution to another program. But for some reason I get the following results, depending on the input variables and searching the Internet thus far has not helped solve my problem. I wrote the following code to simulate my problem.

temp = 1
cool = 0.4999 # This is the important variable
count = 0
while temp != 0.0:
        temp *= cool
        count += 1
        print(count, temp)
print(count)

So, at some point I'd expect the script to stop and print the amount of loops necessary to get to 0.0. And with the code above that is indeed the case. After 1075 loops the program stops and returns 1075. However, if I change the value of cool to something above 0.5 (for example 0.5001) the program seems to run indefinitely. Why is this the case?

The default behavior in many implementations of floating-point arithmetic is to round the real-number arithmetic result to the nearest number representable in floating-point arithmetic.

In any floating-point number format, there is some smallest positive representable value, say s . Consider how s * .25 would be evaluated. The real number result is .25• s . This is not a representable value, so it must be rounded to the nearest representable number. It is between 0 and s , so those are the two closest representable values. Clearly, it is closer to 0 than it is to s , so it is rounded to 0, and that is the floating-point result of the operation.

Similarly, when s is multiplied by any number between 0 and ½, the result is rounded to zero.

In contrast, consider s * .75 . The real number result is .75• s . This is closer to s than it is to 0, so it is rounded to s , and that is the floating-point result of the operation.

Similarly, when s is multiplied by any number between ½ and 1, the result is rounded to s .

Thus, if you start with a positive number and continually multiply by some fraction between 0 and 1, the number will get smaller and smaller until it reaches s , and then it will either jump to 0 or remain at s depending on whether the fraction is less than or greater than ½.

If the fraction is exactly ½, .5• s is the same distance from 0 and s . The usual rule for breaking ties favors the number with the even low bit in its fraction portion, so the result is rounded to 0.

Note that Python does not fully specific floating-point semantics, so the behaviors may vary from implementation to implementation. Most commonly, IEEE-754 binary64 is used, in which the smallest representable positive number is 2 −1074 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM