Python之math模块常用方法汇总

python中math模块常用的方法整理

ceil:取大于等于x的最小的整数值,如果x是一个整数,则返回x

copysign:把y的正负号加到x前面,可以使用0

cos:求x的余弦,x必须是弧度

degrees:把x从弧度转换成角度

e:表示一个常量

exp:返回math.e,也就是2.71828的x次方

expm1:返回math.e的x(其值为2.71828)次方的值减1

fabs:返回x的绝对值

factorial:取x的阶乘的值

floor:取小于等于x的整数值,如果x是一个整数,则返回自身

fmod:得到x/y的余数,其值是一个浮点数

frexp:返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围

fsum:对迭代器里的每个元素进行求和操作

gcd:返回x和y的公约数

hypot:如果x是不是无穷大的数字,则返回True,否则返回False

isfinite:如果x是正无穷大或负无穷大,则返回True,否则返回False

isinf:如果x是正无穷大或负无穷大,则返回True,否则返回False

isnan:如果x不是数字True,否则返回False

ldexp:返回x*(2**i)的值

log:返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)

log10:返回x的以10为底的对数

log1p:返回x+1的自然对数(基数为e)的值

log2:返回x的基2对数

modf:返回由x的小数部分和整数部分组成的元组

pi:数字常量,圆周率

pow:返回x的y次方,即x**y

radians:把角度x转换成弧度

sin:求x(x为弧度)的正弦值

sqrt:求x的平方根

tan:返回x(x为弧度)的正切值

trunc:返回x的整数部分

ceil

#取大于等于x的最小的整数值,如果x是一个整数,则返回x
ceil(x)
Return the ceiling of x as an int.
This is the smallest integral value >= x.
>>> math.ceil(4.01)
5
>>> math.ceil(4.99)
5
>>> math.ceil(-3.99)
-3
>>> math.ceil(-3.01)
-3

copysign

#把y的正负号加到x前面,可以使用0
copysign(x, y)
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.
>>> math.copysign(2,3)
2.0
>>> math.copysign(2,-3)
-2.0
>>> math.copysign(3,8)
3.0
>>> math.copysign(3,-8)
-3.0

cos

#求x的余弦,x必须是弧度
cos(x)
Return the cosine of x (measured in radians).
#math.pi/4表示弧度,转换成角度为45度
>>> math.cos(math.pi/4)
0.7071067811865476
math.pi/3表示弧度,转换成角度为60度
>>> math.cos(math.pi/3)
0.5000000000000001
math.pi/6表示弧度,转换成角度为30度
>>> math.cos(math.pi/6)
0.8660254037844387

degrees

#把x从弧度转换成角度
degrees(x)
Convert angle x from radians to degrees.
>>> math.degrees(math.pi/4)
45.0
>>> math.degrees(math.pi)
180.0
>>> math.degrees(math.pi/6)
29.999999999999996
>>> math.degrees(math.pi/3)
59.99999999999999

e

#表示一个常量
>>> math.e
2.718281828459045

exp

#返回math.e,也就是2.71828的x次方
exp(x)
Return e raised to the power of x.
>>> math.exp(1)
2.718281828459045
>>> math.exp(2)
7.38905609893065
>>> math.exp(3)
20.085536923187668

expm1

#返回math.e的x(其值为2.71828)次方的值减1
expm1(x)
Return exp(x)-1.
This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
>>> math.expm1(1)
1.718281828459045
>>> math.expm1(2)
6.38905609893065
>>> math.expm1(3)
19.085536923187668

fabs

#返回x的绝对值
fabs(x)
Return the absolute value of the float x.
>>> math.fabs(-0.003)
0.003
>>> math.fabs(-110)
110.0
>>> math.fabs(100)
100.0

factorial

#取x的阶乘的值
factorial(x) -> Integral
Find x!. Raise a ValueError if x is negative or non-integral.
>>> math.factorial(1)
1
>>> math.factorial(2)
2
>>> math.factorial(3)
6
>>> math.factorial(5)
120
>>> math.factorial(10)
3628800

floor

#取小于等于x的整数值,如果x是一个整数,则返回自身
floor(x)
Return the floor of x as an int.
This is the largest integral value <= x.
>>> math.floor(4.1)
4
>>> math.floor(4.999)
4
>>> math.floor(-4.999)
-5
>>> math.floor(-4.01)
-5

fmod

#得到x/y的余数,其值是一个浮点数
fmod(x, y)
Return fmod(x, y), according to platform C.  x % y may differ.
>>> math.fmod(20,3)
2.0
>>> math.fmod(20,7)
6.0

frexp

#返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
#2**e的值在这个范围内,e取符合要求的整数值,然后x/(2**e),得到m的值
#如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
frexp(x)
Return the mantissa and exponent of x, as pair (m, e).
m is a float and e is an int, such that x = m * 2.**e.
If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
>>> math.frexp(10)
(0.625, 4)
>>> math.frexp(75)
(0.5859375, 7)
>>> math.frexp(-40)
(-0.625, 6)
>>> math.frexp(-100)
(-0.78125, 7)
>>> math.frexp(100)
(0.78125, 7)

fsum

#对迭代器里的每个元素进行求和操作
fsum(iterable)
Return an accurate floating point sum of values in the iterable.
Assumes IEEE-754 floating point arithmetic.
>>> math.fsum([1,2,3,4])
10.0
>>> math.fsum((1,2,3,4))
10.0
>>> math.fsum((-1,-2,-3,-4))
-10.0
>>> math.fsum([-1,-2,-3,-4])
-10.0

gcd

#返回x和y的公约数
gcd(x, y) -> int
greatest common divisor of x and y
>>> math.gcd(8,6)
2
>>> math.gcd(40,20)
20
>>> math.gcd(8,12)
4

hypot

#得到(x**2+y**2),平方的值
hypot(x, y)
Return the Euclidean distance, sqrt(x*x + y*y).
>>> math.hypot(3,4)
5.0
>>> math.hypot(6,8)
10.0

isfinite

#如果x是不是无穷大的数字,则返回True,否则返回False
isfinite(x) -> bool
Return True if x is neither an infinity nor a NaN, and False otherwise.
>>> math.isfinite(100)
True
>>> math.isfinite(0)
True
>>> math.isfinite(0.1)
True
>>> math.isfinite("a")
>>> math.isfinite(0.0001)
True

isinf

#如果x是正无穷大或负无穷大,则返回True,否则返回False
isinf(x) -> bool
Return True if x is a positive or negative infinity, and False otherwise.
>>> math.isinf(234)
False
>>> math.isinf(0.1)
False

isnan

#如果x不是数字True,否则返回False
isnan(x) -> bool
Return True if x is a NaN (not a number), and False otherwise.
>>> math.isnan(23)
False
>>> math.isnan(0.01)
False

ldexp

#返回x*(2**i)的值
ldexp(x, i)
Return x * (2**i).
>>> math.ldexp(5,5)
160.0
>>> math.ldexp(3,5)
96.0

log

#返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
log(x[, base])
Return the logarithm of x to the given base.
If the base not specified, returns the natural logarithm (base e) of x.
>>> math.log(10)
2.302585092994046
>>> math.log(11)
2.3978952727983707
>>> math.log(20)
2.995732273553991

log10

#返回x的以10为底的对数
log10(x)
Return the base 10 logarithm of x.
>>> math.log10(10)
1.0
>>> math.log10(100)
2.0
#即10的1.3次方的结果为20
>>> math.log10(20)
1.3010299956639813

log1p

#返回x+1的自然对数(基数为e)的值
log1p(x)
Return the natural logarithm of 1+x (base e).
The result is computed in a way which is accurate for x near zero.
>>> math.log(10)
2.302585092994046
>>> math.log1p(10)
2.3978952727983707
>>> math.log(11)
2.3978952727983707

log2

#返回x的基2对数
log2(x)
Return the base 2 logarithm of x.
>>> math.log2(32)
5.0
>>> math.log2(20)
4.321928094887363
>>> math.log2(16)
4.0

modf

#返回由x的小数部分和整数部分组成的元组
modf(x)
Return the fractional and integer parts of x.  Both results carry the sign
of x and are floats.
>>> math.modf(math.pi)
(0.14159265358979312, 3.0)
>>> math.modf(12.34)
(0.33999999999999986, 12.0)

pi

#数字常量,圆周率
>>> print(math.pi)
3.141592653589793

pow

#返回x的y次方,即x**y
pow(x, y)
Return x**y (x to the power of y).
>>> math.pow(3,4)
81.0
>>> 
>>> math.pow(2,7)
128.0

radians

#把角度x转换成弧度
radians(x)
Convert angle x from degrees to radians.
>>> math.radians(45)
0.7853981633974483
>>> math.radians(60)
1.0471975511965976

sin

