来自python的【数字Number总结/Math数字函数】

Number数字

在python的文档中,几个注意点:

  • Math 模块不支持复数的函数,复数的函数是使用cmath来进行计算。所以绝大部都是不支持复数的。
  • 在math模块下,除非显示的标注,否则返回浮点数.

number 定义

  • number用于定义数据,并且数据是不允许改变的。只能够重新开辟内存空间number是不可变数据。str也是不可变数据。
  • 变量被赋值的时候才会被创建
  • 删除变量del var1,varr2,可以同时删除多个,会删除内存空间,后续用到的将not defined

数值类型

  • 整型(int):无小数点,可正可负,也可以用十六进制、八进制、二进制来表示整数
  • 浮点型(float):整数和小数组成,可以是科学计数法
  • 复数(complex):实数和虚数组成,复数的实部和虚部均是浮点数。a+bj 或 complex(a,b)

在Python3中无Long型,python2,会有范围划分,并且long型数据带有后缀L,是将long整合到int类型中,当超过之后自动换算成高精度。
在很多语言中是有:整数分为short 、int 、long、; 浮点数分为float double

# 2 8 16 进制表示int类型整数
print(0xA0f)#2527
print(type(0xA0f))#<class int>
print(type(0o37))# int
print(type(0b10))#int

各进制表示

二进制:0b
八进制:0o
十六进制:0x

int取值范围/float取值范围等

  1. int 取值范围是-231 ~ 231-1,即-2147483648 - 2147483647 ,int占据4个字节,一个字节8位,也就是32位。每个int数据都可以用32位的二进制来表示。int取值范围详解链接中说的很详细啦。
  2. float取值范围,百度出态度答案 ,还需要再次寻找 后补

数字类型转换

  • 能够使用函数进行各种数据类型的转换
  • int(x):转换成一个整数
  • float(x):转换成浮点数
  • complex(x):转换成复数,虚部是0
  • complex(x,y):转换成复数 x+yj

int(x)转换定义

  • int(x) ,有两种形式。int(x)默认转换成十进制,int(x,base) 转换为base对应的进制。
  • int 转换后返回的是int类型
  • int() =0
  • int(x,base)将数字、字符串转换成int类型;如果x是一个数字,就使用
    x.int();如果x不是数字并且给予了第二个参数,那么第一个参数的类型应该是字符串、字节、字节数组
  • base的默认数值是10,取值范围是0,2~36;0表示转换为十进制。
class int(object)
 |  int(x=0) -> integer  # 返回的是int类型
 |  int(x, base=10) -> integer #返回的是int类型
 |  Convert a number or string to an integer, or return 0 if no arguments are given.  If x is a number, return x.__int__().  For floating point numbers, this truncates towards zero.
 # int(x)将数字、字符串转换成int类型,如果没有给予参数int() 返回0
 #如果x是一个数字,x.__int__() 
 |  If x is not a number or if base is given, then x must be a string,  bytes, or bytearray instance representing an integer literal in the given base.  The literal can be preceded by '+' or '-' and be surrounded by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
 # int(x,base) 如果x不是数字,并且基于了第二个参数,那么第一个参数是字符串、字节、字节数组,表示给定基数中的整数文字。参数可以是整数也可以是负数,进制数默认是十进制,取值方位是2-36.
 |  Base 0 means to interpret the base from the string as an integer literal. # base=0的意思就是将字符串转换成十进制整数
 |  >>> int('0b100', base=0)
 |  4

int(x)举例

  • int() 取整,不是四舍五入,是直接舍弃小数部分。也就是向0取整
  • 复数不能强制转换成int类型,复数是浮点数,但是也不能强制转换成浮点数can't convert complex to float/int
  • int() 只能转换纯数字的字符串为整,意味着不转换小数的字符串,否者报错invalid literal for int() with base 10: '12.3',并不会转换成ASCII码。
  • 二进制的组成只有0,1组成,也就是二进制编码。
  • 如果要进行二进制转换,也就是具有第二个参数的时候,x必须是字符串、字节、字节数组否则会报错哦TypeError: int() can't convert non-string with explicit base类型错误,无法转换非字符串类型的数据。
  • 如果要进行对应进制的转换,表示形式与进制要对应,即0x12用8进制表示就是会出错的哦!进制类型要与格式对应
  • 可以转换boolTrue=1;False=0; 注意首字母大写 不会作为变量哦!
  • 0b 0o 0x 分别对应二进制、八进制、十六进制,在使用的时候必须搭配base参数使用,否则会被人称字符串,base范围0~36
  • print(int('1'*2,2)) 注意字符串的重复输出,先重复再进行转换。
  • 不能转换罗马字符、unicode字符也不能转换
  • 如果又有数字又有字母,会报错,11a 没有如同js一样的隐式转换
