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里的有理数类(精度高,可计算)相关推荐

  1. python 动态加载与静态加载_python中的元类、静态方法、类方法,动态添加方法...

    首先介绍几个概念: 1.#所谓的静态方法就是这个方法任何类都可以调用,程序一加载就存在的方法 2.所谓的类方法就是这个类一加载就存在的方法,不用实例化这个类就已经存在的方法 3.所谓的元类就是创建类的 ...

  2. python举两种字符串引号的例子_python里的单引号和双引号的有什么作用

    在Python当中表达字符串既可以使用单引号,也可以使用双引号,那两者有什么区别吗? python单引号和双引号的区别 简单来说,在Python中使用单引号或双引号是没有区别的,都可以用来表示一个字符 ...

  3. python中print后面要加括号吗_python里print要括号吗

    print函数加与不加括号,主要是在Python2和Python3中的差异,print函数在Python2中是不需要加括号的,在Python3中,需要加括号. 主要体现在以下几个方面:1.python ...

  4. 0基础学好python难不难_零基础学习Python难不难?Python有什么优势?

    原标题:零基础学习Python难不难?Python有什么优势? Python是一种计算机程序设计语言.首先,我们普及一下编程语言的基础知识.用任何编程语言来开发程序,都是为了让计算机干活,比如下载一个 ...

  5. 学Python有哪些优势

    Python在人工智能领域应用是比较广泛的,近几年,越来越多的人对Python技术比较感兴趣,想要学习,那么具体学Python有哪些优势呢?我们来看看下面的详细介绍就知道了. 学Python有哪些优势 ...

  6. 学习Python有什么优势?

    学习Python的人越来越多,很多人就想知道,编程语言有那么多种,学习Python有什么优势?为什么这么多人会选择学习Python技术?今天我们就来聊一聊Python语言. 学习Python有什么优势 ...

  7. python编程语言能干什么-python编程语言的优势与劣势--python能干啥

    首先先发表小编的观点吧嘿嘿:python并不适合作为第一编程语言,但是第二编程语言的首选 (哥哥们先别喷我,且听我慢慢说来~~~) 先解释一下啥是第一第二编程语言吧: 通常从事开发的程序员都会学不止一 ...

  8. python的优点-Python语言的优势有哪些?

    现如今,python语言非常火热,对于python人才的需求量也在逐年攀升.那么,python语言的优势有哪些?下面我们了解下. Python语言主要有以下9个优势: (1)简单易学 Python是一 ...

  9. python爬虫有什么用处-python为什么叫爬虫 python有什么优势

    作为一门编程语言而言,Python是纯粹的自由软件,以简洁清晰的语法和强制使用空白符进行语句缩进的特点从而深受程序员的喜爱.很多程序员都会把python叫做爬虫,那么你们知道python为什么叫爬虫吗 ...

最新文章

  1. 中国矿业大学考研计算机技术,中国矿业大学(北京)(专业学位)计算机技术考研难吗...
  2. origin画图_把heatmap翻一转:imshow的origin和extent
  3. GetLastInputInfo 判断离开状态
  4. asp.net网页上嵌入Flash显示
  5. 基于python的图片修复程序-可用于水印去除
  6. python excel vlookup_Python Excel vlookup实现
  7. 产品经理之深度学习促进产品(二)
  8. Java数据结构——顺序表
  9. hmaster和datanaode启动后很快停止_室内消火栓系统消防泵启动方法
  10. 明白了一个重要的道理
  11. 【渝粤教育】电大中专门店销售与服务技巧 (2)作业 题库
  12. 【Java万字笔记】重要基础知识点整理与汇总
  13. linux sqlplus 历史命令,SQLPLUS下历史命令查找
  14. mil和mm之间的换算
  15. java中输出值保留四位小数_JAVA基础笔记(1)
  16. android直播流渲染,Android手机直播之处理技术分析
  17. 初识二维码 第二讲 二维码的结构
  18. 基于Java基础的家庭收支记账软件
  19. Nginx高阶用法(一)
  20. 特种浓缩分离:实验室专用离心分离机

热门文章

  1. hdu 6015 Gameia(树上博弈)
  2. getFields和getDeclaredFields
  3. [Linux网络编程学习笔记]索引
  4. UVA10494 If We Were a Child Again【大数除法】
  5. JSK-18 跳跃游戏【基础】
  6. POJ1061 青蛙的约会【扩展欧几里得算法】
  7. CCF NOI1026 表演打分
  8. CCF201412试题
  9. 各种图示的介绍及绘制(boxplot、stem)
  10. UNIX环境高级编程(三)—— 静态链接库与动态链接库