Skip to main content
Associate
December 4, 2024
Solved

Operations on doubles with very unexpected results

  • December 4, 2024
  • 7 replies
  • 1151 views

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 !

 

This topic has been closed for replies.
Best answer by 4lchemist

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 !

7 replies

Karl Yamashita
Principal
December 5, 2024

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

 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
4lchemistAuthor
Associate
December 5, 2024

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

Tesla DeLorean
Guru
December 5, 2024

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Senior II
December 5, 2024

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

waclawek.jan
Super User
December 5, 2024

Only for sake of debugging, make converted volatile.

JW

4lchemistAuthorBest answer
Associate
December 5, 2024

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 !

Senior II
December 5, 2024

Gotta look at the lst file.