#print(help(int))
print(int())#0
print(int(2))#2
print(int(2.1))#2
print(int(-2.1))#-2
print(int(2.6))#2
#print(int(1+2j)) can;t convert complex to int
#print(float(1+2j))

#字符
print(int('15'))#15
#print(int('12.3')) #invalid literal for int() with base 10: '12.3'
#print(int('s'))
#print(int(12,8))TypeError: int() can't convert non-string with explicit base
print(int('12',8))#10
print(int('121',8))#81
#print(int('0x12'))#ValueError: invalid literal for int() with base 10: '0x12'
# 这样会被误以为是字符 所以必须带进制
print(int('0x12',16))#18
#print(int('0x12',8))# 进制类型要对应
print(int('0o12',8))#10
print(int('0b11',2))#3
#print(int([0b10,0b11,0b01],2)) typearray??
#print(int(b'\x01\x02\x03')) 不懂 需要补
# 各种进制
print(int('12',3))#5
print(int('1234',36))#49360
print(int(True))#1
print(int(False))#0
# 注意字符串 * 代表多次输出
print(int('1'*2,2)) # 把二进制下的2转换成对应的10进制
#11=3
#在x进制的情况下 转换成十进制
print(int('1'*3,2)) #表示的是一共有3位  
#(111)= 1*2的平方+1*2的1次方+1*2的零次方

print(int('1'*64,2)) 
#表示一共有64个1 然后对应乘以2的次方再相加

print(int('三')) # 不能转换罗马字符哈哈哈

float(x)转换定义

  • float(x) 返回的是浮点数
  • 小数点具有精度,当超出电脑精度后会忽略,只读取前面的数字
  • 复数不能转换成浮点数
  • float只有一个参数,没有进制转换等功能。
  • 不能转换罗马字符等。也不能使用ASCII码转换字母
  • 不存在像js一样的隐式转换12q can’t convert string to flaot
class float(object)
 |  float(x) -> floating point number
 |  #返回浮点数
 |  Convert a string or number to a floating point number, if possible.
 # 转换字符串、数字转换成浮点数
 #

float(x)举例

#print(help(float))
print(float(1))#1.0
print(float(-2.1))#-2.1
#print(float(1+2j)) can't conver complex to float
#print(float('12',8))
print(float('12.1'))#12.1 int can't
#print(float('0o12')) could not convert string to float
#print(float('三'))
#print(float('12a'))
print(float(True))#1.0
print(float(False))#0.0

complex(x,y)定义

  • 返回的是复数形式,有两种形式。x是实部,y是虚部,并且都是浮点数;无论哪种形式,返回的都是复数形式。
  • complex(x) :x是数字,y是默认为0,输出形式x+0j
  • complex(x,y):输出形式x+yj
  • 小数整数都能够作为实部和虚部,字符串类型数据也可以哦;字符串类型会转换成浮点数。字符串不存在隐式转换
  • 如果第一个参数为字符串类型数字,那么不能存在第二个参数can;t take second arg if first is a string;字符串数据作为第一个参数的时候,可以作为纯实数,或者是复数形式 ,即'a+bj'的形式
  • 复数内部能够进行计算,complex(a,b) = a+b*j ,最后得出返回数据。j*j = -1 例子:print(complex(1,2j))#-1+0jj = 1+2j*j = 1-2 = -1
class complex(object)
 |  complex(real[, imag]) -> complex number
 |  #返回复数
 |  Create a complex number from a real part and an optional imaginary part.
 # 创建一个负数,有一个实部和可选的虚部
 |  This is equivalent to (real + imag*1j) where imag defaults to 0.
 # 这里相当于是 虚部默认等于0
 

complex(x)举例

在这里插入代#print(help(complex))
print(complex(1))#1+0j
print(complex(1,1.1)) #(1+1.1j)
print(complex('1.1'))# 纯数字的字符串会转换成float类型
print(complex('1.1+2j'))#1.1+2j
#print(complex('1.1',2))#can‘t take second arg if first is a string
print(complex('2'))#2+0j
#print(complex('2q')) value error
print(complex(1,2j))#-1+0jj =  1+2j*j = 1-2 = -1
print(complex(1+2,2j)) # 3-2 = 1+0j
#码片

运算时数据类型

  • 复数中的实部和虚部都是浮点数
  • 不同机器上浮点运算结果不一样,和精度有关系,/返回的是浮点数,// 得到的数据与数据类型有关,但凡有浮点数参与的计算都会返回浮点数
  • 不同类型的数混合运算时会将整数转换为浮点数

