现在位置: 首页 > Python 3 教程 > 正文

Python math.copysign() 方法

Python math 模块 Python math 模块

Python math.copysign(x,y) 方法返回一个基于 x 的绝对值和 y 的符号 +/- 的浮点数。

Python 版本:2.6

语法

math.copysign() 方法语法如下:

math.copysign(x, y)

参数说明:

  • x -- 必需,数字。
  • y -- 必需,数字,返回将值具有该参数的符号 +/-

返回值

返回一个浮点值,由第一个参数的值和第二个参数的符号组成。

实例

以下实例返回第一个参数的值和第二个参数的符号:

实例

# 导入 math 包
import math

# 返回第一个参数的值和第二个参数的符号
print(math.copysign(4, -1))
print(math.copysign(-8, 97.21))
print(math.copysign(-43, -76))

输出结果:

-4.0
8.0
-43.0

Python math 模块 Python math 模块