Closed norgeous closed 7 years ago
Hey @norgeous,
Thanks for submitting, is there any particular reason "4dp precision." was selected?
Everything after the 4th digit is garbage. It is an effect of the way its calculated. There is no actual information after the 4th dp. I suspect that 4dp is the max resolution of the sensor. 4dp = mV resolution.
Battery voltage is 4.1107000000000005V
Battery voltage is 4.111800000000001V
As you can see from this sample, 4.1107000000000005
has no info other than 4.1107
, the 000000000005
part is meaningless.
@norgeous it would be nice to mathematically determine the precision using some info from the data sheet.
@sandeepmistry Looking at the data sheet, we can see:
Channel | Registers | 000h | Step | FFFh |
---|---|---|---|---|
Battery Voltage | 78h, 79h | 0 mV | 1.1 mV | 4.5045 V |
The precision is 4dp, with a 0.0011 V
step. There are 4095 possible values.
@norgeous nice! Thanks for the info from the data sheet!
Thanks @sandeepmistry, I agree with your comment. So after much research and testing, I think the best solution actually is:
var voltage = (value * 11) / 10000;
Simple right? I guess it's not a good idea to multiply by floating numbers in js. I'll change the PR to this way now...
Test case:
(3817 * 1.1) / 1000 = 4.1987000000000005
(3817 * 11) / 10000 = 4.1987
@norgeous thanks!
Simple right? I guess it's not a good idea to multiply by floating numbers in js. I'll change the PR to this way now...
I would say JS per say, it applies to floating point in all languages.
Observation
When running the command:
I get an output like:
There should not be multiple console.log of the same message.
Remove the fixed precision in the
examples/battery-voltage.js
to see what is happening, change:to:
Then I get an out like:
Conclusion
This PR changes the precision to 4 decimal places, and applies it (earlier) before the change event is emitted, resulting in an output like:
There are sequential duplicates and we now have 4dp precision.