how to calculate floating point in MCU with C

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hi guys

I have been teaching myself MCU and C randomly for a few months now (mainly through making stuff, if anything I don't know, google it).

So I recently making a temperature sensor with my new msp430 launchpad, dh11 temperature sensor and display it on a lcd0802(something like lcd1602), I got it working except I want to calculate the dew point, and I am stucked.

my code is:
Rich (BB code):
uint8_t calculate_dew_point(float temp, float r_humidity)
{
	return pow((r_humidity/100),(1/8))*(112+0.9*temp)+0.1*temp-112;
}
I couldn't get the same answer as my calculator, so what am I missing here? what do I need to google to get it work?

Thanks guys! :)
 

WBahn

Joined Mar 31, 2012
33,179
Unless you have the necessary floating point libraries for your compiler, I wouldn't bother with floating point; instead use fixed point.

If you do have the libraries, make note that (1/8) is zero. This is because both the numerator and denominator have been given as integers, which tells the compiler to perform integer division, which truncates any fractional part. In C, always be explicit when using floating point values, so use (1.0/8.0) instead.
 

MrChips

Joined Oct 2, 2009
35,167
Couple of points. Floating point calculations require a lot of resources from an MCU.
If you have limited memory space it is easier to do fixed point math. A simple solution is to multiply all values by 10 or 100 and manually insert the decimal point in the result.

The second point is proper dewpoint calculation is very complex and includes exponential functions. The equation you show is very simplistic.
 

spinnaker

Joined Oct 29, 2009
7,830
Couple of points. Floating point calculations require a lot of resources from an MCU.
If you have limited memory space it is easier to do fixed point math. A simple solution is to multiply all values by 10 or 100 and manually insert the decimal point in the result.

The second point is proper dewpoint calculation is very complex and includes exponential functions. The equation you show is very simplistic.
Well easier than figuring a way to make more memory space but not easier than using floating point if you have the memory.
 

spinnaker

Joined Oct 29, 2009
7,830
To WBahns point. I would check the compiler output and make sure there are no warnings. A warning will allow compile but might not allow the code to work as you think it should.


Also you can break your formula into several lines of code using some temp variables. Step through the code with the debugger and watch the calculations. Once you get the problem figured out, you can put things back together in one formula.
 

be80be

Joined Jul 5, 2008
2,395
The compiler I use can do floating point math I was going crazy trying to figure why it was rounding the numbers you had to force it to like shown 1000/23 would round off the left of the decimal, but 1000.0/23 would give you the right side to 3 places which is changeable
for how many places you want.
 

spinnaker

Joined Oct 29, 2009
7,830
The compiler I use can do floating point math I was going crazy trying to figure why it was rounding the numbers you had to force it to like shown 1000/23 would round off the left of the decimal, but 1000.0/23 would give you the right side to 3 places which is changeable
for how many places you want.
How is this helping the OP?
 

be80be

Joined Jul 5, 2008
2,395
Where your help spinnaker

The op may read it do like I did and find out how to use floating point math some compiler you have to force them to use floating point. I use Code Composer Studio on the chip the OP is using MSP430

((If he is using a LCD library his string function may be the problem His code as posted looks fine ))

And back a Spinnaker the debugger showed the right numbers My LCD didn't
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
Hey bug. Did the thank you in post #5 help you to figure out the problem? Or is it just a general thank you.

You are welcome either way.
 

be80be

Joined Jul 5, 2008
2,395
Hey bug. Did the thank you in post #5 help you to figure out the problem? Or is it just a general thank you.

You are welcome either way.
What are you talking about I fixed my problem days before I read this stuff

I was playing with some math just like the OP and with a calculator it worked out fine but On the LCD the numbers where totally wrong.

I was how I handled the string to the LCD it couldn't handle floating point so the compiler changed them to integers
 

spinnaker

Joined Oct 29, 2009
7,830
What are you talking about
That is what I should be asking you. :rolleyes:


I fixed my problem days before I read this stuff

I was playing with some math just like the OP and with a calculator it worked out fine but On the LCD the numbers where totally wrong.

I was how I handled the string to the LCD it couldn't handle floating point so the compiler changed them to integers

I'm glad you fixed your problem but this isn't your thread. Unless you are trying to help the OP please do no post here.
 

be80be

Joined Jul 5, 2008
2,395
spinnaker There a name for some one like you

I was trying to Point out two facts

For the OP One like been said He may need to force the compiler to do the math in floating point

And two the problem may be just the LCD part of the program mine was.

And if you spinnaker was so worried about helping some one you wouldn't be posting the gunk your posting " where that hepping."

I've seen your post you would be lucky if you could program a pop tart in a toaster oven.
 

MrChips

Joined Oct 2, 2009
35,167
spinnaker There a name for some one like you

I was trying to Point out two facts

For the OP One like been said He may need to force the compiler to do the math in floating point

And two the problem may be just the LCD part of the program mine was.

And if you spinnaker was so worried about helping some one you wouldn't be posting the gunk your posting " where that hepping."

I've seen your post you would be lucky if you could program a pop tart in a toaster oven.
Hey beb80be, such behavior is not tolerated on this forum.
This is not your thread, so beat it.
 

be80be

Joined Jul 5, 2008
2,395
So I recently making a temperature sensor with my new msp430 launchpad, dh11 temperature sensor and display it on a lcd0802(something like lcd1602),
The op said he was using a LCD to display his math output I just done this a week ago the math was right the registers showed the right figures But the code changed when displayed on the LCD

Now maybe the op will have a look at how His LCD is handling strings
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hey bug. Did the thank you in post #5 help you to figure out the problem? Or is it just a general thank you.

You are welcome either way.
Following WBahns suggestion, I use fix point calculation instead of floating point, and I stepped it through as spinnaker suggested, it works, and I am happy with the result.

and to be80be,

because there is no itoa() in code composer(at least not I can find), I have to write my own my_itoa(), if you don't mind, can you share your code on your_itoa()? I am keen to learn from your code :)

edit:
forcing the compiler to use floating point works too.
 

be80be

Joined Jul 5, 2008
2,395
I worked mine out over here http://www.jb.man.ac.uk/~slowe/cpp/itoa.html

Some really good reading on writing your own
Rich (BB code):
/**
	 * C++ version 0.4 char* style "itoa":
	 * Written by Lukás Chmela
	 * Released under GPLv3.
	 */
	char* itoa(int value, char* result, int base) {
		// check that the base if valid
		if (base < 2 || base > 36) { *result = '\0'; return result; }
	
		char* ptr = result, *ptr1 = result, tmp_char;
		int tmp_value;
	
		do {
			tmp_value = value;
			value /= base;
			*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
		} while ( value );
	
		// Apply negative sign
		if (tmp_value < 0) *ptr++ = '-';
		*ptr-- = '\0';
		while(ptr1 < ptr) {
			tmp_char = *ptr;
			*ptr--= *ptr1;
			*ptr1++ = tmp_char;
		}
		return result;
	}
 

vortmax

Joined Oct 10, 2012
102
Hunt around on the 430h.com forums. There are a few printf ports that work well on the msp430's that can handle this translation problem.

I have attempted to implement floating point on a MSP430 and even doing simple math is horribly slow. Just multiply by 10 or 100, calculate in fixed, then shift in a decimal to display the number.
 
Top