#求x(x为弧度)的正弦值
sin(x)
Return the sine of x (measured in radians).
>>> math.sin(math.pi/4)
0.7071067811865475
>>> math.sin(math.pi/2)
1.0
>>> math.sin(math.pi/3)
0.8660254037844386

sqrt

#求x的平方根
sqrt(x)
Return the square root of x.
>>> math.sqrt(100)
10.0
>>> math.sqrt(16)
4.0
>>> math.sqrt(20)
4.47213595499958

tan

#返回x(x为弧度)的正切值
tan(x)
Return the tangent of x (measured in radians).
>>> math.tan(math.pi/4)
0.9999999999999999
>>> math.tan(math.pi/6)
0.5773502691896257
>>> math.tan(math.pi/3)
1.7320508075688767

trunc

#返回x的整数部分
trunc(x:Real) -> Integral
Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
>>> math.trunc(6.789)
6
>>> math.trunc(math.pi)
3
>>> math.trunc(2.567)
2

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/258049.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

docker制作php5.4运行环境镜像

1.下载镜像 docker pull centos:7或者在控制面板下 2.运行centos7镜像的容器&#xff0c;edncenos7 是新生成的容器名称 ## --name 新名字 docker run -it --name edncenos7 c9a1fdca3387 /bin/bash3.在容器内下载php5.4等插件&#xff0c;以便提交成为新镜像 wget --no-ch…

亚信安慧AntDB数据库——助力5G计费核心替换,全面自主可控

数字经济时代&#xff0c;5G以更快、更丰富、更智能的连接方式服务于各行各业。AntDB数据库&#xff0c;源于亚信科技&#xff0c;自2008年起成功落地全国24个省份的中国移动、中国电信、中国联通和中国广电等运营商项目&#xff0c;为数字化服务和信息化基础建设提供支持。 在…

精选猫咪最爱:五款性价比超高的猫罐头品牌大PK!

新手养猫很容易陷入疯狂购买的模式&#xff0c;但有些品牌真的不能乱买&#xff01;现在的大环境不太好&#xff0c;我们需要学会控制自己的消费欲望&#xff0c;把钱花在刀刃上&#xff01;现在宠物市场真的很内卷&#xff0c;很多品牌都在比拼产品的数据和营养成分。很多铲屎…

大数据讲课笔记5.1 初探MapReduce

文章目录 零、学习目标一、导入新课二、新课讲解&#xff08;一&#xff09;MapReduce核心思想&#xff08;二&#xff09;MapReduce编程模型&#xff08;三&#xff09;MapReduce编程实例——词频统计思路1、Map阶段&#xff08;映射阶段&#xff09;2、Reduce阶段&#xff08…

STM32启动流程详解(超全,startup_stm32xx.s分析)

单片机上电后执行的第一段代码 1.初始化堆栈指针 SP_initial_sp 2.初始化 PC 指针Reset_Handler 3.初始化中断向量表 4.配置系统时钟 5.调用 C 库函数_main 初始化用户堆栈&#xff0c;然后进入 main 函数。 在正式讲解之前&#xff0c;我们需要了解STM32的启动模式。 STM32的…

透视数据:数据可视化工具的多重场景应用

数据可视化工具已经成为了许多领域中的重要利器&#xff0c;它们在各种场景下发挥着重要作用。下面我就以可视化从业者的角度简单谈谈数据可视化工具在不同场景下的应用&#xff1a; 企业数据分析与决策支持 在企业层面&#xff0c;数据可视化工具被广泛应用于数据分析和决策…

27jd网卡丢失IP地址问题追踪

一、问题描述及复现步骤 问题描述 启用network服务&#xff0c;关闭NetworkManager服务后&#xff0c;&#xff08;通过 ip a 查看&#xff09; em1网卡丢失IP地址 网络相关组件信息 glib-networking-2.58.0-7.ky10.x86_64 network-scripts-10.01-6.ky10.x86_64 dracut-…

MapReduce综合应用案例 — 电信数据清洗

文章目录 第1关&#xff1a;数据清洗 第1关&#xff1a;数据清洗 测试说明 平台会对你编写的代码进行测试&#xff1a; 评测之前先在命令行启动hadoop&#xff1a;start-all.sh&#xff1b; 点击测评后MySQL所需的数据库和表会自动创建好。 PhoneLog&#xff1a;封装对象 L…

Android定制ROM简介

