2024-12-04 03:50 PM
Hi all,
I am stumped with an issue that looks trivial but apparently isn't really ?
This code... :
double TMP117ConvertTemperatureToCelsius(uint16_t temperature) {
double temp_double = (double)temperature;
double multiplied = (double)temp_double * 7.8125f;
double converted = (double)multiplied / 1000.0f;
return (converted);
}
Gives with the debugger :
- temperature (uint16_t) =2627 (correct)
- temp_double (double) = 2627 (correct)
- multiplied (double) = 20523.4375 (correct)
- converted (double) = 0 (???)
And this code... :
double TMP117ConvertTemperatureToCelsius(uint16_t temperature) {
double temp_double = (double)temperature;
double converted = (double)temp_double * 0.0078125f;
return (converted);
}
gives :
- temperature (uint16_t) =2626 (correct)
- temp_double (double) = 2626 (correct)
- converted (double) = 3.0981138548092976e-312 (?????)
Have I overlooked something really basic or is something very wrong ? This is a question I never thought I'd ask but oh well :grinning_face_with_sweat:
Thanks in advance !
Solved! Go to Solution.
2024-12-05 03:02 AM
Thanks all for your help !
Somehow, switching to using floats solved it, this code produces the expected result :
float TMP117ConvertTemperatureToCelsius(uint16_t temperature) {
return ((float)temperature * 0.0078125);
}
I have no idea why doubles did struggle here unfortunately, but this seems to do the trick, and the precision will be fine for the temperature conversion... Will probably have to look into it in more details sometime :)
Thanks again !
2024-12-04 04:23 PM
I ran both of your functions on a STM32G071RB and I get a return value of 20.523438
2024-12-04 04:31 PM
Thanks a lot Karl for taking your time with this ! This is indeed what I expect the result to be, but for some reason, I can't seem to convince floats or doubles to behave as expected here. It's strange as I am using them with no trouble at all in other parts of my code. The debugger seems to uphold the weird results for now, as well as another part of the code that uses this function to compare the temperature to a threshold which it then never reaches despite it being so low that it should trigger right away... Really quite stumped with this one...
2024-12-04 04:51 PM
Have you tried examining the .lst file? Sometimes the debugger interprets a value differently from how an instruction will.
2024-12-04 04:57 PM
Not clear on any of the tools here, versions, etc.
Check the disassembly, confirm the library, make sure to enable floating point printf() functionality so you can confirm computations with that.
Not sure why you're using float constants here, or why it should take three steps.
Not sure I trust the debugger.
Perhaps check the stack usage
2024-12-04 08:29 PM
Only for sake of debugging, make converted volatile.
JW
2024-12-05 03:02 AM
Thanks all for your help !
Somehow, switching to using floats solved it, this code produces the expected result :
float TMP117ConvertTemperatureToCelsius(uint16_t temperature) {
return ((float)temperature * 0.0078125);
}
I have no idea why doubles did struggle here unfortunately, but this seems to do the trick, and the precision will be fine for the temperature conversion... Will probably have to look into it in more details sometime :)
Thanks again !
2024-12-05 04:16 AM
Gotta look at the lst file.