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