Coverage for pygeodesy/fsums.py : 98%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*-
summation, based on respectively similar to Python's C{math.fsum}.
Generally, an L{Fsum} instance is considered a C{float} plus a small or zero C{residual} value, see property L{Fsum.residual}. However, there are several C{integer} L{Fsum} cases, for example the result of C{ceil}, C{floor}, C{Fsum.__floordiv__} and methods L{Fsum.fint} and L{Fsum.fint2}.
Also, L{Fsum} methods L{Fsum.pow}, L{Fsum.__ipow__}, L{Fsum.__pow__} and L{Fsum.__rpow__} return a (very long) C{int} if invoked with optional argument C{mod} set to C{None}. The C{residual} of an C{integer} L{Fsum} may be between C{-1.0} and C{+1.0}, including C{INT0} if considered to be I{exact}.
Set env variable C{PYGEODESY_FSUM_PARTIALS} to an empty string (or anything other than C{"fsum"}) for backward compatible summation of L{Fsum} partials.
Set env variable C{PYGEODESY_FSUM_RESIDUAL} to a C{float} string for the threshold to throw a L{ResidualError} in division or exponention of an L{Fsum} instance with a I{relative} C{residual} exceeding the threshold, see methods L{Fsum.RESIDUAL}, L{Fsum.pow}, L{Fsum.__ipow__} and L{Fsum.__itruediv__}. ''' # make sure int/int division yields float quotient, see .basics
signOf, _signOf _0_0, _1_0, _N_1_0 _ValueError, _xError2, _xkwds_get, _xkwds_get_, \ _ZeroDivisionError _EQUAL_, _exceeds_, _from_, _iadd_, _LANGLE_, \ _negative_, _not_finite_, _not_scalar_, \ _PERCENT_, _PLUS_, _R_, _RANGLE_, _SLASH_, \ _SPACE_, _STAR_, _UNDER_ Property_RO, property_RO # from pygeodesy.streprs import Fmt, unstr # from .named
'''(INTERNAL) Raise C{TypeError} or C{ValueError} if not scalar or infinite. ''' except Exception as X: E, t = _xError2(X) n = Fmt.SQUARE(n, index) raise E(n, v, txt=t)
'''(INTERNAL) Yield all B{C{xs}} as C{float}s. ''' ps = map(neg, ps) else:
'''(INTERNAL) Yield each C{xs} as C{float(x**power)}. ''' else: except Exception as X: E, t = _xError2(X) if i is None: i = Fmt.PARENSPACED(xs=xs) else: i = Fmt.SQUARE(xs=i + origin) i = Fmt.PARENSPACED(i, x) raise E(i, txt=t)
'''(INTERNAL) Partials summation updating C{ps}, I{overridden below}. ''' (p < 0 and r < 0): # signs match
'''(INTERNAL) Return B{C{other}} as C{int}, C{float} or C{as-is}. ''' if r: # PYCHOK no cover if _raiser and _raiser(r, s): raise ValueError(_stresidual(_non_zero_, r)) s = other # L{Fsum} as-is else:
'''(INTERNAL) C{Complex} 2- or 3-arg C{pow} error C{str}. '''
'''(INTERNAL) Residual error C{str}. '''
'''(INTERNAL) Return C{a + b} as 2-tuple (sum, residual). ''' raise _OverflowError(u, txt=t) (b - (s - a)) # abs(a) >= abs(b)
'''Precision floating point I{running} summation similar to standard Python's C{math.fsum}.
Unlike C{math.fsum}, this class accumulates values and provides I{intermediate} precision floating point summation. Accumulation may continue after I{intermediate} summuation, aka I{running} summation.
@note: Accumulated values may be L{Fsum} or C{scalar} instances with C{scalar} meaning type C{float}, C{int} or any C{type} convertible to a single C{float}, having method C{__float__}.
@note: Handling of exceptions and of values C{inf}, C{INF}, C{nan} and C{NAN} differs from standard Python's C{math.fsum}.
@see: U{Hettinger<https://GitHub.com/ActiveState/code/blob/master/recipes/Python/ 393090_Binary_floating_point_summatiaccurate_full/recipe-393090.py>}, U{Kahan <https://WikiPedia.org/wiki/Kahan_summation_algorithm>}, U{Klein <https://Link.Springer.com/article/10.1007/s00607-005-0139-x>}, Python 2.6+ file I{Modules/mathmodule.c} and the issue log U{Full precision summation <https://Bugs.Python.org/issue2819>}. ''' # _ps = [] # partial sums # _px = 0
'''New L{Fsum} for precision floating point I{running} summation.
@arg xs: No, one or more initial values (each C{scalar} or an L{Fsum} instance). @kwarg name_RESIDUAL: Optional C{B{name}=NN} for this L{Fsum} (C{str}) and C{B{RESIDUAL}=None} for the L{ResidualError} threshold.
@see: Methods L{Fsum.fadd} and L{Fsum.RESIDUAL}. ''' # self._n = 0
'''Return this instance' absolute value as an L{Fsum}. ''' self._copy_2(self.__abs__)
'''Return the C{Fsum(B{self}, B{other})}.
@arg other: An L{Fsum} or C{scalar}.
@return: The sum (L{Fsum}).
@see: Method L{Fsum.__iadd__}. '''
'''Return C{True} if this instance is non-zero. '''
'''Return this instance' C{math.ceil} as C{int} or C{float}.
@return: An C{int} in Python 3+, but C{float} in Python 2-.
@see: Methods L{Fsum.__floor__} and property L{Fsum.ceil}. '''
'''Return C{divmod(B{self}, B{other})} as 2-tuple C{(quotient, remainder)}, an C{int} in Python 3+ or C{float} in Python 2- and an L{Fsum}.
@arg other: An L{Fsum} or C{scalar} modulus.
@see: Method L{Fsum.__itruediv__}. '''
'''Compare this with an other instance or scalar. '''
'''Return this instance' current precision running sum as C{float}.
@see: Methods L{Fsum.fsum} and L{Fsum.int_float}. '''
'''Return this instance' C{math.floor} as C{int} or C{float}.
@return: An C{int} in Python 3+, but C{float} in Python 2-.
@see: Methods L{Fsum.__ceil__} and property L{Fsum.floor}. '''
'''Return C{B{self} // B{other}} as an L{Fsum}.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: The C{floor} quotient (L{Fsum}).
@see: Methods L{Fsum.__ifloordiv__}. '''
def __format__(self, *other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, *other)
'''Compare this with an other instance or scalar. '''
'''Compare this with an other instance or scalar. '''
def __hash__(self): # PYCHOK no cover '''Return this instance' C{hash}. ''' return hash(self._ps) # XXX id(self)?
'''Apply C{B{self} += B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar} instance.
@return: This instance, updated (L{Fsum}).
@raise TypeError: Invalid B{C{other}}, not C{scalar} nor L{Fsum}.
@see: Methods L{Fsum.fadd} and L{Fsum.fadd_}. '''
'''Apply C{B{self} //= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: This instance, updated (L{Fsum}).
@raise ResidualError: Non-zero residual in B{C{other}}.
@raise TypeError: Invalid B{C{other}} type.
@raise ValueError: Invalid or non-finite B{C{other}}.
@raise ZeroDivisionError: Zero B{C{other}}.
@see: Methods L{Fsum.__itruediv__}. '''
def __imatmul__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Apply C{B{self} %= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar} modulus.
@return: This instance, updated (L{Fsum}).
@see: Method L{Fsum.__divmod__}. '''
'''Apply C{B{self} *= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar} factor.
@return: This instance, updated (L{Fsum}).
@raise OverflowError: Partial C{2sum} overflow.
@raise TypeError: Invalid B{C{other}} type.
@raise ValueError: Invalid or non-finite B{C{other}}. '''
'''Return this instance as an C{int}.
@see: Methods L{Fsum.int_float}, L{Fsum.__ceil__} and L{Fsum.__floor__} and properties L{Fsum.ceil} and L{Fsum.floor}. '''
'''Apply C{B{self} **= B{other}} to this instance.
@arg other: The exponent (L{Fsum} or C{scalar}). @arg mod: Optional modulus (C{int} or C{None}) for the 3-argument C{pow(B{self}, B{other}, B{mod})} version.
@return: This instance, updated (L{Fsum}).
@note: If B{C{mod}} is given, the result will be an C{integer} L{Fsum} in Python 3+ if this instance C{is_integer} or set to C{as_integer} if B{C{mod}} given as C{None}.
@raise OverflowError: Partial C{2sum} overflow.
@raise ResidualError: Non-zero residual in B{C{other}} and env var C{PYGEODESY_FSUM_RESIDUAL} set or this instance has a non-zero residual and either B{C{mod}} is given and non-C{None} or B{C{other}} is a negative or fractional C{scalar}.
@raise TypeError: Invalid B{C{other}} type or 3-argument C{pow} invocation failed.
@raise ValueError: If B{C{other}} is a negative C{scalar} and this instance is C{0} or B{C{other}} is a fractional C{scalar} and this instance is negative or has a non-zero residual or B{C{mod}} is given and C{0}.
@see: CPython function U{float_pow<https://GitHub.com/ python/cpython/blob/main/Objects/floatobject.c>}. '''
'''Apply C{B{self} -= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar}.
@return: This instance, updated (L{Fsum}).
@raise TypeError: Invalid B{C{other}} type.
@see: Method L{Fsum.fadd}. '''
'''Return an C{iter}ator over a C{partials} duplicate. '''
'''Apply C{B{self} /= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: This instance, updated (L{Fsum}).
@raise OverflowError: Partial C{2sum} overflow.
@raise ResidualError: Non-zero residual in B{C{other}} and env var C{PYGEODESY_FSUM_RESIDUAL} set.
@raise TypeError: Invalid B{C{other}} type.
@raise ValueError: Invalid or non-finite B{C{other}}.
@raise ZeroDivisionError: Zero B{C{other}}.
@see: Method L{Fsum.__ifloordiv__}. '''
'''Compare this with an other instance or scalar. '''
'''Return the I{total} number accumulations (C{int}). '''
'''Compare this with an other instance or scalar. '''
def __matmul__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Return C{B{self} % B{other}} as an L{Fsum}.
@see: Method L{Fsum.__imod__}. '''
'''Return C{B{self} * B{other}} as an L{Fsum}.
@see: Method L{Fsum.__imul__}. '''
'''Compare this with an other instance or scalar. '''
'''Return I{a copy of} this instance, negated. '''
'''Return this instance I{as-is}, like C{float.__pos__()}. '''
'''Return C{B{self}**B{other}} as an L{Fsum}.
@see: Method L{Fsum.__ipow__}. '''
'''Return C{B{other} + B{self}} as an L{Fsum}.
@see: Method L{Fsum.__iadd__}. '''
'''Return C{divmod(B{other}, B{self})} as 2-tuple C{(quotient, remainder)}.
@see: Method L{Fsum.__divmod__}. '''
# def __repr__(self): # '''Return the default C{repr(this)}. # ''' # return self.toRepr(lenc=True)
'''Return C{B{other} // B{self}} as an L{Fsum}.
@see: Method L{Fsum.__ifloordiv__}. '''
def __rmatmul__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Return C{B{other} % B{self}} as an L{Fsum}.
@see: Method L{Fsum.__imod__}. '''
'''Return C{B{other} * B{self}} as an L{Fsum}.
@see: Method L{Fsum.__imul__}. '''
def __round__(self, ndigits=None): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, ndigits=ndigits)
'''Return C{B{other}**B{self}} as an L{Fsum}.
@see: Method L{Fsum.__ipow__}. '''
'''Return C{B{other} - B{self}} as L{Fsum}.
@see: Method L{Fsum.__isub__}. '''
'''Return C{B{other} / B{self}} as an L{Fsum}.
@see: Method L{Fsum.__itruediv__}. '''
'''Return the current size of this instance in C{bytes}. ''' self._fint2[0], self._fint2[1], self._fprs, self._fprs2, self._fprs2.fsum, self._fprs2.residual, self._n, self._ps, *self._ps))
'''Return the default C{str(self)}. '''
'''Return C{B{self} - B{other}} as an L{Fsum}.
@arg other: An L{Fsum} or C{scalar}.
@return: The difference (L{Fsum}).
@see: Method L{Fsum.__isub__}. '''
'''Return C{B{self} / B{other}} as an L{Fsum}.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: The quotient (L{Fsum}).
@see: Method L{Fsum.__itruediv__}. '''
if _sys_version_info2 < (3, 0): # PYCHOK no cover # <https://docs.Python.org/2/library/operator.html#mapping-operators-to-functions> __div__ = __truediv__ __idiv__ = __itruediv__ __long__ = __int__ __nonzero__ = __bool__ __rdiv__ = __rtruediv__
'''Return this instance as the ratio of 2 integers.
@return: 2-Tuple C{(numerator, denominator)} both C{int} and with positive C{denominator}.
@see: Standard C{float.as_integer_ratio} in Python 3+. ''' else: # PYCHOK no cover d = 1
'''Get this instance' C{ceil} value (C{int} in Python 3+, but C{float} in Python 2-).
@note: The C{ceil} takes the C{residual} into account.
@see: Method L{Fsum.int_float} and properties L{Fsum.floor}, L{Fsum.imag} and L{Fsum.real}. '''
'''(INTERNAL) Return C{scalar(self - B{other})} for 0-comparison. ''' else: raise self._TypeError(op, other) # txt=_invalid_
'''Copy this instance, C{shallow} or B{C{deep}}.
@return: The copy (L{Fsum}). '''
'''(INTERNAL) Copy with/-out overriding C{partials}. ''' # for x in xs: # assert isscalar(x)
'''(INTERNAL) Copy for I{dyadic} operators. ''' # NOT .classof due to .Fdot(a, *b) args, etc. # assert f._n == self._n
'''(INTERNAL) Negated copy for I{monadic} C{__abs__} and C{__neg__}. ''' # f._facc_up(up=False) else: f = self._Fsum(self._n, _0_0)
'''(INTERNAL) Copy for I{reverse-dyadic} operators. ''' Fsum(other, name=which.__name__) # see ._copy_2
'''(INTERNAL) Copy C{other._RESIDUAL}. '''
'''(INTERNAL) Minimal, anonymous copy. '''
'''Return C{divmod(B{self}, B{other})} as 2-tuple C{(quotient, remainder)}.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: 2-Tuple C{(quotient, remainder)}, with the C{quotient} an C{int} in Python 3+ or a C{float} in Python 2- and the C{remainder} an L{Fsum} instance.
@see: Method L{Fsum.__itruediv__}. '''
'''(INTERNAL) Format an B{C{Error}} for C{{self} B{op} B{other}}. '''
'''(INTERNAL) Format a caught exception. '''
'''(INTERNAL) Accumulate more known C{scalar}s. ''' # assert isscalar(x) and isfinite(x) # assert self._ps is ps # Fsum._px = max(Fsum._px, len(ps))
'''(INTERNAL) Accumulate all positional C{scalar}s. '''
# def _facc_up(self, up=True): # '''(INTERNAL) Update the C{partials}, by removing # and re-accumulating the final C{partial}. # ''' # while len(self._ps) > 1: # p = self._ps.pop() # if p: # n = self._n # self._facc_(p, up=False) # self._n = n # break # return self._update() if up else self # ._fpsqz()
'''Add an iterable of C{scalar} or L{Fsum} instances to this instance.
@arg xs: Iterable, list, tuple, etc. (C{scalar} or L{Fsum} instances).
@return: This instance (L{Fsum}).
@raise OverflowError: Partial C{2sum} overflow.
@raise TypeError: An invalid B{C{xs}} type, not C{scalar} nor L{Fsum}.
@raise ValueError: Invalid or non-finite B{C{xs}} value. ''' self._facc_(_2float(x=xs)) # PYCHOK no cover
'''Add all positional C{scalar} or L{Fsum} instances to this instance.
@arg xs: Values to add (C{scalar} or L{Fsum} instances), all positional.
@return: This instance (L{Fsum}).
@raise OverflowError: Partial C{2sum} overflow.
@raise TypeError: An invalid B{C{xs}} type, not C{scalar} nor L{Fsum}.
@raise ValueError: Invalid or non-finite B{C{xs}} value. '''
'''(INTERNAL) Apply C{B{self} += B{other}}. ''' raise self._TypeError(op, other) # txt=_invalid_
'''(INTERNAL) C{divmod(B{self}, B{other})} as 2-tuple (C{int} or C{float}, remainder C{self}). ''' # result mostly follows CPython function U{float_divmod # <https://GitHub.com/python/cpython/blob/main/Objects/floatobject.c>}, # but at least divmod(-3, 2) equals Cpython's result (-2, 1).
if s and self.signOf() == -s: # PYCHOK no cover self += other q -= 1
# t = self.signOf() # if t and t != s: # from pygeodesy.errors import _AssertionError # raise self._Error(op, other, _AssertionError, txt=signOf.__name__)
'''(INTERNAL) Return B{C{other}} if C{finite}. ''' raise ValueError(_not_finite_) if not op else \ self._ValueError(op, other, txt=_not_finite_)
'''Return this instance' current running sum as C{integer}.
@kwarg raiser: If C{True} throw a L{ResidualError} if the I{integer} residual is non-zero. @kwarg name: Optional name (C{str}), overriding C{"fint"}.
@return: The C{integer} (L{Fsum}).
@raise ResidualError: Non-zero I{integer} residual.
@see: Methods L{Fsum.int_float} and L{Fsum.is_integer}. ''' t = _stresidual(_integer_, r) raise ResidualError(_integer_, i, txt=t)
'''Return this instance' current running sum as C{int} and the I{integer} residual.
@kwarg name: Optional name (C{str}).
@return: An L{Fsum2Tuple}C{(fsum, residual)} with C{fsum} an C{int} and I{integer} C{residual} a C{float} or C{INT0} if the C{fsum} is considered to be I{exact}. '''
'''(INTERNAL) Get 2-tuple (C{int}, I{integer} residual). ''' (self._ps[0] - i) if self._ps else -i)
def float_int(self): # PYCHOK no cover '''DEPRECATED, use method C{Fsum.int_float}.''' return self.int_float() # raiser=False
'''Get this instance' C{floor} (C{int} in Python 3+, but C{float} in Python 2-).
@note: The C{floor} takes the C{residual} into account.
@see: Method L{Fsum.int_float} and properties L{Fsum.ceil}, L{Fsum.imag} and L{Fsum.real}. '''
# floordiv = __floordiv__ # for naming consistency
'''Apply C{B{self} //= B{other}}. '''
'''(INTERNAL) Apply C{B{self} *= B{other}}. ''' raise self._TypeError(op, other) # txt=_invalid_ else: # len(other._ps) == len(self._ps) == 1
'''Apply C{B{self} /= B{over}} and summate.
@arg over: An L{Fsum} or C{scalar} denominator.
@return: Precision running sum (C{float}).
@see: Methods L{Fsum.fsum} and L{Fsum.__itruediv__}. '''
'''Apply C{B{self} **= B{other}}, optional B{C{mod}} or C{None}. ''' # return an exact C{int} for C{int}**C{int} else: # pow(self, other) == pow(self, other, None) raise self._ResidualError(op, other, r) p = self._pow_scalar(r, other, op) # p = _2scalar(p) # _raiser = None raise self._TypeError(op, other) # txt=_invalid_ else: s *= p
'''(INTERNAL) Get and cache this instance' precision running sum (C{float} or C{int}), ignoring C{residual}.
@note: The precision running C{fsum} after a C{//=} or C{//} C{floor} division is C{int} in Python 3+. ''' else: # len(ps) == 1 # assert self._ps is ps # assert Fsum._fprs2.name not in self.__dict__
'''(INTERNAL) Get and cache this instance' precision running sum and residual (L{Fsum2Tuple}). '''
# def _fpsqz(self): # '''(INTERNAL) Compress, squeeze the C{partials}. # ''' # if len(self._ps) > 2: # _ = self._fprs # return self
'''(INTERNAL) Overwrite this instance with an other or a C{scalar}. ''' # use or zap the C{Property_RO} values # Property_RO _fint2, _fprs and _fprs2 can't be a Property: # Property's _fset zaps the value just set by the @setter else: # PYCHOK no cover raise self._TypeError(_fset_op_, other) # txt=_invalid_
'''Subtract an iterable of C{scalar} or L{Fsum} instances from this instance.
@arg xs: Iterable, list, tuple. etc. (C{scalar} or L{Fsum} instances).
@return: This instance, updated (L{Fsum}).
@see: Method L{Fsum.fadd}. '''
'''Subtract all positional C{scalar} or L{Fsum} instances from this instance.
@arg xs: Values to subtract (C{scalar} or L{Fsum} instances), all positional.
@return: This instance, updated (L{Fsum}).
@see: Method L{Fsum.fadd}. '''
'''(INTERNAL) Apply C{B{self} -= B{other}}. ''' raise self._TypeError(op, other) # txt=_invalid_
'''(INTERNAL) New L{Fsum} instance. '''
'''Add more C{scalar} or L{Fsum} instances and summate.
@kwarg xs: Iterable, list, tuple, etc. (C{scalar} or L{Fsum} instances).
@return: Precision running sum (C{float} or C{int}).
@see: Method L{Fsum.fadd}.
@note: Accumulation can continue after summation. '''
'''Add all positional C{scalar} or L{Fsum} instances and summate.
@arg xs: Values to add (C{scalar} or L{Fsum} instances), all positional.
@return: Precision running sum (C{float} or C{int}).
@see: Method L{Fsum.fsum}. '''
'''Add more C{scalar} or L{Fsum} instances and return the current precision running sum and the C{residual}.
@kwarg xs: Iterable, list, tuple, etc. (C{scalar} or L{Fsum} instances). @kwarg name: Optional name (C{str}).
@return: L{Fsum2Tuple}C{(fsum, residual)} with C{fsum} the current precision running sum and C{residual}, the (precision) sum of the remaining C{partials}. The C{residual is INT0} if the C{fsum} is considered to be I{exact}.
@see: Methods L{Fsum.fint2}, L{Fsum.fsum} and L{Fsum.fsum2_} ''' t = t.dup(name=_xkwds_get(name, name=NN))
'''Add any positional C{scalar} or L{Fsum} instances and return the precision running sum and the C{differential}.
@arg xs: Values to add (C{scalar} or L{Fsum} instances), all positional.
@return: 2-Tuple C{(fsum, delta)} with the current precision running C{fsum} and C{delta}, the difference with the previous running C{fsum} (C{float}s).
@see: Methods L{Fsum.fsum_} and L{Fsum.fsum}. ''' else: # PYCHOK no cover return p, _0_0
# ftruediv = __itruediv__ # for naming consistency
'''(INTERNAL) Apply C{B{self} /= B{other}}. ''' if not d: # PYCHOK no cover d = r raise self._ResidualError(op, other, r) else: d, n = other.as_integer_ratio() else: # PYCHOK no cover raise self._TypeError(op, other) # txt=_invalid_ d if isnan(d) else self._finite(n / d)) except Exception as x: E, t = _xError2(x) raise self._Error(op, other, E, txt=t)
'''Get the C{imaginary} part of this instance (C{0.0}, always).
@see: Properties L{Fsum.ceil}, L{Fsum.floor} and L{Fsum.real}. '''
'''Return this instance' current running sum as C{int} or C{float}.
@kwarg raiser: If C{True} throw a L{ResidualError} if the residual is non-zero.
@return: This C{integer} sum if this instance C{is_integer}, otherwise return the C{float} sum if the residual is zero or if C{B{raiser}=False}.
@raise ResidualError: Non-zero residual and C{B{raiser}=True}.
@see: Methods L{Fsum.fint} and L{Fsum.fint2}. ''' if r and raiser: # PYCHOK no cover t = _stresidual(_non_zero_, r) raise ResidualError(int_float=s, txt=t)
'''Is this instance' current running C{fsum} considered to be exact? (C{bool}). '''
'''Is this instance' current running sum C{integer}? (C{bool}).
@see: Methods L{Fsum.fint} and L{Fsum.fint2}. '''
'''Return whether functions L{fsum}, L{fsum_}, L{fsum1} and L{fsum1_} plus partials summation are based on Python's C{math.fsum} or not.
@return: C{2} if all functions and partials summation are based on C{math.fsum}, C{True} if only the functions are based on C{math.fsum} (and partials summation is not) or C{False} if none are. '''
'''(INTERNAL) Return C{B{self} * Fsum B{other}} as L{Fsum}. ''' # assert isinstance(other, Fsum)
'''(INTERNAL) Return C{B{self} * scalar B{factor}} as L{Fsum} or C{0}. ''' # assert isscalar(factor) else:
'''Get this instance' current partial sums (C{tuple} of C{float}s and/or C{int}s). '''
'''Return C{B{self}**B{x}} as L{Fsum}.
@arg x: The exponent (L{Fsum} or C{scalar}). @arg mod: Optional modulus (C{int} or C{None}) for the 3-argument C{pow(B{self}, B{other}, B{mod})} version.
@return: The C{pow(self, B{x})} or C{pow(self, B{x}, *B{mod})} result (L{Fsum}).
@note: If B{C{mod}} is given as C{None}, the result will be an C{integer} L{Fsum} provided this instance C{is_integer} or set C{integer} with L{Fsum.fint}.
@see: Methods L{Fsum.__ipow__}, L{Fsum.fint} and L{Fsum.is_integer}. ''' else:
'''(INTERNAL) Return B{C{self}**1} or C{B{self}**0 == 1.0}. '''
'''(INTERNAL) 2-arg C{pow(B{self}, scalar B{x})} embellishing errors. ''' # assert len(self._ps) == 1 and isscalar(x) # neg**frac == complex in Python 3+, but ValueError in 2- E, t = _ValueError, _strcomplex(s, b, x) # PYCHOK no cover except Exception as x: E, t = _xError2(x) raise self._Error(op, other, E, txt=t)
'''(INTERNAL) 3-arg C{pow(B{self}, B{other}, int B{mod} or C{None})}. ''' else: # neg**frac == complex in Python 3+, but ValueError in 2- E, t = _ValueError, _strcomplex(s, b, x, mod) # PYCHOK no cover except Exception as x: E, t = _xError2(x) t = _COMMASPACE_(Fmt.PARENSPACED(mod=mod), t) raise self._Error(op, other, E, txt=t)
'''(INTERNAL) Return C{B{self} **= B{x}} for C{int B{x} >= 0}. ''' # assert isint(x) and x >= 0 else: else: # self**1 or self**0 else: # PYCHOK no cover # 0**pos_int == 0, but 0**0 == 1 f = 0 if x else 1 # like ._fprs
'''(INTERNAL) Return C{self**B{x}} for C{scalar B{x}}. ''' # assert x < 0 # < -1 s, r = f._fprs2 if r: return self._copy_0(_1_0)._ftruediv(f, op) # use **= -1 for the CPython float_pow # error if s is zero, and not s = 1 / s x = -1 # elif y > 1: # self**2 or self**-2 # f = self._mul_Fsum(self, op) # if x < 0: # f = f._copy_0(_1_0)._ftruediv(f, op) # return f else: # self**1 or self**0 raise self._TypeError(op, other, txt=_not_scalar_) # raise self._ResidualError(op, other, r, fractional_power=x) raise self._Error(op, other, ResidualError, txt=t) # assert isscalar(s) and isscalar(x)
'''(INTERNAL) Yield partials, 1-primed and subtract any C{less}. '''
'''(INTERNAL) Yield partials, negated. '''
'''(INTERNAL) Yield all C{partials} times each B{C{factor}}, in total, up to C{len(partials) * len(factors)} items. ''' else: # PYCHOK no cover self._finite(p, op) # throw ValueError
'''Get the C{real} part of this instance (C{float}).
@see: Methods L{Fsum.__float__} and L{Fsum.fsum} and properties L{Fsum.ceil}, L{Fsum.floor}, L{Fsum.imag} and L{Fsum.residual}. '''
'''Get this instance' residual (C{float} or C{int}), the C{sum(partials)} less the precision running sum C{fsum}.
@note: If the C{residual is INT0}, the precision running C{fsum} is considered to be I{exact}.
@see: Methods L{Fsum.fsum}, L{Fsum.fsum2} and L{Fsum.is_exact}. '''
'''(INTERNAL) Does the ratio C{r / s} exceed threshold? '''
'''Get and set this instance' I{ratio} for raising L{ResidualError}s, overriding the default from env variable C{PYGEODESY_FSUM_RESIDUAL}.
@arg threshold: If C{scalar}, the I{ratio} to exceed for raising L{ResidualError}s in division and exponention, if C{None} restore the default set with env variable C{PYGEODESY_FSUM_RESIDUAL} or if omitted, keep the current setting.
@return: The previous C{RESIDUAL} setting (C{float}).
@raise ValueError: Negative B{C{threshold}}.
@note: A L{ResidualError} is thrown if the non-zero I{ratio} C{residual} / C{fsum} exceeds the B{C{threshold}}. ''' float(t) if isscalar(t) else ( # for backward ... _0_0 if bool(t) else _1_0)) # ... compatibility u = self._unstr(self.RESIDUAL, *threshold) raise _ValueError(u, RESIDUAL=t, txt=_negative_)
'''(INTERNAL) Non-zero B{C{residual}} etc. ''' RESIDUAL=self._RESIDUAL)
'''Determine the sign of this instance.
@kwarg res: If C{True} consider, otherwise ignore the residual (C{bool}).
@return: The sign (C{int}, -1, 0 or +1). '''
'''Return this C{Fsum} instance as representation.
@kwarg prec_sep_fmt_lenc: Optional keyword arguments for method L{Fsum2Tuple.toRepr} plus C{B{lenc}=True} (C{bool}) to in-/exclude the current C{[len]} of this L{Fsum} enclosed in I{[brackets]}.
@return: This instance (C{repr}). '''
'''Return this C{Fsum} instance as string.
@kwarg prec_sep_fmt_lenc: Optional keyword arguments for method L{Fsum2Tuple.toStr} plus C{B{lenc}=True} (C{bool}) to in-/exclude the current C{[len]} of this L{Fsum} enclosed in I{[brackets]}.
@return: This instance (C{str}). '''
'''(INTERNAL) Helper for C{toRepr} and C{toStr}. '''
def _TypeError(self, op, other, **txt): # PYCHOK no cover '''(INTERNAL) Return a C{TypeError}. ''' return self._Error(op, other, _TypeError, **txt)
'''(INTERNAL) Zap all cached C{Property_RO} values. '''
def _ValueError(self, op, other, **txt): # PYCHOK no cover '''(INTERNAL) Return a C{ValueError}. ''' return self._Error(op, other, _ValueError, **txt)
def _ZeroDivisionError(self, op, other, **txt): # PYCHOK no cover '''(INTERNAL) Return a C{ZeroDivisionError}. ''' return self._Error(op, other, _ZeroDivisionError, **txt)
'''(INTERNAL) Unit of L{Fsum2Tuple} items. '''
'''2-Tuple C{(fsum, residual)} with the precision running C{fsum} and the C{residual}, the sum of the remaining partials. Each item is either C{float} or C{int}.
@note: If the C{residual is INT0}, the C{fsum} is considered to be I{exact}, see method L{Fsum2Tuple.is_exact}. '''
'''Get this L{Fsum2Tuple} as an L{Fsum}. ''' f = Fsum(name=self.name) return f._copy_0(*(s for s in reversed(self) if s))
'''Is this L{Fsum2Tuple} considered to be exact? (C{bool}). ''' return self.Fsum.is_exact()
'''Is this L{Fsum2Tuple} C{integer}? (C{bool}). ''' return self.Fsum.is_integer()
'''Error raised for an operation involving a L{pygeodesy.sums.Fsum} instance with a non-zero C{residual}, I{integer} or otherwise.
@see: Module L{pygeodesy.fsums} and method L{Fsum.RESIDUAL}. '''
# make sure _fsum works as expected (XXX check # float.__getformat__('float')[:4] == 'IEEE'?) if _fsum((1, 1e101, 1, -1e101)) != 2: # PYCHOK no cover del _fsum # nope, remove _fsum ... raise ImportError # ... use _fsum below
except ImportError:
def _fsum(xs): '''(INTERNAL) Precision summation, Python 2.5-. ''' return Fsum(name=_fsum.__name__)._facc(xs, up=False)._fprs
'''Precision floating point summation based on or like Python's C{math.fsum}.
@arg xs: Iterable, list, tuple, etc. of values (C{scalar} or L{Fsum} instances). @kwarg floats: Optionally, set C{B{floats}=True} iff I{all} B{C{xs}} are known to be C{float}.
@return: Precision C{fsum} (C{float}).
@raise OverflowError: Partial C{2sum} overflow.
@raise TypeError: Non-scalar B{C{xs}} value.
@raise ValueError: Invalid or non-finite B{C{xs}} value.
@note: Exceptions and I{non-finite} handling may differ if not based on Python's C{math.fsum}.
@see: Class L{Fsum} and methods L{Fsum.fsum} and L{Fsum.fadd}. '''
'''Precision floating point summation of all positional arguments.
@arg xs: Values to be added (C{scalar} or L{Fsum} instances), all positional. @kwarg floats: Optionally, set C{B{floats}=True} iff I{all} B{C{xs}} are known to be C{float}.
@return: Precision C{fsum} (C{float}).
@see: Function C{fsum}. ''' else: # PYCHOK no cover return _0_0
'''Precision floating point summation of a few values, 1-primed.
@arg xs: Iterable, list, tuple, etc. of values (C{scalar} or L{Fsum} instances). @kwarg floats: Optionally, set C{B{floats}=True} iff I{all} B{C{xs}} are known to be C{float}.
@return: Precision C{fsum} (C{float}).
@see: Function C{fsum}. '''
'''Precision floating point summation of a few arguments, 1-primed.
@arg xs: Values to be added (C{scalar} or L{Fsum} instances), all positional. @kwarg floats: Optionally, set C{B{floats}=True} iff I{all} B{C{xs}} are known to be C{float}.
@return: Precision C{fsum} (C{float}).
@see: Function C{fsum} ''' else: # PYCHOK no cover return _0_0
# **) MIT License # # Copyright (C) 2016-2022 -- mrJean1 at Gmail -- All Rights Reserved. # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. |