Skip to content

Commit

Permalink
Add string formatting and output options (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgobat authored Jan 11, 2024
1 parent 96b21ad commit 42b1e27
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions asymmetric_uncertainty/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,22 @@ def __init__(self, nominal, pos_err=0, neg_err=0):
self.minimum = self.value-self.minus
self.sign = 1 if self.value >= 0 else -1
self.is_symmetric = np.isclose(self.plus, self.minus)

def __str__(self):
return f"{self}"

def __format__(self, fmt_spec):
if np.isclose(self.plus, self.minus):
return "{} ± {}".format(self.value,self.plus)
return f"{format(self.value, fmt_spec)} ± {format(self.plus, fmt_spec)}"
else:
return "{} (+{}, -{})".format(self.value,self.plus,self.minus)
def _repr_latex_(self):
return "%s (+%s, -%s)" % tuple(format(x, fmt_spec) for x in self.items())

def _repr_latex_(self, float_fmt="f"):
if np.isclose(self.plus, self.minus):
return "$%f \pm %f$" %(self.value,self.plus)
return f"${format(self.value, float_fmt)} \pm {format(self.plus, float_fmt)}$"
else:
return "$%f_{-%f}^{+%f}$" %(self.value,self.minus,self.plus)
return "$%s^{+%s}_{-%s}$" % tuple(format(x, float_fmt) for x in self.items())

def pdf(self, x):
"""
Computes and returns the values of the probability distribution function for the specified input.
Expand Down

0 comments on commit 42b1e27

Please sign in to comment.