python中math库的常用方法_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的绝对值

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39569894/article/details/110023489

智能推荐

Spring参数校验--List<E>类型参数校验_spring 校验list_天涯拓荒的博客-程序员宅基地

1.遇到的问题:今天开发接口,遇到请求参数固定为List<CommonConfigBean>类型的,如下。发现参数校验失效。 @RequestMapping(method = RequestMethod.POST) public void insertCommonConfig(@RequestBody @Validated List<CommonConfigBean> list){ int result=commonConfigService._spring 校验list

静态数据成员与非静态数据成员的区别_Z_J_T的博客-程序员宅基地

数据成员:静态数据成员是类的一部分,为类的所有实例共享(静态区); 非静态数据成员,类的每个实例都有一份拷贝(动态区)。静态数据成员的访问:静态数据成员是类的一部分,在产生任何实例之前已经存在,通过 类名::静态成员变量名 访问。函数成员(都在代码区):静态函数成员与非静态函数成员都为类所有,对象并不存在函数的拷贝。静态成员函数和非静态成员函数的根本区别在于非静态函数由对象名.或者对象指针->_静态数据成员

Mybatis自定义插件_mybatis自定义插件例子_codingCoge的博客-程序员宅基地

Mybatis插件原理和执行流程对四大对象和插件有了一定了解后,这里我就开始简单实现一个自定义插件,来拦截四大对象,实现拦截器功能。 步骤:1. 编写Interceptor的实现类2. 使用@Intercepts注解完成插件签名 说明插件的拦截四大对象之一的哪一个对象的哪一个方法3. 将写好的插件注册到全局配置文件中编写Interceptor的实现类(dao层)以及注解声..._mybatis自定义插件例子

laydate 在vue中使用_在vue中使用laydate插件_彭哲夫的博客-程序员宅基地

今天碰到一个同学问了laydate在vue中的使用问题,因为以前在项目中也用过laydate,也算有一些了解,所以在这里做一个简单的测试官方网址:http://www.layui.com/laydate/GitHub地址:https://github.com/sentsin/laydate/测试文件:新建vue的webpack模板1、首先是组件里import引用这里是在components中的he..._vue3 使用 laydate.js

PID调试_金_大虾的博客-程序员宅基地

Kp: 比例系数 ----- 比例带(比例度)P:输入偏差信号变化的相对值与输出信号变化的相对值之比的百分数表示 (比例系数的倒数)T:采样时间Ti: 积分时间Td: 微分时间2. PID常用口诀: 参数整定找最佳,从小到大顺序查 先是比例后积分,最后再把微分加 曲线振荡很频繁,比例度盘要放大 曲线漂浮绕大湾,比例度盘往小扳 曲线偏离回复慢,积分时间往下降 曲线波..._pid调试

随便推点

《Neo4j 3.x入门经典》已正式出版,各大网店均有售!_download.3xmodeling.com_gobitan的博客-程序员宅基地

《Neo4j 3.x入门经典》已正式出版,各大网店均有售! 大家好,由我参与翻译(第二译者)的《Neo4j 3.x入门经典》已拿到批号正式出版,在各大网店均有售!京东链接:https://item.jd.com/41497370796.html天猫链接:https://detail.tmall.com/item.htm?id=587002748115本书是张帜老师主..._download.3xmodeling.com

L-BFGS剖析_golden_xuhaifeng的博客-程序员宅基地

机器学习中经常利用梯度下降法求最优解问题,通过大量的迭代来得到最优解,但是对于维度较多的数据,除了占用大量的内存还会很耗时,L-BFGS算法是一种在牛顿法基础上提出的一种求解函数根的算法,下面由简入深尽量用简洁的语言剖析算法的本质。一.牛顿法 解决函数求根问题 f(x)函数在x1点的导数,是该函数在x1点的切线的斜率y/x,f(x1)=f(x1)/(x1-x2) ,x1-x3=f(x..._l-bfgs

用vue仿贝壳地图找房功_贝壳找房画图圈区域实现方法_这样不好吧!的博客-程序员宅基地

用vue仿贝壳地图找房功能主要实现:通过baidu-map 实现鼠标滚动缩放地图级别,同时控制行政区划气泡的展示和隐藏;放大地图,行政区划气泡消失,同时展示改行政区划下面的房源信息,我这里展示的是垃圾站点信息;预览地址:http://61.160.234.13:8081/#/baiduMapindex.vue<template> <baidu-map class="map" :center="center" :zoom="zoom" :min-zoom="mi_贝壳找房画图圈区域实现方法

2020年7月 :国产数据库名录和产品信息一览_安可数据库目录_普通网友的博客-程序员宅基地

国产数据库列表,此表不断更新,以期反映国产数据库的发展动态,如有反馈提醒,请在本文末留言。注意,文章所列产品,将以产品名称为顺序,所以序列可能随时更改。我们还将这些产品和墨天轮的国产数据库排行相关联,以期为读者和关注国产数据库生态的朋友,提供充分的信息。目录国产数据库列表国产数据库大事记国产数据库列表序号产品名称产品类别厂商相关资讯1AISWare AntDB分布式关系型数据库亚信科技控股有限公司2AliSQL开源云数据库RDS阿里云计算有限公司._安可数据库目录

php爬虫实时更新天气,Python爬虫获取中国天气网天气预报数据[2018-06-12更新]_慈悲饮的博客-程序员宅基地

实时天气显示建议用Domoticz内置的DarkSky。天气预报只能自己获取。此脚本获取中国天气网七日预报,设备需要自建虚拟硬件,添加虚拟设备,设备类型选择Text文本。效果:屏幕快照 2017-06-06 11.55.25.jpg (29.5 KiB) 查看 30878 次Python2.x代码:CNWeather.py代码: 全选#!/usr/bin/python# -*- coding: U...

推荐文章

热门文章

相关标签