github.com/cznic/mathutil@v0.0.0-20181122101859-297441e03548/rat.go (about)

     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  
     5  package mathutil
     6  
     7  // QCmpUint32 compares a/b and c/d and returns:
     8  //
     9  //   -1 if a/b <  c/d
    10  //    0 if a/b == c/d
    11  //   +1 if a/b >  c/d
    12  //
    13  func QCmpUint32(a, b, c, d uint32) int {
    14  	switch x, y := uint64(a)*uint64(d), uint64(b)*uint64(c); {
    15  	case x < y:
    16  		return -1
    17  	case x == y:
    18  		return 0
    19  	default: // x > y
    20  		return 1
    21  	}
    22  }
    23  
    24  // QScaleUint32 returns a such that a/b >= c/d.
    25  func QScaleUint32(b, c, d uint32) (a uint64) {
    26  	return 1 + (uint64(b)*uint64(c))/uint64(d)
    27  }