博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python实现带误差传递的数学计算
阅读量:4616 次
发布时间:2019-06-09

本文共 1489 字,大约阅读时间需要 4 分钟。

import mathclass Measurement(object):    def __init__(self, val, perc):        self.val = val        self.perc = perc        self.abs  = self.val * self.perc / 100.0    def __repr__(self):        return "Measurement (%r, %r)" % (self.val, self.perc)    def __str__(self):        return "%g+-%g%%" % (self.val, self.perc)    def _addition_result(self, result, other_abs):        new_perc = 100.0 *(math.hypot(self.abs, other_abs) / result)        return Measurement(result, new_perc)    def __add__(self, other):        result = self.val + other.val        return self._addition_result(result, other.abs)    def __sub__(self, other):        result = self.val - other.val        return self._addition_result( result, other.abs)    def _multiplication_result(self, result, other_perc):        new_perc = math.hypot(self.perc, other_perc)        return Measurement(result, new_perc)    def __mul__(self, other):        result = self.val * other.val        return self._multiplication_result(result, other.perc)    def __div__(self, other):        result = self.val / other.val        return self._multiplication_result(result, other.perc)m1 = Measurement(100.0, 5.5)m2 = Measurement(50, 2)print "m1 = " , m1print "m2 = ",  m2print "m1 + m2 = ", m1 + m2print "m1 - m2 = ", m1 - m2print "m1 * m2 = ", m1 * m2print "m1 / m2 = ", m1 / m2print "(m1+m2) *(m1-m2) = " ,(m1+m2) * (m1-m2)print "(m1+m2) / (m1+m2) = ", (m1-m2) / (m1+m2)

转载于:https://www.cnblogs.com/hzhida/archive/2012/08/31/2665847.html

你可能感兴趣的文章
第k小整数
查看>>
民航项目经验总结
查看>>
HTTP权威协议笔记-4.连接管理
查看>>
php面试题之三——PHP语言基础(基础部分)
查看>>
PHP基础-常用的数组相关处理函数
查看>>
在Python中使用glob模块查找文件路径的方法
查看>>
如何提高你的数据分析能力?
查看>>
。Java中的一些小细节
查看>>
前端服务化——页面搭建工具的死与生
查看>>
Dev chartControl添加提示层信息
查看>>
在CentOS上搭建最^1024基本的Nginx反向服务器
查看>>
eclipse中编码格式调整
查看>>
终端I/O之综述
查看>>
2.10 读取打快数据文件
查看>>
Markdown 语法
查看>>
Objective-c学习笔记03——内存管理
查看>>
zxin博客园开博第一天手记
查看>>
贝叶斯网络笔记
查看>>
每天进步一点点,,
查看>>
weblog expert分析工具使用
查看>>