python math.copysign()方法

math.copysign()语法

copysign(x, y, /)

python源码中对math模块中的copysign()方法的介绍:

    Return a float with the magnitude (absolute value) of x but the sign of y.
    
    On platforms that support signed zeros, copysign(1.0, -0.0)
    returns -1.0.

即,copysign()方法可以用来返回一个以参数x的绝对值为数值,以参数y的值的正负为符号的浮点数。


copysign()参数

参数描述
xreal number 以x的绝对值指定浮点数返回值中的数字部分
yreal number 指定返回值的符号

返回值

python浮点数float类型值。


copysign()实例代码

>>> import math
>>> math.copysign(-1,-2)
-1.0
>>> math.copysign(-5,0)
5.0
>>> math.copysign('6','-3') #参数应当是real number
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be real number, not str

全栈后端 / Python库 :









Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.