This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Calculation for temperature false

In the data sheet of SHT21 is this the formula to calculate the temperature:

T = -46.85 + 175.72 * temp_data / 2^16

temp_data has the value 26'464.

The result should be: T = 24.1°C

But in my code I get this result = 18052.3105

Code:

float temperature;

temperature = (-46.85 + (175.72*(temperature_data / 65536)));

What is wrong with my calculation for the temperature?

Parents
  • That doesn't make sense however. If temperature_data is an int, or integral type, and really has the value '26,464', then the division would be done as integer division, the result would be zero (as 26464 < 65536) and the entire expression would then evaluate to -46.85, which it doesn't, according to the original poster.

    If you work backwards, the only way you get 18052.3105 is if the initial value is 26464 x 256. At that point yes the integer division rounds off the result to 103 (instead of 103.375) and the value would then work out to 18052.3105.

    So it seems to me the OP has temp_data wrong by a factor of 256. When they correct that then I will agree that they will have integer division issues with their code as-written.

    edit: and I'm also totally in agreement with the 'avoid floats' comment. You can do without them here with a little bit of judicious scaling.

Reply
  • That doesn't make sense however. If temperature_data is an int, or integral type, and really has the value '26,464', then the division would be done as integer division, the result would be zero (as 26464 < 65536) and the entire expression would then evaluate to -46.85, which it doesn't, according to the original poster.

    If you work backwards, the only way you get 18052.3105 is if the initial value is 26464 x 256. At that point yes the integer division rounds off the result to 103 (instead of 103.375) and the value would then work out to 18052.3105.

    So it seems to me the OP has temp_data wrong by a factor of 256. When they correct that then I will agree that they will have integer division issues with their code as-written.

    edit: and I'm also totally in agreement with the 'avoid floats' comment. You can do without them here with a little bit of judicious scaling.

Children
No Data
Related