[Nut-upsdev] Use double instead of float?

Arjen de Korte nut+devel at de-korte.org
Wed Aug 15 19:04:37 UTC 2007


Hi Peter,

I was browsing through the usbhid-ups debug output and came to the
conclusion that we might be running into problems with the conversion
between long and float (and back). Not only because we happily cast a
float to a long (which I understand is not always portable, since some
systems will round and others truncate the value).

Being equal in size on many systems (including mine), a float is not big
enough to hold a large long without loss of precision:

=>> Before exponent: 124735908, 0/0)
PhyMax = 0, PhyMin = 0, LogMax = 2147483647, LogMin = 0
=>> After conversion: 124735904.000000 (124735904), 0/0)

If this was a status, we would be in trouble (fortunately, it is just a
timer value).

A mitigating factor is that in most cases variables returned from the
UPS will not be larger than unsigned. I have yet to see the first UPS
that sports a 16 bit A/D converter, so for voltage and current
measurements this is not likely going to be a problem. But for larger
values (do we have 16 bit status words?) it would surely help if we
would increase the size to a double, allow for a lossless conversion.

Best regards, Arjen



More information about the Nut-upsdev mailing list