You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
612 B

  1. package humanize
  2. import (
  3. "math/big"
  4. )
  5. // order of magnitude (to a max order)
  6. func oomm(n, b *big.Int, maxmag int) (float64, int) {
  7. mag := 0
  8. m := &big.Int{}
  9. for n.Cmp(b) >= 0 {
  10. n.DivMod(n, b, m)
  11. mag++
  12. if mag == maxmag && maxmag >= 0 {
  13. break
  14. }
  15. }
  16. return float64(n.Int64()) + (float64(m.Int64()) / float64(b.Int64())), mag
  17. }
  18. // total order of magnitude
  19. // (same as above, but with no upper limit)
  20. func oom(n, b *big.Int) (float64, int) {
  21. mag := 0
  22. m := &big.Int{}
  23. for n.Cmp(b) >= 0 {
  24. n.DivMod(n, b, m)
  25. mag++
  26. }
  27. return float64(n.Int64()) + (float64(m.Int64()) / float64(b.Int64())), mag
  28. }