6.3 cmath--数学函数
本模块提供了处理复数的数学函数。因此这些函数接受整数、浮点数或者复数作为參数。
6.3.1 与极坐标相互转换的函数
在Python里表示一个复数z,实部使用z.real表示,虚部使用z.imag,能够使用以下的公式来表示:
z = z.real + z.imag*1j
相同,採用极坐标也能够表示一个复数,详细是这样表示复数z。採用复数z的模r和复数向量与x轴正坐标的夹角来表示。
cmath.phase(x)
返回复数在极坐标里的相位。
样例:
#python 3.4
import cmath
n = cmath.phase(complex(-1.0, 0.0))
print(n)
n = cmath.phase(complex(-1.0, -0.0))
print(n)
结果输出例如以下:
3.141592653589793
-3.141592653589793
cmath.polar(x)
返回复数x在极坐标的表示(r, phi)。
样例:
#python 3.4
import cmath
n = cmath.polar(complex(-1.0, 0.0))
print(n)
n = cmath.polar(complex(-1.0, -0.0))
print(n)
结果输出例如以下:
(1.0, 3.141592653589793)
(1.0, -3.141592653589793)
cmath.rect(r, phi)
从极坐标表示(r, phi)转换为笛卡尔坐标表示的复数。
样例:
#python 3.4
import cmath
n = cmath.rect(1.0, cmath.pi)
print(n)
结果输出例如以下:
(-1+1.2246467991473532e-16j)
6.4 decimal--十进制的固定数和浮点数运算
本模块提供了高速地进行十进制的浮点数运算,与内置类型float有以下几点差别:
l 十进制模块是基于人类来设计的,跟人们在学校里学习到的数学内容保持一致。
l 十进制模块的浮点数是能够准确地表示。比方1.1和2.2相加,用户一般觉得是等于3.3。而不是等于3.3000000000000003。
l 正确地进行算术运算。
比方0.1 + 0.1 + 0.1 - 0.3。在数学上是等于0。但在计算机的浮点数类型时,会返回5.5511151231257827e-017。
l 保留小数点后的有效位数。比方1.30 + 1.20 等于2.50。1.3*1.2等于1.56,而1.30*1.20等于1.5600。
l 跟浮点数类型不一样的地方,它能够由用户来选择合适的精度,默认是28位。
l 二进制和十进制的浮点数都是按已经公布的标准来实现。
l 十进制模块被设计来计算固定的浮点数计算。
6.4.1 简单使用介绍
要使用decimal模块。先要把此模块导入,然后使用函数getcontext()来进看精度、小数点保留多少位。以及异常处理等等。
样例:
#python 3.4
from decimal import *
print(getcontext())
结果输出例如以下:
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
十进制的模块支持从整数、字符串、浮点数或元组构造对象:
样例:
#python 3.4
from decimal import *
print(Decimal(10))
print(Decimal('3.14'))
print(Decimal(3.14))
print(Decimal((0, (3, 1, 4), -2)))
print(Decimal(str(2.0**0.5)))
print(Decimal(2)**Decimal('0.5'))
print(Decimal('NaN'))
print(Decimal('-Infinity'))
结果输出例如以下:
10
3.14
3.140000000000000124344978758017532527446746826171875
3.14
1.4142135623730951
1.414213562373095048801688724
NaN
-Infinity
打开浮点数操作时就抛出异常:
样例:
#python 3.4
from decimal import *
c = getcontext()
c.traps[FloatOperation] = True
print(Decimal('3.14'))
print(Decimal(3.14))
结果输出例如以下:
3.14
Traceback (most recent call last):
File "F:/temp/pywin/dec1.py", line 7, in <module>
print(Decimal(3.14))
decimal.FloatOperation: [<class 'decimal.FloatOperation'>]
改变十进制模块的位数精度、有效位取舍:
样例:
#python 3.4
from decimal import *
c = getcontext()
c.prec = 6
print(Decimal('3.14'))
print(Decimal(3.14))
print(Decimal('3.1415926535') + Decimal('2.7182818285'))
c.rounding = ROUND_UP
print(Decimal('3.1415926535') + Decimal('2.7182818285'))
结果输出例如以下:
3.14
3.140000000000000124344978758017532527446746826171875
5.85987
5.85988
对于浮点数的字符串进行处理:
样例:
#python 3.4
from decimal import *
data = list(map(Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split()))
print(max(data))
print(min(data))
print(sorted(data))
print(sum(data))
结果输出例如以下:
9.25
0.03
[Decimal('0.03'), Decimal('1.00'), Decimal('1.34'), Decimal('1.87'), Decimal('2.35'), Decimal('3.45'), Decimal('9.25')]
19.29
针对不同小数位进行取舍:
样例:
#python 3.4
from decimal import *
print(Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN))
print(Decimal('7.325').quantize(Decimal('1.'), rounding=ROUND_UP))
结果输出例如以下:
7.32
8
6.4.2 Decimal对象
class decimal.Decimal(value="0", context=None)
从值value构造一个Decimal对象。
value的类型能够是整数、字符串、元组、浮点数或者还有一个Decimal对象。假设输入是一个字符串,符合以下的规则的表达式:
sign ::= '+' | '-'
digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
indicator ::= 'e' | 'E'
digits ::= digit [digit]...
decimal-part ::= digits '.' [digits] | ['.'] digits
exponent-part ::= indicator [sign] digits
infinity ::= 'Infinity' | 'Inf'
nan ::= 'NaN' [digits] | 'sNaN' [digits]
numeric-value ::= decimal-part [exponent-part] | infinity
numeric-string ::= [sign] numeric-value | [sign] nan
adjusted()
返回调整为指数形式之后。指数须要多少表示。
样例:
#python 3.4
from decimal import *
print(Decimal('7.325').adjusted())
print(Decimal('1000').adjusted())
print(Decimal('9000').adjusted())
print(Decimal('0.0009').adjusted())
输出结果例如以下:
0
3
3
-4
as_tuple()
返回命名的元组表示十进制数值。
样例:
#python 3.4
from decimal import *
print(Decimal('7.325').as_tuple())
print(Decimal('1000').as_tuple())
print(Decimal('9000').as_tuple())
print(Decimal('0.0009').as_tuple())
结果输出例如以下:
DecimalTuple(sign=0, digits=(7, 3, 2, 5), exponent=-3)
DecimalTuple(sign=0, digits=(1, 0, 0, 0), exponent=0)
DecimalTuple(sign=0, digits=(9, 0, 0, 0), exponent=0)
DecimalTuple(sign=0, digits=(9,), exponent=-4)
canonical()
返回规范的格式。
样例:
#python 3.4
from decimal import *
print(Decimal('7.325').canonical())
print(Decimal('1000').canonical())
print(Decimal('9000').canonical())
print(Decimal('0.0009').canonical())
结果输出例如以下:
7.325
1000
9000
0.0009
compare(other, context=None)
与其他other十进制数值比較。等于返回0, 大于返回1。小于返回-1,与不是数值比較返回NaN。
样例:
#python 3.4
from decimal import *
r = Decimal('100').compare(Decimal('200'))
print(r)
r = Decimal('100').compare(Decimal('50'))
print(r)
r = Decimal('100').compare(Decimal('100'))
print(r)
r = Decimal('100').compare(Decimal('-inf'))
print(r)
r = Decimal('100').compare(Decimal('NaN'))
print(r)
结果输出例如以下:
-1
1
0
1
NaN
compare_signal(other, context=None)
除了NaN操作不一样之外。其他与compare()函数是一样的。
样例:
#python 3.4
from decimal import *
r = Decimal('100').compare_signal(Decimal('200'))
print(r)
r = Decimal('100').compare_signal(Decimal('50'))
print(r)
r = Decimal('100').compare_signal(Decimal('100'))
print(r)
r = Decimal('100').compare_signal(Decimal('-inf'))
print(r)
r = Decimal('100').compare_signal(Decimal('NaN'))
print(r)
结果输出例如以下:
-1
1
0
1
Traceback (most recent call last):
File "F:\temp\pywin\dec1.py", line 12, in <module>
r = Decimal('100').compare_signal(Decimal('NaN'))
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
compare_total(other, context=None)
使用抽象表示方式进行比較,而不是使用值。
返回的结果跟compare()函数一样。
样例:
#python 3.4
from decimal import *
r = Decimal('100').compare_total(Decimal('100'))
print(r)
r = Decimal('100').compare_total(Decimal('100.0'))
print(r)
r = Decimal('100.0').compare_total(Decimal('100'))
print(r)
结果输出例如以下:
0
1
-1
compare_total_mag(other, context=None)
不考虑符号的比較,与上面的函数返回值一样。
样例:
#python 3.4
from decimal import *
r = Decimal('100').compare_total_mag(Decimal('-100'))
print(r)
r = Decimal('100').compare_total_mag(Decimal('-100.0'))
print(r)
r = Decimal('100.0').compare_total_mag(Decimal('-100'))
print(r)
结果输出例如以下:
0
1
-1
conjugate()
返回十进制值本身。
样例:
#python 3.4
from decimal import *
r = Decimal('100').conjugate()
print(r)
结果输出例如以下:
100
copy_abs()
返回对象的绝对值。
样例:
#python 3.4
from decimal import *
r = Decimal('100').copy_abs()
print(r)
r = Decimal('-100').copy_abs()
print(r)
结果输出例如以下:
100
100
copy_negate()
返回相反数。
样例:
#python 3.4
from decimal import *
r = Decimal('100').copy_negate()
print(r)
r = Decimal('-100').copy_negate()
print(r)
结果输出例如以下:
-100
100
copy_sign(other, context=None)
从另外十进制对象获取符号。本对象取绝对值。
样例:
#python 3.4
from decimal import *
r = Decimal('100').copy_sign(Decimal('-1.0'))
print(r)
r = Decimal('-100').copy_sign(Decimal('-1.0'))
print(r)
结果输出例如以下:
-100
-100
exp(context=None)
返回这个值的自然指数值,
6.3 cmath--数学函数的更多相关文章
- cmath模块——复数域数学函数模块
cmath——复数域数学函数模块 转自:https://blog.csdn.net/zhtysw/article/category/7511293 该模块属于内置模块,随时可以调用.它提供了数学函数在 ...
- Python数学函数
1.Python数学函数 1.abs(x):取绝对值,内建函数 2.math.ceil(x):向上取整,在math模块中 3.cmp(x,y):如果 x < y ,返回-1:如果 x == y ...
- C/C++常用数学函数
math.h/cmath(C++)数学函数库 1 三角函数 double sin (double); double cos (double); double tan (double) ...
- C++中常用的数学函数总结
我们在C++程序设计的过程中往往会使用到一些数学函数,那么不同的数学运算要用到什么函数哪?大家可以参考我的总结如下: 首先引用到数学函数时一定要记得加函数头文件 #include<cmath&g ...
- Sql Server函数全解<二>数学函数
阅读目录 1.绝对值函数ABS(x)和返回圆周率的函数PI() 2.平方根函数SQRT(x) 3.获取随机函数的函数RAND()和RAND(x) 4.四舍五入函数ROUND(x,y) 5.符号函数SI ...
- Sql Server函数全解(二)数学函数
数学函数主要用来处理数值数据,主要的数学函数有:绝对值函数,三角函数(包括正弦函数,余弦函数,正切函数,余切函数).对数函数,随机函数等.在错误产生时,数学函数将返回空值null.本次介绍各种数学 ...
- Java语言程序设计(基础篇) 第四章 数学函数、字符和字符串
第四章 数学函数.字符和字符串 4.2 常用数学函数 方法分三类:三角函数方法(trigonometric method).指数函数方法(exponent method)和服务方法(service m ...
- javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数
javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数 1.常规函数 javascript常规函数包括以下9个 ...
- cocos2d-x:懒人数学函数
做游戏开发,要用到比较多的数学计算,对于程序员来说,还是用一种懒一点的方法,cocos2d-x方便开发者投机取巧...提供了很多方便的的数学函数,方便我们的数学计算.以下是在网上收集到的一些常用的数学 ...
- Myth – 支持变量和数学函数的 CSS 预处理器
Myth 是一个预处理器,有点类似于 CSS polyfill .Myth 让你写纯粹的 CSS,同时还让你可以使用类似 LESS 和 Sass 的工具.您仍然可以使用变量和数学函数,就像你在其它预处 ...
随机推荐
- Python网络爬虫 - 下载图片
下载博客园的logo from urllib.request import urlretrieve from urllib.request import urlopen from bs4 import ...
- Java AES 加密工具类
package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.S ...
- c++构造函数具体解释
一.Default constructor 1. 对于class X ,假设没有不论什么user-declared constructor,那么编译器生成的default construc ...
- STS项目html文件中文乱码解决
解决方案: windows -- perferences -- encoding,设置成utf-8 步骤一:Content Types 步骤二:Workspace 步骤三:JSP Files
- 解决 jersey 单jar包 IME media type text/plain was not found.
1.maven-assembly-plugin 换成 --> maven-shade-plugin <plugins> <!-- shade插件打包成jar包 --> ...
- 获取每月第一天最后一天 java
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //获取前月的第一天 Calendar cal_1=Ca ...
- 通过导入虚拟电脑的方式还原centos
通过oracle vm VirtualBox安装完成一台centos,然后导出虚拟电脑,再通过导入虚拟电脑的方式还原一台centos,还原的时候改一下机器名,不要选择重新初始化所有网卡mac,还原完成 ...
- ls -lrt
1,按照时间升序 命令:ls -lrt 详细解释: -l use a long listing format 以长列表方式显示(详细信息方式) -t sort by modification time ...
- redis编译
简介: Redis是Nosql中比较出名的,分布式数据库缓存,提升相应的速度,降低对数据库的访问! Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,(永久 ...
- JavaScript实现碰撞检测(分离轴定理)
概述 分离轴定理是一项用于检测碰撞的算法.其适用范围较广,涵盖检测圆与多边形,多边形与多边形的碰撞:缺点在于无法检测凹多边形的碰撞.本demo使用Js进行算法实现,HTML5 canvas进行渲染. ...