|
| 1 | +package drivers |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "math" |
| 6 | + "math/big" |
| 7 | +) |
| 8 | + |
| 9 | +// Float32Fractions calculates the biggest possible denominator "den" for a given floating point number "f" and |
| 10 | +// returns this denominator together with the resulting nominator "nom", so "f" can be reconstructed by "f = num / den". |
| 11 | +// All values are in the range MaxInt32: 2147483647, MinInt32: -2147483648. If the given "f" exceeds this range, it is |
| 12 | +// not possible anymore to represent "f" with "f = num / 1" and an error will be returned with the nearest values. |
| 13 | +// As an exception we define that "den" is always positive, so negative numbers "f" leads always to negative "num". |
| 14 | +// |
| 15 | +// Used formulas: |
| 16 | +// For "abs(f)=af < 1" applies the biggest denominator: "den = math.MaxInt32" and "num = af * den". For "af > 0" this |
| 17 | +// can be written more generalized when split integer part "ip" from fractional part "fp" with "af = ip + fp": |
| 18 | +// "den = math.MaxInt32/(ip + 1)"; "num = af * den" |
| 19 | +// very good accuracy can be reached, similar to calculating with "math/big.Rat", but 2-15 times faster: |
| 20 | +// max. epsilon = 1.9073486328125e-06 in test for "17459216/697177" on arm64, but better for this example on MCU, |
| 21 | +// nrf52840 12.207µs-14.496µs (independent of used base) |
| 22 | +// |
| 23 | +// Sign: |
| 24 | +// "abs(MaxInt32) > abs (MinInt32)", the sign can be applied to "den" or "nom", but we already defined "den" as positive |
| 25 | +// |
| 26 | +// Considered other options: see function in test file |
| 27 | +func Float32Fractions(f float32) (int32, int32, error) { |
| 28 | + const baseMax = math.MaxInt32 |
| 29 | + //const baseMax = 1000000000 // 10 digits |
| 30 | + //const baseMax = 2000000000 // 10.5 digits |
| 31 | + ip, den, err := float32FractionsPreCheck(f) |
| 32 | + if den == 1 || err != nil { |
| 33 | + return ip, den, err |
| 34 | + } |
| 35 | + |
| 36 | + if f < 0 { |
| 37 | + ip = -ip |
| 38 | + } |
| 39 | + |
| 40 | + // see also "math.Modf()" |
| 41 | + den = baseMax / (ip + 1) |
| 42 | + if den == 0 { |
| 43 | + den = 1 |
| 44 | + } |
| 45 | + |
| 46 | + return int32(float32(den) * f), den, nil |
| 47 | +} |
| 48 | + |
| 49 | +// Float32FractionsBigRat uses the big/math go library for splitting the given value in fractions. This function seems |
| 50 | +// to produce more accurate results, but is 5-8 times slower than Float32Fractions (depending on value). |
| 51 | +// nrf52840: 74.768µs-106.049µs |
| 52 | +func Float32FractionsBigRat(f float32) (int32, int32, error) { |
| 53 | + if ip, den, err := float32FractionsPreCheck(f); den == 1 || err != nil { |
| 54 | + return ip, den, err |
| 55 | + } |
| 56 | + |
| 57 | + r := new(big.Rat).SetFloat64(float64(f)) |
| 58 | + d := r.Denom().Int64() //Denom returns the denominator of x; it is always > 0. |
| 59 | + if d > math.MaxInt32 { |
| 60 | + d = math.MaxInt32 |
| 61 | + } |
| 62 | + |
| 63 | + n := f * float32(d) |
| 64 | + if n > math.MaxInt32 { |
| 65 | + println("n ex+", n) |
| 66 | + return math.MaxInt32, int32(float32(math.MaxInt32) / f), nil |
| 67 | + } else if n < math.MinInt32 { |
| 68 | + println("n ex-", n) |
| 69 | + return math.MinInt32, int32(float32(math.MinInt32) / f), nil |
| 70 | + } |
| 71 | + |
| 72 | + return int32(n), int32(d), nil |
| 73 | +} |
| 74 | + |
| 75 | +func float32FractionsPreCheck(f float32) (int32, int32, error) { |
| 76 | + if f > math.MaxInt32 { |
| 77 | + return math.MaxInt32, 1, errors.New("input value exceeds +int32 range") |
| 78 | + } |
| 79 | + |
| 80 | + if f < math.MinInt32 { |
| 81 | + return math.MinInt32, 1, errors.New("input value exceeds -int32 range") |
| 82 | + } |
| 83 | + |
| 84 | + ip := int32(f) |
| 85 | + den := int32(0) |
| 86 | + |
| 87 | + if float32(ip) == f { |
| 88 | + // float is an integer |
| 89 | + den = 1 |
| 90 | + } |
| 91 | + |
| 92 | + return ip, den, nil |
| 93 | +} |
0 commit comments