数字函数–math.ceil() 返回大于或等于的一个整数

  • math.ceil 定义 ,属于Math函数,写的时候,需要math.ceil(x);返回一个大于或者等于的整数,使用的时候需要import math
ceil(x)
Return the ceiling of x, the smallest integer greater than or equal to x. If x is not a float, delegates to x.__ceil__(), which should return an Integral value.
#返回x的上限(天花板),大于或等于x的整数。如果x不是浮点数,那么就使用x.__ceil__()方法,需要返回一个整数
  • 复数不能使用,字符串也不能使用
  • ceil意天花板,返回大于或者等于的整数。
import math
print(math.ceil(1))#1
print(math.ceil(2.1))#3
print(math.ceil(2.5))#3
# 不是四舍五入
print(math.ceil(-0.1))#0
print(math.ceil(-1.1))#-1
#print(math.ceil('2')) # must be real number not str
#print(math.ceil(1+2j))# cnat‘ conver complex to float
#print(math.ceil((1+2j)))# can’t

数字函数–math.exp() 求x的指数

  • math.exp 定义: 返回ex 指数
Return e**x  # 返回e**x
  • 需要导入import math 通过静态对象调用方法
  • x参数不能是字符串、复数,是int 和float类型
#exp
print(math.exp(2))
print(math.exp(2.1))
#print(math.exp('12')) must be real number not str
#print(math.exp(1+2j))can‘t convert complex to float
print(math.exp(-2))

数学函数-- math.fabs() 求绝对值

  • math.fabs(x)定义,需要import math
math.fabs(x)
Return the absolute value of x. # 返回x的绝对值
  • 返回的均为浮点数,只对int、float类型有效,复数、字符串无效
# fabs
print(math.fabs(1))#1.0 #
print(math.fabs(1.1))#1.1
print(math.fabs(-1.2))#1.2
#print(math.fabs('12.1'))# must be rral number not str
#print(math.fabs(1+2j))can't convert complex to float

math.fabs 和 abs的区别

abs() 是内置函数。 fabs() 函数在 math 模块中定义。

  • fabs() 函数只对浮点型跟整型数值有效。 返回的均为浮点数
  • abs() 还可以运用在复数中。返回以实际数据为准

数学函数–math.floor 返回小于或等于的整数

  • math.floor 定义,返回一个小于或者等于的整数,需要import math
Return the floor of x, the largest integer less than or equal to x. If x is not a float, delegates to x.__floor__(), which should return an Integral value.
# 返回最小或者等于x的整数,如果x不是浮点数,使用x.__floor__,它应该返回一个浮点数,也就是字符串可以使用?
  • floor是地板的意思,返回的是小于或者等于的整数;不存在四舍五入
  • 不能对复数、字符串使用,只能对int 、flaot是使用
# floor
print(math.floor(1.1))#1
print(math.floor(2.7))#2
print(math.floor(-3.5))#-4
print(math.floor(-0.1))#-1
#print(math.floor('12'))#must be ral number not str
#print(math.floor(1+2j))# can't convert complex to float

数字函数 – math.modf(x) 返回浮点数的小数和整数部分

  • math.modf(x) 定义,属于math函数,需要导入import math
  • 返回x的整数部分和小数部分,返回的均为浮点数,并且都带有符号。
  • 不能字符串数字哦
Return the fractional and integer parts of x. Both results carry the sign of x and are floats.
#返回x的小数部分和整数部分,两个部分都带有x的符号,并且都是浮点型。
#modf
print('modf')
print(math.modf(1))# 0.0 1.0
print(math.modf(2.1))# 0.1 18位 2.0
print(math.modf(-1.3))# -0.3000000...  -1.0
#print(math.modf('1.1')) must be real number
#print(math.modf(1+2j)) #cmath

数学函数 – math.copysign(x,y) x获取y的符号

  • math.copysign(x,y)定义:返回x的数字,符号由y决定。
  • 返回的都是浮点数
  • 不同平台上对于0、0.0、-0、-0.0的结果可能不一样
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.
#返回具有大小的x,但是x的符号取决于y。支持数据为0,
#math.copysign
print(math.copysign(1,2))#1.0
print(math.copysign(2,-23))#-2.0
#print(math.copysign(2,1+2j)) complex cant
#print(math.copysign(1,'23')) not str

数字函数 – math.factorial(x) 求阶乘

  • math.factorial(x),返回1-x的阶乘,x不能为负数以及浮点数,只能整数
