python的最大优势之一是有理数吗_Python里的有理数类(精度高,可计算)
up主最近正在python里写一个库,名叫‘better_math’下周将会做一个关于它的视频
这是一部分代码(本人小白,有bug请指正)(代码自取)
class Rational:
'''a rational number instead of python int or float'''
def __init__(self,num=0,den=1):
if den == 0:
raise ZeroDivisionError('the denominator can\'t be zero')
if type(num)!=int or type(den)!=int:
raise TypeError('the numrator and the denominator can only be integers')
self.num=num
self.den=den
def reduct(self):
'''make the fraction in the lowest term'''
num=int(self.num/gcd(self.num,self.den))
den=int(self.den/gcd(self.num,self.den))
self.num=num
self.den=den
def __int__(self):
return self.num//self.den
def __float__(self):
return self.num/self.den
def __add__(self,other):
if type(other)==int:
return Rational(self.num+other*self.den,self.den)
else :
d=self.den*other.den
n=self.den*other.num+self.num*other.den
a=Rational(d,n)
a.reduct()
return a
def __radd__(self,other):
return self+other
def __neg__(self):
return Rational(-self.num,self.den)
def __sub__(self,other):
return self+(-other)
def __rsub__(self,other):
return -(self-other)
def __mul__(self,other):
if type(other)==int:
a=Rational(self.num*other,self.den)
a.reduct()
return a
else:
a=Rational(self.num*other.num,self.den*other.den)
a.reduct()
return a
def __rmul__(self,other):
return self*other
def __div__(self,other):
if float(other)==0:
raise ZeroDivisionError('Can\'t divide by zero')
if type(other)==int:
a = Rational(self.num,self.den*other)
a.reduct()
return a
else :
a = Rational(self.num*other.den,self.den*other.num)
a.reduct()
return a
def __rdiv__(self,other):
if float(self)==0:
raise ZeroDivisionError('Can\'t divide by zero')
a = Rational(self.den*other,self.num)
a.reduct()
return a
def __pow__(self,other):
if other>=0:
return Rational(self.num**other,self.den**other)
if other<0:
return Rational(self.den**(-other),self.num**(-other))
def __repr__(self):
return '('+str(self.num)+'/'+str(self.den)+')'
python的最大优势之一是有理数吗_Python里的有理数类(精度高,可计算)相关推荐
- python 动态加载与静态加载_python中的元类、静态方法、类方法,动态添加方法...
首先介绍几个概念: 1.#所谓的静态方法就是这个方法任何类都可以调用,程序一加载就存在的方法 2.所谓的类方法就是这个类一加载就存在的方法,不用实例化这个类就已经存在的方法 3.所谓的元类就是创建类的 ...
- python举两种字符串引号的例子_python里的单引号和双引号的有什么作用
在Python当中表达字符串既可以使用单引号,也可以使用双引号,那两者有什么区别吗? python单引号和双引号的区别 简单来说,在Python中使用单引号或双引号是没有区别的,都可以用来表示一个字符 ...
- python中print后面要加括号吗_python里print要括号吗
print函数加与不加括号,主要是在Python2和Python3中的差异,print函数在Python2中是不需要加括号的,在Python3中,需要加括号. 主要体现在以下几个方面:1.python ...
- 0基础学好python难不难_零基础学习Python难不难?Python有什么优势?
原标题:零基础学习Python难不难?Python有什么优势? Python是一种计算机程序设计语言.首先,我们普及一下编程语言的基础知识.用任何编程语言来开发程序,都是为了让计算机干活,比如下载一个 ...
- 学Python有哪些优势
Python在人工智能领域应用是比较广泛的,近几年,越来越多的人对Python技术比较感兴趣,想要学习,那么具体学Python有哪些优势呢?我们来看看下面的详细介绍就知道了. 学Python有哪些优势 ...
- 学习Python有什么优势?
学习Python的人越来越多,很多人就想知道,编程语言有那么多种,学习Python有什么优势?为什么这么多人会选择学习Python技术?今天我们就来聊一聊Python语言. 学习Python有什么优势 ...
- python编程语言能干什么-python编程语言的优势与劣势--python能干啥
首先先发表小编的观点吧嘿嘿:python并不适合作为第一编程语言,但是第二编程语言的首选 (哥哥们先别喷我,且听我慢慢说来~~~) 先解释一下啥是第一第二编程语言吧: 通常从事开发的程序员都会学不止一 ...
- python的优点-Python语言的优势有哪些?
现如今,python语言非常火热,对于python人才的需求量也在逐年攀升.那么,python语言的优势有哪些?下面我们了解下. Python语言主要有以下9个优势: (1)简单易学 Python是一 ...
- python爬虫有什么用处-python为什么叫爬虫 python有什么优势
作为一门编程语言而言,Python是纯粹的自由软件,以简洁清晰的语法和强制使用空白符进行语句缩进的特点从而深受程序员的喜爱.很多程序员都会把python叫做爬虫,那么你们知道python为什么叫爬虫吗 ...
最新文章
- 中国矿业大学考研计算机技术,中国矿业大学(北京)(专业学位)计算机技术考研难吗...
- origin画图_把heatmap翻一转:imshow的origin和extent
- GetLastInputInfo 判断离开状态
- asp.net网页上嵌入Flash显示
- 基于python的图片修复程序-可用于水印去除
- python excel vlookup_Python Excel vlookup实现
- 产品经理之深度学习促进产品(二)
- Java数据结构——顺序表
- hmaster和datanaode启动后很快停止_室内消火栓系统消防泵启动方法
- 明白了一个重要的道理
- 【渝粤教育】电大中专门店销售与服务技巧 (2)作业 题库
- 【Java万字笔记】重要基础知识点整理与汇总
- linux sqlplus 历史命令,SQLPLUS下历史命令查找
- mil和mm之间的换算
- java中输出值保留四位小数_JAVA基础笔记(1)
- android直播流渲染,Android手机直播之处理技术分析
- 初识二维码 第二讲 二维码的结构
- 基于Java基础的家庭收支记账软件
- Nginx高阶用法(一)
- 特种浓缩分离:实验室专用离心分离机