Coverage for pygeodesy/constants.py : 100%

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 -*-
modules and related functions L{pygeodesy.float_}, L{pygeodesy.isclose}, L{pygeodesy.isfinite}, L{pygeodesy.isinf}, L{pygeodesy.isint0}, L{pygeodesy.isnan}, L{pygeodesy.isnear0}, L{pygeodesy.isnear1}, L{pygeodesy.isneg0}, L{pygeodesy.isninf}, L{pygeodesy.isnon0} and L{pygeodesy.remainder}. ''' # make sure int/int division yields float quotient, see .basics
# from pygeodesy.lazily import _ALL_LAZY # from .errors # from pygeodesy.streprs import Fmt # from .unitsBase
except ImportError: # Python 2- _inf, _nan = float(_INF_), float(_NAN_)
'''(INTERNAL) New named, cached C{Float}. '''
'''(INTERNAL) New named, cached C{Radius}. '''
'''Get scalars as C{float} or I{intern}'ed C{float}.
@arg fs: One more values (C{scalar}), all positional. @kwarg sets: Use keyword argument C{B{sets}=True} to C{intern} each B{C{fs}}, otherwise don't.
@return: Single C{float} if only one B{C{fs}} is given, otherwise a tuple of C{float}s.
@raise TypeError: Some B{C{fs}} not C{scalar}. ''' _floats.get except Exception as x: _E, t = _xError2(x) _fs_i = Fmt.SQUARE(fs=i) raise _E(_fs_i, f, txt=t)
'''(INTERNAL) Cache initial C{float}s. '''
'''(INTERNAL) Return C{float(B{f})} or C{INT0}. '''
'''(INTERNAL) Cache a tuple of C{float}s. '''
'''(INTERNAL) Return reciprocal C{1 / B{x}}. '''
# _float = float # PYCHOK expected # del _floats # XXX zap floats cache never
# MAX_EXP = Int( MAX_EXP =_f_i.max_exp) # PYTHON system's max base 2 exponent # MIN_EXP = Int( MIN_EXP =_f_i.min_exp) # PYTHON system's min base 2 exponent # RADIX = Int( RADIX =_f_i.radix) # PYTHON system's float base except ImportError: # PYCHOK no cover DIG = Int( DIG =15) # PYCHOK system's 64-bit float decimal digits EPS = _Float(EPS =2.220446049250313e-16) # PYCHOK EPSilon 2**-52, M{EPS +/- 1 != 1} MANT_DIG = Int( MANT_DIG=53) # PYCHOK float mantissa bits ≈ 53 (C{int}) MAX = _Float(MAX =pow(_2_0, 1023) * (_2_0 - EPS)) # PYCHOK ≈ 10**308 # MAX_EXP = Int( MAX_ESP =1024) # 308 base 10 MIN = _Float(MIN =pow(_2_0, -1021)) # PYCHOK ≈ 10**-308 # MIN_EXP = Int(MIN_EXP =-1021) # -307 base 10 # RADIX = Int(Radix =2) # base
# _1EPS = _Float(_1EPS =_1_0 + EPS) # PYCHOK ≈ 1.0000000000000002 # _2_EPS = _Float(_2_EPS =_2_0 / EPS) # PYCHOK = 9007199254740992.0
# <https://Numbers.Computation.Free.FR/Constants/Miscellaneous/digits.html>
# sqrt(2) <https://WikiPedia.org/wiki/Square_root_of_2> # 1.414213562373095_048_801_688_724_209_698_078_569_671_875_376_948_073_176_679_737_99 # _1SQRT2 = _Float(_1SQRT2 =sqrt(_2_0) + 1)
# See <https://www.EdWilliams.org/avform.htm>, <https://www.DTIC.mil/dtic/tr/fulltext/u2/a216843.pdf> # and <https://GitHub.com/NASA/MultiDop/blob/master/src/share/man/man3/geog_lib.3> based on the # International Standard Nautical Mile of 1,852 meter (1' latitude) # <http://Wiki.GIS.com/wiki/index.php/Ellipsoidal_quadratic_mean_radius> # Rtri= _Radius(Rtri=6372797.5559594) # PYCHOK Rtriaxial quadratic mean radius (C{meter}), WGS84 # Rbi = _Radius(Rbi =6367453.6345163) # PYCHOK Rbiaxial quadratic mean radius (C{meter}), WGS84
'''(INTERNAL) Return an C{B{n}-tuple} of C{_0_0} zeros. '''
except ImportError: # Python 3.4-
def _isclose(a, b, rel_tol=1e-9, abs_tol=0): '''Mimick Python 3.5+ C{math.isclose}. ''' t, d = abs_tol, abs(a - b) if d > t: r = max(abs(a), abs(b)) * rel_tol t = max(r, t) return d <= t
'''Like C{math.isclose}, but with defaults such that C{isclose(0, EPS0)} is C{True} by default. '''
except ImportError: # Python 3.1-
def _isfinite(x): '''Mimick Python 3.2+ C{math.isfinite}. ''' return not (isinf(x) or isnan(x))
'''Check a finite C{scalar} or C{complex} value.
@arg obj: Value (C{scalar} or C{complex}).
@return: C{False} if B{C{obj}} is C{INF}, C{NINF} or C{NAN}, C{True} otherwise.
@raise TypeError: Non-scalar and non-complex B{C{obj}}. ''' except Exception as x: if iscomplex(obj): # _isfinite(complex) thows TypeError return isfinite(obj.real) and isfinite(obj.imag) raise _xError(x, Fmt.PAREN(isfinite.__name__, obj))
'''Check for L{INT0} or C{int(0)} value.
@arg obj: The object (any C{type}). @kwarg both: If C{true}, also check C{float(0)} (C{bool}).
@return: C{True} if B{C{obj}} is L{INT0}, C{int(0)} or C{float(0)}, C{False} otherwise. ''' (not obj) and isint(obj, both=True))) and not isbool(obj)
'''Is B{C{x}} near zero within a tolerance?
@arg x: Value (C{scalar}). @kwarg eps0: Near-zero tolerance (C{EPS0}).
@return: C{True} if C{abs(B{x}) < B{eps0}}, C{False} otherwise.
@see: Function L{isnon0}. '''
'''Is B{C{x}} near one within a tolerance?
@arg x: Value (C{scalar}). @kwarg eps0: Near-one tolerance (C{EPS0}).
@return: C{isnear0(B{x} - 1)}.
@see: Function L{isnear0}. '''
'''Check for L{NEG0}, negative C{0.0}.
@arg x: Value (C{scalar}).
@return: C{True} if B{C{x}} is C{NEG0} or C{-0.0}, C{False} otherwise. ''' # and str(x).startswith(_MINUS_)
'''Check for L{NINF}, negative C{INF}.
@arg x: Value (C{scalar}).
@return: C{True} if B{C{x}} is C{NINF} or C{-inf}, C{False} otherwise. '''
'''Is B{C{x}} non-zero with a tolerance?
@arg x: Value (C{scalar}). @kwarg eps0: Non-zero tolerance (C{EPS0}).
@return: C{True} if C{abs(B{x}) > B{eps0}}, C{False} otherwise.
@see: Function L{isnear0}. '''
'''(INTERNAL) Off 90.0 for .gars and .wgrs. '''
except ImportError: # Python 3.6- from math import fmod as _fmod
def remainder(x, y): '''Mimick Python 3.7+ C{math.remainder}. ''' if isnan(y): x = NAN elif x and not isnan(x): y = abs(y) x = _fmod(x, y) h = _0_5 * y if x >= h: x -= y elif x < -h: x += y return x # keep signed 0.0
'''(INTERNAL) Non-negative C{deg} modulo 360, basic C{.utily.wrap360}. '''
if __name__ == '__main__':
from pygeodesy.errors import itemsorted from pygeodesy.lazily import printf
t = n = v = [] for n, v in itemsorted(locals()): if isinstance(v, (Float, Int, Radius)): printf('%9s: %r', n, v.toRepr(std=False)) if v.name != n: raise AssertionError('%r != %r' % (n, v)) if v.name is not n: raise AssertionError('%r is not %r' % (n, v)) if not n.startswith(_UNDER_): t.append(n) t.append(float_.__name__) printf('__all__ = %r', tuple(t))
# **) 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. |