korken89 / fugit

`fugit` provides a comprehensive library of `Duration` and `Instant` for the handling of time in embedded systems, doing all it can at compile time.
Apache License 2.0
55 stars 16 forks source link

Better rounding on rate conversion? #50

Open thejpster opened 1 month ago

thejpster commented 1 month ago

When doing rate conversion, fugit multiplies by RD_TIMES_LN and divides by LD_TIMES_RN in https://github.com/korken89/fugit/blob/0ad21f78f7d0b4f691ba9c7445f93299039d9f54/src/rate.rs#L220.

Should it add half of LD_TIMES_RN to do half-up rounding?

I expect Rate<u32, 1, 1>(12_000_000) to convert to Rate<u32, 1000, 65536>(0x0020c49c) but instead I get Rate<u32, 1000, 65536>(0x0020c49b).

Converting 0x0020c49b back I get 32767.990 (error 0.01) whereas 0x0020c49c gives 32768.005 (error 0.005).

Am I holding it wrong?