FitnessKit / FitDataProtocol

Swift Implementation the Garmin Flexible and Interoperable Data Transfer Protocol.
https://fitnesskit.github.io/FitDataProtocol/
MIT License
51 stars 15 forks source link

Resolutionable: decoding to incorrect value #11

Closed rcw5 closed 5 years ago

rcw5 commented 5 years ago

There's a minor bug in the Resolutionable code where encoding a value then decoding it doesn't result in the original value. This affects certain fields with an offset such as altitude/enhanced_altitude. Fields without offsets are unaffected.

This is because the offset is added last when encoding, but removed last when decoding.

e.g. current implementation:

original = 1000, scale = 5, offset = 500
encoded = (1000 * 5) + 500 = 5500
decoded = (5500 * (1/5)) - 500 = 600

If I opened my fit files in other tools I saw the incorrect decoded value which suggested it was a problem with the encoding of the data.

Looking at the FIT protocol it appears the encoding logic should add the offset first, then multiply by the scale factor. This will give the original value after decoding.

encoded = (1000 + 500) * 5 = 7500
decoded = (7500 * (1/5)) - 500 = 1000
khoogheem commented 5 years ago

Thanks for bringing this to my attention a 1.0.1 has been pushed out that fixes this issue.