Return x factorial. Raises ValueError if x is not integral or is negative.
#返回x的阶乘,如果x不是int类型,以及是负数的时候,会报错valueError
print(math.factorial(3))#6
print(math.factorial(4))#24
print(math.factorial(2.0))# 等于2
#print(math.factorial(2.3))# not 只能整数

数字函数 – math.fmod(x,y) 求模 求余

  • math.fmod(x,y) 定义:能够针对数据进行求余求模,主要是针对浮点数。需要引入import math,返回的是浮点数。
  • %对浮点数求余不精准,所以使用fmod计算浮点数。%则针对整数
  • math.fmod(x,y) 与 % 区别
    (1)math.fmod 返回的均为浮点数,%是根据数据返回,如果均是int类型,则返回int类型,其中一个数据为浮点数,则返回浮点数。
    (2)math.fmod(x,y) 结果的符号(+/-)与x相同;%符号与y相同。并且相同数据(负数)在计算下有区别。如下所示
    (3)math.fmod(x,y)的计算结果是 |x| %|y|;
    % :如果x,y符号(+/-)相同,结果|x| % |y|
    如果x,y符号不同,结果是 |y| - (|x| % |y|)
    但是要注意,当x与y符号不同的时候, |y| - (|x| % |y|),需要满足下面两个条件:
    1. |x|远小于|y|;
    2. x与y中至少有一个是浮点数(只有浮点数才会导致精度损失)。此时,导致(|x| % |y|)的值相对非常小(即等于|x|),而因为|y|相对又非常的大,加上计算机对浮点数计算精度的损失,会出现“|y| - (|x| % |y|)”的最终结果被截取成为“|y|”的奇怪现象,如下所示。
    下面例子哦!
# fmod
print(1%2)#1
print(10%3)#1
print(10.1%3)#1.099999999999..6
print(math.fmod(10,3))#1
print(math.fmod(10.1,3))#1.09999..6
print(math.fmod(100.02,2))
print(math.fmod(-100.01,3))#-1.01000...
print(math.fmod(100.01,-3))#1.01... 符号与x有关
print(-100.01 %3)#1.98
print(100.01%-3)#-1.98
# 100.3 % 3   -100.3%3  100.3%-3  -100.3%-3
print(math.fmod(100.3,3)) #1.299...72
print(100.3%3) #1。299...72
#计算结果相同
print(math.fmod(-100.3,3)) 
# -(|-100.3| % |3|)=-1.2999 约等于1。3 符号以x为准
print(-100.3%3)
# 符号不同 正负以y为准 |y| - (|x| % |y|) = 3-1.2999 =1.7..


# 例子转载,并未检验。
print('“%”求模运算符的异常情况:     -0.0000000001 % 10000000 =', 
      -0.0000000001 % 10000000, '(相当于结果直接等于第2个数了)')
# 采用fmod求模不会出现上述情况
print('采用fmod求模,正常:math.fmod(-0.0000000001, 10000000) = ', 
      math.fmod(-0.0000000001, 10000000), '(结果等于第1个数)')
print('“%”求模运算符的异常情况: 10   % -10000000000000000000000.0 =', 
      10 % -10000000000000000000000.0, '(相当于结果直接等于第2个数了)')
print('“%”求模运算符的异常情况:-10.0 %  10000000000000000000000   =', 
      -10.0 % 10000000000000000000000, '(相当于结果直接等于第2个数了)')
#非浮点数则不会出现上述情况了:
print('“%”求模运算符,正常:    10    % -10000000000000000000000   =', 10 % -10000000000000000000000)
print('“%”求模运算符,正常: , -10    %  10000000000000000000000   =', -10 % 10000000000000000000000)

数字函数 – math.frexp(x)/math.ldexp(x,i) 浮点数的可移植性

  • math.frexp(x) 的定义,x = m * 2e ; 根据x求出m、e,返回元祖(m,e),m范围:0.5 <= abs(m) < 1,m是浮点数,e是整数;使得浮点数进行拆分
  • math.frexp (x) 也就是把浮点数x拆分成(m,e) 元祖,后面可以使用ldexp合并。
  • math.ldexp(x,i) 是math.frexp(x) 的逆操作。返回一个浮点数
Return the mantissa and exponent of x as the pair (m, e). 
#返回x的尾数和指数作为配对(m, e)。
m is a float and e is an integer such that x == m * 2**e exactly.
# m 是浮点数,e是整数,x == m *(2**e)
 If x is zero, returns (0.0, 0), otherwise 0.5 <= abs(m) < 1. This is used to “pick apart” the internal representation of a float in a portable way.
 # 使用可移植的方式,表示一个浮点数,也就是把一个浮点数拆分成指数形式。
 # 如果x是0,返回(0.0,0);复奏