Android定制ROM简介 这篇文章是为对自定义ROM、AOSP等词汇不太熟悉的技术爱好者和好奇的人写的。我希望通过向您介绍这个世界来开始博客写作。 在我们将注意力转向定制ROM之前&#xff0c;让我们先了解一些基础知识。 什么是操作系统&#xff1f; 维基百科对此的定义简洁而…

探讨二维半导体的概念、应用前景及其与传统半导体的差异

当探讨二维半导体时&#xff0c;我们置身于科技革新的前沿。这种材料以其纳米级薄度和独特电学性质区别于传统半导体&#xff0c;引发了科学界的广泛兴趣。本文将深入探讨二维半导体的概念、应用前景及其与传统半导体的差异。 什么是二维半导体&#xff1f; 二维半导体是由单…

计算机网络 网络层下 | IPv6 路由选择协议,P多播,虚拟专用网络VPN,MPLS多协议标签

文章目录 5 IPv65.1 组成5.2 IPv6地址5.3 从IPv4向IPv6过渡5.3.1 双协议栈5.3.2 隧道技术 6 因特网的路由选择协议6.1 内部网关协议RIP6.2 内部网关协议 OSPF基本特点 6.3 外部网关协议 BGP6.3.1 路由选择 6.4 路由器组成6.4.1 基本了解6.4.2 结构 7 IP多播7.1 硬件多播7.2 IP多…

0062-Java运算符

文章目录 1.运算符介绍2.算术运算符2.1 介绍2.2 细节说明 3.关系运算符(比较运算符)3.1 介绍3.2 细节说明 4.逻辑运算符4.1 介绍4.2 逻辑运算规则4.3 && 和 & 基本规则4.4 && 和 & 使用区别4.5 || 和 | 基本规则4.6 || 和 | 使用区别 5. ! 取反 基本规…

Logback简介与配置详解

在开发和维护Spring Boot应用程序时&#xff0c;一个强大而灵活的日志框架是至关重要的。Spring Boot默认集成了Logback&#xff0c;一个高性能的Java日志框架。本文将介绍如何配置Logback以满足你的日志记录需求。 Logback简介 官方网址&#xff1a;https://logback.qos.ch/ …

Kafka核心参数(带完善)

客户端 api Kafka提供了以下两套客户端API HighLevel(重点)LowLevel HighLevel API封装了kafka的运行细节&#xff0c;使用起来比较简单&#xff0c;是企业开发过程中最常用的客户端API。 而LowLevel API则需要客户端自己管理Kafka的运行细节&#xff0c;Partition&#x…

RocketMQ系统性学习-SpringCloud Alibaba集成RocketMQ以及消费收发实战

文章目录 Spring Cloud Alibaba 集成 RocketMQ 最佳实践集成依赖DashBoard消息收发实战 Spring Cloud Alibaba 集成 RocketMQ 最佳实践 SpringBoot 相对于 SSM 来说已经很大程度上简化了开发&#xff0c;但是使用 SpringBoot 集成一些第三方的框架&#xff0c;还是需要花费一些…

Node.js 工作线程与子进程:应该使用哪一个

Node.js 工作线程与子进程&#xff1a;应该使用哪一个 并行处理在计算密集型应用程序中起着至关重要的作用。例如&#xff0c;考虑一个确定给定数字是否为素数的应用程序。如果我们熟悉素数&#xff0c;我们就会知道必须从 1 遍历到该数的平方根才能确定它是否是素数&#xff…

搭建知识付费平台?明理信息科技为你提供全程解决方案

明理信息科技saas知识付费平台 在当今数字化时代&#xff0c;知识付费已经成为一种趋势&#xff0c;越来越多的人愿意为有价值的知识付费。然而&#xff0c;公共知识付费平台虽然内容丰富&#xff0c;但难以满足个人或企业个性化的需求和品牌打造。同时&#xff0c;开发和维护…

Python Pandas Excel/csv文件的保存与读取(第14讲)

Python Pandas Excel/csv文件的读取于保存(第14讲)         🍹博主 侯小啾 感谢您的支持与信赖。☀️ 🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔…

在 Kubernetes 上部署 Python 3.7、Chrome 和 Chromedriver(版本 114.0.5735.90)的完整指南

一、构建基础镜像 docker build -f /u01/isi/DockerFile . -t thinking_code.com/xhh/crawler_base_image:v1.0.2docker push thinking_code.com/xhh/crawler_base_image:v1.0.2 二、K8s运行Pod 三、DockerFile文件 # 基于镜像基础 FROM python:3.7# 设置代码文件夹工作目录…
最新文章