[Nut-upsdev] Use double instead of float?
Peter Selinger
selinger at mathstat.dal.ca
Wed Aug 15 19:11:10 UTC 2007
That code is stupid right now. For example, if a UPS gives a voltage
with precision 0.1V, the driver will actually convert this to an
integer.
Double is better than float in any case. But some of the conversion
functions physical<->logical should use integers for logical and
doubles for physical units.
-- Peter
Arjen de Korte wrote:
>
> 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