print(math.frexp(10)) #x = m * 2 **e (0.625,4)
print (math.frexp(100)) # 0.71825 ,7
print(math.frexp(10.2))
#print(math.frexp('12')) not str complex
 print(math.ldexp(0.625,4))#10.0

数字函数 – math.fsum(iterable) 迭代器的累加

  • math.fsum(iterable) 参数是一个可迭代的;返回可迭代的数据中的和。可迭代的:字符串、字典、元祖、列表;引入import math
  • 参数必须是可迭代的,不能print(math.fsum(1,2,3,4)) TypeError: fsum() takes exactly one argument (4 given);不能是字符串,也不是字符串类型数组,没有隐式转换
  • 返回的都是浮点数
  • math.fsum 的计算精度大于sum
Return an accurate floating point sum of values in the iterable. Avoids loss of precision by tracking multiple intermediate partial sums:
# 返回一个精确的浮点值和。通过跟踪多个中间部分和避免精度损失。
>>>
>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.9999999999999999
>>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0 # 精度更精准
# fsum(iterable)
#print(math.fsum(1,2,3,4)) TypeError: fsum() takes exactly one argument (4 given)
#print(sum(1,2,3,4)) typeError at most 2 arguments
print(math.fsum((1,2,3,4)))#10.0
#print(math.fsum(('1','2','3','4'))) must be real number not str
print(math.fsum([1,2,3,4,5]))#15.0
print(math.fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]))#1.0
print(sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]))#0.99999

数字函数 math.gcd(a,b) 求最大公约数

  • math.gcd(a,b) 定义,求a,d的最大公约数,返回的int类型,a,d需要是整数
  • math.gac(0,0) = 0
Return the greatest common divisor of the integers a and b. # 返回最大公约数
If either a or b is nonzero, then the value of gcd(a, b) is the largest positive integer that divides both a and b. gcd(0, 0) returns 0.
#如果a或b都是非0的,那么gcd(a, b)的值是同时除a和b的最大正整数。gcd(0,0)返回0
print(math.gcd(12,3))#3
#print(math.gcd(12.2,3)) TypeError: 'float' object cannot be interpreted as an integer
#print(math.gcd(12,'12')) str can't 

数字函数 – math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) a,b数据是否近似

  • math.isclose() 定义 :比较a、b两个参数是否接近
  • 算法:abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)。
  • NaN不接近任何数,inf、-inf是无限接近它本身
  • rel_tol:相对容差,ab之间能够存在的最大差异。默认是1e-9,确保至少9位小树相同。
  • abs_tol : 绝对容差,是ab之间能够存在的最小差异,无限接近于0最好,
  • 在使用的时候,注意格式,必须要rel_tol =… abs_tol.否则报错print(math.isclose(0.1,0.01,rel_tol=1))#true
  • 不懂有什么意义,后期补
Return True if the values a and b are close to each other and False otherwise.
#  如果a,b两个数字很接近,那么就返回True,否则返回false

Whether or not two values are considered close is determined according to given absolute and relative tolerances.
#两个值是否接近取决于给定的绝对公差和相对公差 也就是 abs_tol 和rel_tol

rel_tol is the relative tolerance – it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b. For example, to set a tolerance of 5%, pass rel_tol=0.05. The default tolerance is 1e-09, which assures that the two values are the same within about 9 decimal digits. rel_tol must be greater than zero.
# 相对公差 这是a和b之间的最大允许差异,相对于较大的a或b的绝对值,举例:要将公差设置为5%,传递rel_tol=0.05。rel_tol默认值是1e-09 它确保这两个值在大约9位小数内是相同的。rel_tol必须大于0

abs_tol is the minimum absolute tolerance – useful for comparisons near zero. abs_tol must be at least zero.
# 绝对公差:abs_tol是最小的绝对容差——对于接近于零的比较是有用的。abs_tol必须至少为零。
If no errors occur, the result will be: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol).
#如果没有错误发生,结果将是:abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)。
the IEEE 754 special values of NaN, inf, and -inf will be handled according to IEEE rules. Specifically, NaN is not considered close to any other value, including NaN. inf and -inf are only considered close to themselves.
#NaN不被认为接近任何其他值,包括NaN。inf和-inf只被认为是接近它们本身的。
print(math.isclose(0.1,0.01)) #false 9位相同
#print(math.isclose(0.1,0.01,0.1))# 最多两个位置???
print(math.isclose(0.1,0.01,rel_tol=0.1))#false
print(math.isclose(0.1,0.01,rel_tol=1))#true

数字函数 – math.isfinite(x) 是否有限

  • math.isfinite(x) 的定义 : 判断数字是否为有限的,有限数字返回true,无限数字返回false
