使用pow时的Python数学域错误 [英] Python math domain error when using pow

查看:66
本文介绍了使用pow时的Python数学域错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

exp = 1.79
def calc(t):
    return pow(t - 1, exp)

t 的输入值范围是0到1(例如0.04).这段代码引发了数学域异常",但是我不确定为什么.

The input values of t range from 0 to 1 (e.g. 0.04). This code throws a "math domain exception," but I'm not sure why.

我该如何解决?

推荐答案

如果 t 的范围是0到1,那么 t-1 的范围是-1到0.既不能通过内置的 pow 也不可以通过 math.pow 将负数提高到小数幂.

If t ranges from 0 to 1, then t - 1 ranges from -1 to 0. Negative numbers cannot be raised to a fractional power, neither by pow builtin nor math.pow.

这篇关于使用pow时的Python数学域错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