utils.py 621 B

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