Converts a number into a tuple with a number and exponent as power of 3 or as scale factor. :param number: Anything representing a number :type mumber: str, int, float, sympy.Expr, sympy.Float :param scaleFactors: if 'True', scale factors 'y', 'z', 'a', 'f', 'p', 'n',
(number, scaleFactors=False)
| 2060 | return (coeffDict) |
| 2061 | |
| 2062 | def ENG(number, scaleFactors=False): |
| 2063 | """ |
| 2064 | Converts a number into a tuple with a number and exponent as power of 3 or |
| 2065 | as scale factor. |
| 2066 | |
| 2067 | :param number: Anything representing a number |
| 2068 | :type mumber: str, int, float, sympy.Expr, sympy.Float |
| 2069 | |
| 2070 | :param scaleFactors: if 'True', scale factors 'y', 'z', 'a', 'f', 'p', 'n', |
| 2071 | 'u', 'm', 'k', 'M', 'G', 'T', and 'P' will be returned |
| 2072 | instead of exponents -24, -21, -18, -15, -12, -9, -6, |
| 2073 | -3, 3, 6, 9, 12, and 15, respectively. |
| 2074 | :type scaleFactors: Bool |
| 2075 | |
| 2076 | :return: number, exp |
| 2077 | :rtype: tuple: |
| 2078 | |
| 2079 | - number: int, float, or input type if conversion failed |
| 2080 | - exp: int, str (in case of scaleFactors == True), or None, |
| 2081 | if conversion failed |
| 2082 | |
| 2083 | Example: |
| 2084 | |
| 2085 | >>> import SLiCAP as sl |
| 2086 | >>> import sympy as sp |
| 2087 | |
| 2088 | >>> sl.ini.disp # number of significant digits to be diplayed |
| 2089 | 4 |
| 2090 | |
| 2091 | >>> sl.ENG(sp.sqrt(sp.pi)) |
| 2092 | (1.772, 0) |
| 2093 | |
| 2094 | >>> sl.ENG(1234567890) |
| 2095 | (1.234, 9) |
| 2096 | |
| 2097 | >>> sl.ENG(1234567890, scaleFactors=True) |
| 2098 | (1.234, 'G') |
| 2099 | |
| 2100 | >>> sl.ENG(1.234567890E-4) |
| 2101 | (123.4, -6) |
| 2102 | |
| 2103 | >>> sl.ENG(1.234567890E-4, scaleFactors=True) |
| 2104 | (123.4, 'u') |
| 2105 | |
| 2106 | """ |
| 2107 | SCALEFACTORS = {-24: 'y', -21: 'z', -18: 'a', -15: 'f', -12: 'p', -9: 'n', |
| 2108 | -6: 'u', -3: 'm', +3: 'k', +6: 'M', +9: 'G', +12: 'T', +15: 'P'} |
| 2109 | exp = None |
| 2110 | try: |
| 2111 | if type(number) != float: |
| 2112 | number = float(number) |
| 2113 | absValue = np.abs(number) |
| 2114 | if absValue != 0: |
| 2115 | # Engineering exponent = largest multiple of 3 not exceeding log10. |
| 2116 | # floor() rounds correctly for both signs (int() truncated toward |
| 2117 | # zero, needing a -=3 hack that over-corrected exact powers of 1000, |
| 2118 | # e.g. 1n -> 1000e-12 instead of 1e-9). log10 is first rounded so |
| 2119 | # floating-point noise does not push a boundary value across a |