cancel
Showing results for 
Search instead for 
Did you mean: 

Operations on doubles with very unexpected results

4lchemist
Associate II

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 !

 

1 ACCEPTED SOLUTION

Accepted Solutions
4lchemist
Associate II

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 !

View solution in original post

7 REPLIES 7
Karl Yamashita
Lead III

I ran both of your functions on a STM32G071RB and I get a return value of 20.523438

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

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...

Carl_G
Associate III

Have you tried examining the .lst file? Sometimes the debugger interprets a value differently from how an instruction will.

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Only for sake of debugging, make converted volatile.

JW

4lchemist
Associate II

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 !

Gotta look at the lst file.