Re: temperature standard - global config option?

Albert D. Cahalan
2001-06-09T08:03:43Z

Michael H. Warfiel writes:
> On Fri, Jun 08, 2001 at 05:16:39PM -0400, Albert D. Cahalan wrote:

>> The bits are free; the API is hard to change.
>> Sensors might get better, at least on high-end systems.
>> Rounding gives a constant 0.15 degree error.
>> Only the truly stupid would assume accuracy from decimal places.
>> Again, the bits are free; the API is hard to change.
...
> No... The average person, NO, the vast majority of people,
> DO assume accuracy from decimal places and honestly do not know the
> difference between precision and accuracy. I've had comments on this
> thread in private E-Mail the reinforce this impression.

I hope you don't think people would assume that a "float" always
has useful data in all 23 fraction bits. It is a similar case.

So here you go, a kernel-safe conversion from C to K. It works
from 0 to 238 degrees C. Print as hex, so user code can toss it
into a union or maybe abuse scanf. Adjust as needed for F to K
or for hardware with greater resolution.

/* unsigned int degrees C --> float degrees K */
unsigned ic_to_fk(unsigned c){
unsigned exponent;
unsigned tmp;

tmp = (c<<23) + 0x88933333; /* Kelvin shifted 23 left */
exponent = 127; /* IEEE floating-point bias */
while(tmp&0xff000000){
tmp >>= 1;
exponent++;
}
tmp &= 0x007fffff; /* keep only the fraction */
tmp |= exponent<<23;
return tmp;
}