Scale val by the rational number num / denom, avoiding overflows and underflows and without loss of precision.
val
num
denom
num must be non-negative and denom must be positive.