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 anywhere between C{-1.0} and C{+1.0}, including C{INT0} if considered to be I{exact}.
Set env variable C{PYGEODESY_FSUM_RESIDUAL} to any non-empty string to throw a L{ResidualError} for division or exponention by an L{Fsum} instance with a non-zero C{residual}, 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
isscalar, map1, neg, signOf, _signOf _xError2, _xkwds_get, _ZeroDivisionError _from_, _iadd_, _not_finite_, _not_scalar_, \ _PERCENT_, _PLUS_, _SLASH_, _SPACE_, _STAR_, \ _0_0, _1_0, _N_1_0
'''(INTERNAL) Half-even rounding. ''' (r < 0 and p < 0): # signs match
'''(INTERNAL) Raise C{TypeError} or C{ValueError} if not scalar or infinite. ''' E, t = _ValueError, _not_finite_ 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) Return B{C{other}} as an L{Fsum} instance. ''' Fsum(name=name)._fset(_2float(other=other), asis=True)
'''(INTERNAL) Return B{C{other}} as C{int}, C{float} or C{as-is}. ''' if r: # PYCHOK no cover if raiser: raise ValueError(_2stresidual(_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}. ''' Fmt.g(s, prec=9) if isscalar(s) else repr(s))
'''(INTERNAL) Precision C{2sum} of M{a + b} as 2-tuple (sum, residual). ''' raise _OverflowError(unstr(_2sum.__name__, a, b), txt=str(s))
'''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 C{scalar} or other L{Fsum} instances with C{scalar} meaning type C{float}, C{int} or any C{type} convertible into a single-instance 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
'''New L{Fsum} for precision floating point I{running} summation.
@arg xs: No, one or more initial values (C{scalar} or L{Fsum} instances). @kwarg name_NN: Optional name (C{str}).
@see: Method L{Fsum.fadd}. ''' # self._n = 0
'''Return this instance' absolute value as an L{Fsum}. '''
'''Return the sum C{B{self} + B{other}} as an L{Fsum}.
@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 of values accumulated (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()
'''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 integer ratio.
@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) Subtract an B{C{other}} instance or scalar and return an L{Fsum2Tuple}C{(fsum, residual)} for comparison operator B{C{op}}. ''' raise self._TypeError(op, other, txt=_not_scalar_) else:
'''Copy this instance, C{shallow} or B{C{deep}}.
@return: The copy (L{Fsum}). ''' # f._update(other=self)
'''(INTERNAL) Fast, un-named 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) Accumulate more C{scalar}s. ''' # assert isscalar(x) and isfinite(x) # assert self._ps is ps
'''(INTERNAL) Accumulate all positional C{scalar}s. '''
'''(INTERNAL) Update the C{partials}, by removing and re-adding the final C{partial}. ''' else: # force zap
'''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}}. ''' else: raise self._TypeError(op, other, txt=_not_scalar_)
'''(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 = _2stresidual(_integer_, r) raise ResidualError(_integer_, i, txt=t)
'''Return this instance' current running sum as C{int} and the I{integer} residual.
@return: 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 I{exact}. '''
'''(INTERNAL) Get 2-tuple (C{int}, I{integer} residual). ''' # assert len(self._ps) > 0 (self._ps[0] - i) or INT0)
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}}. ''' else: raise self._TypeError(op, other, txt=_not_scalar_) else:
'''(INTERNAL) Negate this instance. '''
'''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: s = self._Fsum(i)._fpow(other, op) else: # pow(self, other) == pow(self, other, None) raise self._ResidualError(op, other, r) raise self._TypeError(op, other, txt=_not_scalar_) else: s *= p # each C{scalar} or L{Fsum}
'''(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: else: # PYCHOK no cover ps[i] = s = p # replace and continue # 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}. ''' else: # PYCHOK no cover raise self._TypeError(_fset_, 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=_not_scalar_)
'''(INTERNAL) Fast, single-scalar C{_Fsum}, named the same. '''
'''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).
@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_} '''
'''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}. '''
# ftruediv = __itruediv__ # for naming consistency
'''(INTERNAL) Apply C{B{self} /= B{other}}. ''' raise self._ResidualError(op, other, r) # self / (d + r) == self * n / d # n = d / (d + r) = 1 / (1 + r / d) # d' = d / n = d * (1 + r / d), but # may be moot if (1 + r / d) == 1 else: # PYCHOK no cover d = r else: # PYCHOK no cover raise self._TypeError(op, other) # txt=_invalid_ d if isnan(d) else self._finite(_1_0 / d)) except Exception as x: E, t = _xError2(x) raise self._Error(op, d, 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} or this C{float} sum if the residual is zero or ignored.
@raise ResidualError: Non-zero residual.
@see: Methods L{Fsum.fint} and L{Fsum.fint2}. ''' if r and raiser: # PYCHOK no cover t = _2stresidual(_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 C{True} if functions L{fsum}, L{fsum_}, L{fsum1} and L{fsum1_} are all based on Python's C{math.fsum}, C{False} otherwise. '''
'''(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: else: # to allow setting f._n
'''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, _2strcomplex(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, _2strcomplex(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}}. ''' _, r = s._fprs2 if r: s, x = self._Fsum(_1_0)._ftruediv(s, op), None else: # use **= -1 for the CPython float_pow # error if s is zero, and not s = 1 / s s, x = s._fprs, -1 else: else: # self**1 or self**0 raise self._TypeError(op, other, txt=_not_scalar_) raise self._ResidualError(op, other, r, fractional=x) # assert isscalar(s) and isscalar(x)
'''(INTERNAL) Yield partials, pseudo-sorted, 1-primed minus C{less} if non-zero. '''
'''(INTERNAL) Yield all C{partials} times each B{C{factor}}, in total 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 sum of the C{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}. '''
'''Do or don't raise L{ResidualError} exceptions for this instance, overriding the default from env var C{PYGEODESY_FSUM_RESIDUAL}.
@arg raiser: If C{True} throw L{ResidualError}s for division and exponention, if C{False} don't, if C{None} restore the default setting (C{bool}) and if omitted, maintain the current setting.
@return: The previous C{RESIDUAL} setting (C{bool}). '''
'''(INTERNAL) Non-zero B{C{residual}} and C{name=value, ...} error. '''
'''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: The C{float} precision, number of decimal digits (0..9). Trailing zero decimals are stripped for B{C{prec}} values of 1 and above, but kept for negative B{C{prec}} values. @kwarg sep: Separator to join (C{str}). @kwarg fmt: Optional C{float} format (C{str}).
@return: This instance (C{str}). '''
'''Return this C{Fsum} instance as string.
@kwarg prec: The C{float} precision, number of decimal digits (0..9). Trailing zero decimals are stripped for B{C{prec}} values of 1 and above, but kept for negative B{C{prec}} values. @kwarg sep: Separator to join (C{str}). @kwarg fmt: Optional C{float} format (C{str}).
@return: This instance (C{repr}). '''
def _TypeError(self, op, other, **txt): # PYCHOK no cover '''(INTERNAL) Return a C{TypeError}. ''' return self._Error(op, other, _TypeError, **txt)
'''(INTERNAL) Copy, set or zap all cached C{Property_RO} values. ''' else: # dup if present, otherwise zap # Property_RO _fint2, _fprs and _fprs2 can't be a Property: # Property's _fset zaps the value just set by the @setter
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) Fast, single-scalar L{Fsum}. '''
'''(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}. '''
'''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)._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).
@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.
@return: Precision C{fsum} (C{float}).
@see: Function C{fsum}. '''
'''Precision floating point summation of a few values, 1-primed.
@arg xs: Iterable, list, tuple, etc. of values (C{scalar} or L{Fsum} instances).
@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.
@return: Precision C{fsum} (C{float}).
@see: Function C{fsum} '''
# **) 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. |