rat.go 636 B

123456789101112131415161718192021222324252627
  1. // Copyright (c) 2014 The mathutil Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package mathutil
  5. // QCmpUint32 compares a/b and c/d and returns:
  6. //
  7. // -1 if a/b < c/d
  8. // 0 if a/b == c/d
  9. // +1 if a/b > c/d
  10. //
  11. func QCmpUint32(a, b, c, d uint32) int {
  12. switch x, y := uint64(a)*uint64(d), uint64(b)*uint64(c); {
  13. case x < y:
  14. return -1
  15. case x == y:
  16. return 0
  17. default: // x > y
  18. return 1
  19. }
  20. }
  21. // QScaleUint32 returns a such that a/b >= c/d.
  22. func QScaleUint32(b, c, d uint32) (a uint64) {
  23. return 1 + (uint64(b)*uint64(c))/uint64(d)
  24. }