math.log() 导致“ValueError:数学域错误”[重复]

问题描述 投票:0回答:2

这是一个频率到 MIDI 音符转换器,但我似乎无法让数学正常工作。具体来说,使用

math.log()
函数。大多数情况下,这会产生 69.0 的输出,但如果我输入低于 440 的任何数字,它通常会输出“ValueError:数学域错误”。我应该如何修复它?

    #d=69+12*log(2)*(f/440)
    #f=2^((d-69)/12)*440
    #d is midi, f is frequency

    import math
    f=raw_input("Type the frequency to be converted to midi: ")
    d=69+(12*math.log(int(f)/440))/(math.log(2))
    print d`
math python-2.x frequency midi
2个回答
1
投票

这是因为Python 2 使用整数除法。任何低于 440 的值都将计算为 0,然后传递给

math.log()

>>> 500/440
1
>>> 440/440
1
>>> 439/440
0
>>> math.log(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

最简单的方法是通过将此行放在文件顶部来启用 Python 3 风格,即所谓的真正除法:

from __future__ import division

现在 Python 2 将按照您的预期运行:

>>> 439/440
0.9977272727272727
>>> math.log(439/440)
>>> math.log(439/440)
-0.0022753138371355394

作为替代方案,您可以将股息和/或除数转换为浮点数:

d=69+(12*math.log(int(f)/440.0))/(math.log(2))

d=69+(12*math.log(float(f)/440))/(math.log(2))

0
投票

如果是Python 2,看起来整数除法会使括号中的乘积不精确。尝试除以 440.0。

© www.soinside.com 2019 - 2024. All rights reserved.