Return True if x is neither an infinity nor a NaN, and False otherwise. (Note that 0.0 is considered finite.)
# 返回True,如果x是无限大、非数值返回false。注意0.0是被认定为有限
  • 返回的是bool类型
  • NaN、 正无穷、负无穷 是无限大,返回false

那么到达无限的边界值是多少,如何输出这个数值,需要补

数字函数 – math.isinf(x) 是否无穷

  • math.inf(x) 的定义:x是正无穷、负无穷返回true,无限的数字返回true
  • 返回的是bool类型
Return True if x is a positive or negative infinity, and False otherwise.
# 如果x是正无穷、负无穷 返回true,否则返回false

print(math.isinf(12))#false
# not str complex

数字函数 – math.isnan(x) 是否非数值

  • math.isnan(x) 定义:如果x是非数字,则放回true,否则返回false。
Return True if x is a NaN (not a number), and False otherwise.
但是也不能是字符串

数字函数 – math.trunc(x) 返回浮点数x的整数部分

  • math.trunc(x) : 返回x的整数部分modf是把整数小数输出为一个元组。,返回的是int的类型
Return the Real value x truncated to an Integral (usually an integer). Delegates to x.__trunc__().
#返回截断为整数的实值x。代表x.__trunc__ ()。
在这里插入代码片print(math.trunc(2))#2
print(math.trunc(2.3))#2
print(math.trunc(4.34))#4 ```

数字函数–math.log(x,[base])

  • math.log(x,[base]]),定义。,需要import math
With one argument, return the natural logarithm of x (to base e).
# 一个参数,将会返回以e为底的对数
With two arguments, return the logarithm of x to the given base, calculated as log(x)/log(base).
# 两个参数,将会以base的数据为底  是否有取值定义
  • 在计算中,取值需要符合区间范围 (a>0,且a不等于1) 报错:ValueError: math domain error
  • 返回的均为浮点数
  • 有两种参数,log(x) 与 log(x,[base]) ,base 为底,base默认为e;
  • 不允许log(x,e),e会被认为变量
  • base>0 且不等于1 ,与对数的取值区间有关
#log
print(math.log(2))
#print(math.log(-1)) 区间错误
print(math.log(2.3))
print(math.log(2,2))#1.0
#print(math.log(2,e))# 不能e
print(math.log(2,10))
print(math.log(2,3))

数字函数–math.log10(x)/math.log2(x)

  • math.log10(x) ,需要引入 import math,10为底
  • math.log2(x) ,2为底
Return the base-10 logarithm of x. This is usually more accurate than log(x, 10). 
# base是10为底,等同于log(x,10)
  • 返回的是浮点数,
  • 功能与math.log(x,10) 相同
print(math.log10(100))#2.0  lg100
print(math.log2(100)) #log2100

数字函数 – math.log1p(x)

  • math.log1p(x) = =ln(1+x), 这就是以e为底的计算。
Return the natural logarithm of 1+x (base e). The result is calculated in a way which is accurate for x near zero.

数字函数 – math.pow(x,y)/pow(x,y,[z])

  • math.pow(x,y) 定义,需要import math,返回的均为浮点数
  • pow(x,y[z])是内置函数,不需要引入math模块。返回的数据类型与x,y有关,当x,y均为int类型时候,返回int类型,以具体情况为准。
  • pow(1.0,x)或者,pow(x,0.0),无论x为0或者是NaN输出均为1.0;
  • 在math.pow(x,y),x为负数
  • 参数必须是数值类型,不能其他类型,比如字符串数字,复数不行,转换不了,使用cmath
  • math.pow(x,y),当x>0的时候,y不能为小数,必须为整数,否者提示valueError,不在定义域范围内。这个不是很理解,但是pow内置函数可以计算。
  • pow(x,y,z) z是进行求余数操作,等于x**y %z
  • 2.0 相等于2,其余一样,计算时候要注意
Return x raised to the power y. Exceptional cases follow Annex ‘F’ of the C99 standard as far as possible. In particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN. If both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises ValueError.
# 返回x的y次方。例外情况尽可能遵循C99标准的附件F。
#特别是pow(1.0, x)和pow(x, 0.0)总是返回1.0,返回的还是浮点数,不论x是0还是NaN
#如果x和y都是有限的,那么x是负的,而y不是整数,那么pow(x, y)是未定义的,并产生ValueError。

