| 1 | import math |
| 2 | |
| 3 | INF = float("inf") |
| 4 | MINUS_INF = float("-inf") |
| 5 | NaN = float("NaN") |
| 6 | |
| 7 | |
| 8 | def floatToGoString(d): |
| 9 | d = float(d) |
| 10 | if d == INF: |
| 11 | return '+Inf' |
| 12 | elif d == MINUS_INF: |
| 13 | return '-Inf' |
| 14 | elif math.isnan(d): |
| 15 | return 'NaN' |
| 16 | else: |
| 17 | s = repr(d) |
| 18 | dot = s.find('.') |
| 19 | # Go switches to exponents sooner than Python. |
| 20 | # We only need to care about positive values for le/quantile. |
| 21 | if d > 0 and dot > 6: |
| 22 | mantissa = f'{s[0]}.{s[1:dot]}{s[dot + 1:]}'.rstrip('0.') |
| 23 | return f'{mantissa}e+0{dot - 1}' |
| 24 | return s |