热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python精确有理数实验_Python更好的数学第一弹——可计算的有理数类

##哈喽这里是红石小蝈##up最近在Python里构建一个库##代码还知道如何发到github上##以下是Rational类部分(up萌新,有错误请指出,

##哈喽这里是红石小蝈

##up最近在Python里构建一个库

##代码还知道如何发到github上

##以下是Rational类部分(up萌新,有错误请指出,配色使用Py原装配色)

##代码自取

##为获得更好的观看效果,请用网页端阅读

from math import*

class Rational:

'''a rational number class instead of

python integer or floating numbers, the

base of rational functions'''

def __init__(self,num=0,den=1):

ifden == 0:

raise ZeroDivisionError('the denominator can\'t be zero')

elif notisinstance(num,(int,float)) and isinstance(den,(int,float)):

raise TypeError('the numrator and the denominator can only be numbers')

if den == 1and isinstance(num,float):

raise NotImplementedError('''you still can\'t change floating numbers into Rationals,

if u have questions please go to'''+AUTHERS_SPACE+'for help')

else:

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

##change rationals into integers and floating numbers

def __int__(self):

return self.num//self.den

def __float__(self):

return self.num/self.den

##comparing rationals with others numbers

def __eq__(self,other):

return float(self)==float(other)

def __ge__(self,other):

return float(self)>=float(other)

def __gt__(self,other):

return float(self)>float(other)

def __le__(self,other):

return float(self)<&#61;float(other)

def __lt__(self,other):

return float(self)

def __ne__(self,other):

return float(self)!&#61;float(other)

##compute rationals with numbers

def __add__(self,other):

if isinstance(other,int):

return Rational(self.num&#43;other*self.den,self.den)

elif isinstance(other,Rational) :

d&#61;self.den*other.den

n&#61;self.den*other.num&#43;self.num*other.den

a&#61;Rational(d,n)

a.reduct()

return a

def __neg__(self):

return Rational(-self.num,self.den)

def __sub__(self,other):

return self&#43;(-other)

def __rsub__(self,other):

return -(self-other)

def __mul__(self,other):

if isinstance(other,int):

a&#61;Rational(self.num*other,self.den)

a.reduct()

return a

elif isinstance(other,Rational):

a&#61;Rational(self.num*other.num,self.den*other.den)

a.reduct()

return a

def __div__(self,other):

if float(other)&#61;&#61;0:

raise ZeroDivisionError(&#39;Can\&#39;t divide by zero&#39;)

ifisinstance(other,int):

a &#61; Rational(self.num,self.den*other)

a.reduct()

return a

elif isinstance(other,Rational) :

a &#61; Rational(self.num*other.den,self.den*other.num)

a.reduct()

returna

def __rdiv__(self,other):

if float(self)&#61;&#61;0:

raise ZeroDivisionError(&#39;Can\&#39;t divide by zero&#39;)

a &#61; Rational(self.den*other,self.num)

a.reduct()

return a

def __pow__(self,other):

if other>&#61;0:

return Rational(self.num**other,self.den**other)

if other<0:

return Rational(self.den**(-other),self.num**(-other))

__rmul__&#61;__mul__

__radd__&#61;__add__

##show rationals

def __repr__(self):

if self.den&#61;&#61;1:

return str(self.num)

return&#39;(&#39;&#43;str(self.num)&#43;&#39;/&#39;&#43;str(self.den)&#43;&#39;)&#39;

##如果喜欢的话&#xff0c;不要忘记留下一个三连啊&#xff01;&#xff01;



推荐阅读
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • [译]技术公司十年经验的职场生涯回顾
    本文是一位在技术公司工作十年的职场人士对自己职业生涯的总结回顾。她的职业规划与众不同,令人深思又有趣。其中涉及到的内容有机器学习、创新创业以及引用了女性主义者在TED演讲中的部分讲义。文章表达了对职业生涯的愿望和希望,认为人类有能力不断改善自己。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
author-avatar
2335286cc
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有