Python初学者,为什么数学返回NaN?

时间:2015-09-17 09:24:29

标签: python math

我已经获得了I级的代码,但我无法弄清楚为什么我会回归NaN。

我之前从未使用过数学模块。

以下是代码:

import random as rndm
import math
# helper function to start and restart the game
def get_guesses(low, high):
    global guesses
    #x = low - high + 1
    #guesses = math.log(x, 2)
    guesses = math.log(low - high + 1, 2)
    return guesses

def new_game():
    global secret_number, guesses, first
    if first:
        first = False
        secret_number = rndm.randrange(0, 100)
        guesses = get_guesses(0, 99)
    print guesses
    print secret_number

1 个答案:

答案 0 :(得分:3)

 public function insert($data_to_db){
        $this->db->insert('table_name', $data_to_db);
    }

您传递了math.log(low - high + 1, 2) low = 0,因此等式低于零:high = 99

负数的对数位于complex number plane0 - 99 + 1 = -98模块不支持(仅处理实数)。

所以你得到的是一个值错误:

math

如果您想使用复数,可以使用cmath模块:

>>> math.log(0 - 99 + 1, 2)
Traceback (most recent call last):
  File "<pyshell#51>", line 1, in <module>
    math.log(0 - 99 + 1, 2)
ValueError: math domain error

但我怀疑这是你真正想要的;)