I have a function that takes in a 16 bit unsigned integer value and converts it to a double. The data comes from a temperature sensor (MCP9808) and the function converts it to degrees Celsius.
The function is defined as follows:
double convert_tempc(uint16_t data) {
double temp = data & 0x0FFF;
temp /= 16.0;
if (data & 0x1000) temp -= 256;
return temp;
}
My question is, how can I perform the inverse of this series of operations?
I need a function to find the 16-bit value produced by this sensor that results in a given temperature in degrees Celsius.
Since the 16-bit value can only represent a finite number of values, I imagine the inverse function would need to find the nearest double that can be represented, and then perform the inverse conversion.
Is there any simple way to do this?
The function is written in C, but I will be implementing it in Python.