Python中的ldexp()函数
在本文中,我们将了解如何使用ldexp()函数。这是数学库中的方法之一。
函数ldexp(first,second)取两个正数或负数的有效数字,并返回first*(2**second)的结果。让我们看一些例子。
示例
# importing the math library import math # using the function ldexp print(math.ldexp(1, 4)) print(math.ldexp(5, -4)) print(math.ldexp(-3, -1))
如果运行上面的代码,则将得到以下结果。
输出结果
16.0 0.3125 -1.5
如果将数字以外的参数传递给ldexp函数,则会出现错误。让我们来看一个例子。
示例
# importing the math library import math # using the function ldexp print(math.ldexp(1, '4'))
如果运行上面的代码,则将得到以下结果。
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in 3 4 # using the function ldexp ----> 5 print(math.ldexp(1, '4')) TypeError: Expected an int as second argument to ldexp.
结论
如果您对本文有任何疑问,请在评论部分中提及它们。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短