Unlike the built-in ** operator, math.pow() converts both its arguments to type float. Use ** or the built-in pow() function for computing exact integer powers.
#与内置的** 操作符不同。pow会将两个参数都转换为浮点数。使用**/pow来计算整数幂
#pow  math.pow
print(math.pow(1,2))#1.01
print(pow(1,2))#1
print(pow(1.1,1.1))#1.11...
print(math.pow(1.0,1))#1.0
print(math.pow(-2,-1))#-0.5
#print(math.pow(-3,-2.2)) valueError math domain error 取值范围错误
print(pow(-2,-1.2))# ? 内置函数可以返回复数
print(math.pow(-2.2,1))#-2.2
print(pow(1,2,3))#1**2%3

数字函数 math.sqrt()

  • math.sqrt(x) 定义,返回x的平方根,需要导入import math
  • 返回的均为浮点数
  • 负数没有平方根,根据定义域,否知valueError:math domain error
  • math.sqrt(0) = 0
math.sqrt(x)
Return the square root of x.
# sqrt
print('sqrt')
print(math.sqrt(1))#1.0
print(math.sqrt(1.1))
print(math.sqrt(23.23))#4.8
#print(math.sqrt(-123))# math.domain error
#print(math.sqrt('12')) not str

数学函数 – math.exp(x)

  • math.exp(x) 定义 : 计算ex 的答案。
  • 例子:math.exp(2)

数学函数 – math.expm1(x)

  • 定义math.expm1(x) 计算ex-1,比math.exp(x)-1 更精准,减少精度的损失。
  • 精度的损失在大数字下才会显示
Return e**x - 1. For small floats x, the subtraction in exp(x) - 1 can result in a significant loss of precision; the expm1() function provides a way to compute this quantity to full precision:
>>>
>>> from math import exp, expm1
>>> exp(1e-5) - 1  # gives result accurate to 11 places
1.0000050000069649e-05
>>> expm1(1e-5)    # result accurate to full precision
1.0000050000166668e-05


print(math.exp(1e-5)-1)
print(math.expm1(1e-5))
#1.0000050000069649e-05
#1.0000050000166668e-05

数字函数 – math.acos(x)/math.asin(x)/math.atan(x)

  • 分别返回arccosx(x) arcsin(x) acrtan(x) ,他们的取值必须符合x的定义域的值,否则报错:math domain error;
  • acrsinx定义域[-1,1],值域[-pi/2,pi/2]
  • acrcosx定义域[-1,1],值域[0,pi]
  • actranx定义域负无穷到正无穷,值域[-pi/2,pi/2]
  • actcotx定义域负无穷到正无穷值域[0,pi]
  • 返回的都是弧度
math.acos(x)
Return the arc cosine of x, in radians.

math.asin(x)
Return the arc sine of x, in radians.

math.atan(x)
Return the arc tangent of x, in radians.

三个的图像
在这里插入图片描述
在这里插入图片描述

数字函数 – acrtans2(y,x)

  • arctan2 与arctan 都用于求斜率。返回的均为弧度值。但是由于函数的周期性,一个数字有对应两个弧度等。如atan(-1),可能是45°也可能是225°。【相差180°的角具有相同的正切和斜率】;但是math.atan只能返回一个值,因为”0“;但math.atan2可以计算出两个值参考文件1参考文件2

  • atan2 = atan2(y2-y1,x2-x1) 所以,一般可以用于求两点之间的斜率,math.atan2(y2-y1,x2-x1) 即可。

数字函数 – math.sin(x) /cos(x)/tanx(x)

  • 图像,值域定义域,使用时,需要满足x的取值范围
    在这里插入图片描述
    在这里插入图片描述

数字函数 – math.hypot(x,y) 求距离

  • 即√(xx + yy)这是向量从原点到点(x, y)的长度。,原点到x,y坐标点的距离。
  • 返回浮点数
Return the Euclidean norm, sqrt(x*x + y*y). This is the length of the vector from the origin to point (x, y).

print(math.hypot(3,4)) # 5.0

数字函数 – math.degrees(x)/math.radians(x)

  • 弧度和角度转换
print(math.degrees(math.pi/2))#90.0
print(math.degrees(math.pi/3)) # 有精度问题 59.999
print(math.radians(90))  # 1.5...

数字函数 – 双曲线函数

  • 后补 不懂用途
math.acosh(x)
Return the inverse hyperbolic cosine of x.

math.asinh(x)
Return the inverse hyperbolic sine of x.

math.atanh(x)
Return the inverse hyperbolic tangent of x.

math.cosh(x)
Return the hyperbolic cosine of x.

math.sinh(x)
Return the hyperbolic sine of x.

math.tanh(x)
Return the hyperbolic tangent of x.

不知道干啥的函数

math.erf(x)
math.erfc(x)
math.gamma(x)
math.lgamma(x)

数字函数 – 常量

