Debian Codegraph @

api/v1/lib/fixedpoint.go

raw symbols SCIP
1 package mesos
2
3 // fixed point scalar math from mesos:src/common/values.cpp
4 // --
5 // We manipulate scalar values by converting them from floating point to a
6 // fixed point representation, doing a calculation, and then converting
7 // the result back to floating point. We deliberately only preserve three
8 // decimal digits of precision in the fixed point representation. This
9 // ensures that client applications see predictable numerical behavior, at
10 // the expense of sacrificing some precision.
11
12 import "math"
13
14 func convertToFloat64(f int64) float64 {
15 // NOTE: We do the conversion from fixed point via integer division
16 // and then modulus, rather than a single floating point division.
17 // This ensures that we only apply floating point division to inputs
18 // in the range [0,999], which is easier to check for correctness.
19 var (
20 quotient = float64(f / 1000)
21 remainder = float64(f%1000) / 1000.0
22 )
23 return quotient + remainder
24 }
25
26 func convertToFixed64(f float64) int64 {
27 return round64(f * 1000)
28 }
29
30 func round64(f float64) int64 {
31 if math.Abs(f) < 0.5 {
32 return 0
33 }
34 return int64(f + math.Copysign(0.5, f))
35 }