Changeset 2306 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jan 31, 2019, 2:20:35 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • `almost_zero': Function to provide if two real values are the same up to certain power
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2298 r2306  
    6161# all_consecutive_combs: Function to provide all the consecutive possible
    6262#   combinations from a series of indices
     63# almost_zero: Function to provide if two real values are the same up to certain power
    6364# angle_DegMinSec: Function to transform an angle to Degrees Minutes Seconds
    6465# ASCIIfile_stats: Function to provide the statistics of a series of values from an ASCII file
     
    1507815079    return newarray, newdims
    1507915080
     15081def almost_zero(val1, val2, powv):
     15082    """ Function to provide if two real values are the same up to certain power
     15083      val1, val2: values to compare
     15084      powv: power to use to determine zero (as decimal)
     15085    >>> almost_zero(1.12345, 1.12346, 3)
     15086    True
     15087    >>> almost_zero(1.12345, 1.12346, 6)
     15088    False
     15089    """
     15090    fname = 'almost_zero'
     15091
     15092    pot = 10.**powv
     15093
     15094    diff = val1 - val2
     15095    adiff = np.abs(diff)
     15096    idiff = int(adiff*pot)
     15097
     15098    fdiff = idiff/pot
     15099
     15100    iszero = np.abs(fdiff - diff) < 1./pot
     15101    print diff, idiff, fdiff, np.abs(fdiff - diff), ':', 1./pot
     15102
     15103    return iszero
     15104
    1508015105#quit()
    1508115106
Note: See TracChangeset for help on using the changeset viewer.