math.pi : π :3.14…
math.e : e : 2.718…
math.tau:
math.inf:正无穷
-math.inf:负无穷
math.nan:非数值


print(math.pi)#3.1415926..
print(math.e)#2.718
print(math.tau)#6.28
print(math.inf)#inf 无限大
print(-math.inf)#-inf
print(math.nan)#nan

需要补充的地方

 |  
 |  Methods defined here:
 |  
 |  __abs__(self, /)
 |      abs(self)
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __bool__(self, /)
 |      self != 0
 |  
 |  __divmod__(self, value, /)
 |      Return divmod(self, value).
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __float__(self, /)
 |      float(self)
 |  
 |  __floordiv__(self, value, /)
 |      Return self//value.
 |  
 |  __format__(...)
 |      float.__format__(format_spec) -> string
 |      
 |      Formats the float according to format_spec.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getformat__(...) from builtins.type
 |      float.__getformat__(typestr) -> string
 |      
 |      You probably don't want to use this function.  It exists mainly to be
 |      used in Python's test suite.
 |      
 |      typestr must be 'double' or 'float'.  This function returns whichever of
 |      'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the
 |      format of floating point numbers used by the C type named by typestr.
 |  
 |  __getnewargs__(...)
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __int__(self, /)
 |      int(self)
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mod__(self, value, /)
 |      Return self%value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __neg__(self, /)
 |      -self
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __pos__(self, /)
 |      +self
 |  
 |  __pow__(self, value, mod=None, /)
 |      Return pow(self, value, mod).
 |  
 |  __radd__(self, value, /)
 |      Return value+self.
 |  
 |  __rdivmod__(self, value, /)
 |      Return divmod(value, self).
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __rfloordiv__(self, value, /)
 |      Return value//self.
 |  
 |  __rmod__(self, value, /)
 |      Return value%self.
 |  
 |  __rmul__(self, value, /)
 |      Return value*self.
 |  
 |  __round__(...)
 |      Return the Integral closest to x, rounding half toward even.
 |      When an argument is passed, work like built-in round(x, ndigits).
 |  
 |  __rpow__(self, value, mod=None, /)
 |      Return pow(value, self, mod).
 |  
 |  __rsub__(self, value, /)
 |      Return value-self.
 |  
 |  __rtruediv__(self, value, /)
 |      Return value/self.
 |  
 |  __setformat__(...) from builtins.type
 |      float.__setformat__(typestr, fmt) -> None
 |      
 |      You probably don't want to use this function.  It exists mainly to be
 |      used in Python's test suite.
 |      
 |      typestr must be 'double' or 'float'.  fmt must be one of 'unknown',
 |      'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be
 |      one of the latter two if it appears to match the underlying C reality.
 |      
 |      Override the automatic determination of C-level floating point type.
 |      This affects how floats are converted to and from binary strings.
 |  
 |  __str__(self, /)
 |      Return str(self).
 |  
 |  __sub__(self, value, /)
 |      Return self-value.
 |  
 |  __truediv__(self, value, /)
 |      Return self/value.
 |  
 |  __trunc__(...)
 |      Return the Integral closest to x between 0 and x.
 |  
 |  as_integer_ratio(...)
 |      float.as_integer_ratio() -> (int, int)
 |      
 |      Return a pair of integers, whose ratio is exactly equal to the original
 |      float and with a positive denominator.
 |      Raise OverflowError on infinities and a ValueError on NaNs.
 |      
 |      >>> (10.0).as_integer_ratio()
 |      (10, 1)
 |      >>> (0.0).as_integer_ratio()
 |      (0, 1)
 |      >>> (-.25).as_integer_ratio()
 |      (-1, 4)
 |  
 |  conjugate(...)
 |      Return self, the complex conjugate of any float.
 |  
 |  fromhex(...) from builtins.type
 |      float.fromhex(string) -> float
 |      
 |      Create a floating-point number from a hexadecimal string.
 |      >>> float.fromhex('0x1.ffffp10')
 |      2047.984375
 |      >>> float.fromhex('-0x1p-1074')
 |      -5e-324
 |  
 |  hex(...)
 |      float.hex() -> string
 |      
 |      Return a hexadecimal representation of a floating-point number.
 |      >>> (-0.1).hex()
 |      '-0x1.999999999999ap-4'
 |      >>> 3.14159.hex()
 |      '0x1.921f9f01b866ep+1'
 |  
 |  is_integer(...)
 |      Return True if the float is an integer.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  imag
 |      the imaginary part of a complex number
 |  
 |  real
 |      the real part of a complex number

None

Process finished with exit code 0

发布了37 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39532595/article/details/104477665