File tree Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change @@ -21,16 +21,22 @@ def float_pow2str(v: float, min_exponent: float = -np.inf) -> str:
21
21
if not np .isfinite (v ):
22
22
return str (v )
23
23
24
- s = np .sign (v )
24
+ signstr = "-" if np .signbit (v ) else ""
25
+
25
26
x = np .abs (v )
26
- e = np .floor (np .log2 (x ))
27
- sig = x * 2.0 ** - e
27
+ e = int ( np .floor (np .log2 (x ) ))
28
+ sig = np . ldexp ( x , - e )
28
29
if e < min_exponent :
29
- sig *= 2.0 ** ( e - min_exponent )
30
+ sig = np . ldexp ( sig , e - min_exponent )
30
31
e = min_exponent
31
32
33
+ pow2str = f"2^{ int (e ):d} "
34
+
32
35
significand = fractions .Fraction (sig )
33
- return ("-" if s < 0 else "" ) + f"{ significand } *2^{ int (e ):d} "
36
+ if significand == 1 :
37
+ return signstr + pow2str
38
+ else :
39
+ return signstr + f"{ significand } *{ pow2str } "
34
40
35
41
36
42
def float_tilde_unless_roundtrip_str (v : float , width : int = 14 , d : int = 8 ) -> str :
You can’t perform that action at this time.
0 commit comments