가능한 중복 : 코드에서
python - decimal place issues with floats
Python float equality weirdness파이썬 이상한 또한 버그
내가 떠있는 변수 percentage
이 아래. 나는 그것이 number
에 도달하면 10,000
에 도달하도록 percentage
이 올라가도록 .01
으로 설정하도록 설정했습니다.
# Tries to find a number that when squared and 5%'ed is still a square.
import math
print("Script has started:\n")
percentage = .1
number = 1
while number != -1:
number = number + 1
num_powered = number ** 2
num_5per = num_powered * percentage
num_5per_sqrt = math.sqrt(num_5per)
num_list = list(str(num_5per_sqrt))
dot_location = num_list.index(".")
not_zero = 0
for x in num_list[dot_location + 1:]:
if x != "0":
not_zero = 1
break
if not_zero == 0:
print("*********************************")
print("Map :",number)
print("Safe-Area :",num_5per_sqrt)
print("Percentage :",percentage)
print("*********************************")
input()
if number > 10000:
number = 0
percentage = percentage + .01
print(percentage)
출력 : 당신은 부동 소수점 숫자를 사용하고 representation error을 경험 한
0.11
0.12
0.13
0.14
0.15000000000000002 # Here is where it goes crazy
0.16000000000000003
이것은 매우 일반적입니다. 컴퓨터의 부동 소수점 숫자는 기본 2이며 기본 10에 자연스러운 숫자를 정확하게 나타낼 수 없습니다. –
이것은 버그가 아닙니다. 부동 소수점 변수가 작동하는 방식의 결과입니다. http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html을 참조하십시오. 중복으로 투표를 종료합니다. –
오, 감사합니다. 죄송합니다. 검시되었지만 중복 된 것을 보지 못했습니다. – RandomPhobia