-
-
Save sashabaranov/9c32849d1a9d89a81a8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import sqrt | |
class EDouble(object): | |
def __init__(self, val, err): | |
self.val = float(val) | |
self.err = abs(float(err)) | |
def __mul__(self, other): | |
if not isinstance(other, EDouble): | |
other = EDouble(other, 0.0) | |
rVal = self.val * other.val | |
rel_rErr = sqrt((self.err / self.val)**2.0 + ( other.err / other.val )**2.0) | |
return EDouble(rVal, rel_rErr * rVal) | |
def __div__(self, other): | |
if not isinstance(other, EDouble): | |
other = EDouble(other, 0.0) | |
rVal = self.val / other.val | |
rel_rErr = sqrt((self.err / self.val)**2.0 + ( other.err / other.val )**2.0) | |
return EDouble(rVal, rel_rErr * rVal) | |
def __add__(self, other): | |
if not isinstance(other, EDouble): | |
other = EDouble(other, 0.0) | |
rVal = self.val + other.val | |
rErr = sqrt(self.err**2.0 + other.err**2.0) | |
return EDouble(rVal, rErr) | |
def __sub__(self, other): | |
if not isinstance(other, EDouble): | |
other = EDouble(other, 0.0) | |
rVal = self.val - other.val | |
rErr = sqrt(self.err**2.0 + other.err**2.0) | |
return EDouble(rVal, rErr) | |
def __repr__(self): | |
return "${} \\pm {}$".format(self.val, self.err) | |
# somewhat optimized | |
# class EDouble(object): | |
# def __init__(self, val, err): | |
# self.val = float(val) | |
# self.err = abs(float(err)) | |
# def __mul__(self, other): | |
# if isinstance(other, EDouble): | |
# rVal = self.val * other.val | |
# rel_rErr = sqrt((self.err / self.val)**2.0 + ( other.err / other.val )**2.0) | |
# return EDouble(rVal, rel_rErr * rVal) | |
# else: | |
# rVal = self.val * other | |
# rel_rErr = (self.err / self.val) | |
# return EDouble(rVal, rel_rErr * rVal) | |
# def __div__(self, other): | |
# if isinstance(other, EDouble): | |
# rVal = self.val / other.val | |
# rel_rErr = sqrt((self.err / self.val)**2.0 + ( other.err / other.val )**2.0) | |
# return EDouble(rVal, rel_rErr * rVal) | |
# else: | |
# rVal = self.val / other | |
# rel_rErr = self.err / self.val | |
# return EDouble(rVal, rel_rErr * rVal) | |
# def __add__(self, other): | |
# if isinstance(other, EDouble): | |
# rVal = self.val + other.val | |
# rErr = sqrt(self.err**2.0 + other.err**2.0) | |
# return EDouble(rVal, rErr) | |
# else: | |
# rVal = self.val + other | |
# rErr = self.err | |
# return EDouble(rVal, rErr) | |
# def __sub__(self, other): | |
# if isinstance(other, EDouble): | |
# rVal = self.val - other.val | |
# rErr = sqrt(self.err**2.0 + other.err**2.0) | |
# return EDouble(rVal, rErr) | |
# else: | |
# rVal = self.val - other | |
# rErr = self.err | |
# return EDouble(rVal, rErr) | |
# def __repr__(self): | |
# return "${} \\pm {}$".format(self.val, self.err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment