how to calculate floating point in MCU with C

Discussion in 'Programmer's Corner' started by bug13, Dec 26, 2012.

  1. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    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:
    Code ( (Unknown Language)):
    1. uint8_t calculate_dew_point(float temp, float r_humidity)
    2. {
    3.     return pow((r_humidity/100),(1/8))*(112+0.9*temp)+0.1*temp-112;
    4. }
    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! :)
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    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.
     
    bug13 likes this.
  3. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    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.
     
    bug13 likes this.
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,886
    1,011
    Well easier than figuring a way to make more memory space but not easier than using floating point if you have the memory.
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,886
    1,011
    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.
     
    bug13 likes this.
  6. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    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.
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,886
    1,011
    How is this helping the OP?
     
  8. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    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: Dec 26, 2012
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,886
    1,011
    Try reading post #5.
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,886
    1,011
    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.
     
  11. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    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
     
  12. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,886
    1,011
    That is what I should be asking you. :rolleyes:



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

    Senior Member

    Jul 5, 2008
    431
    57
    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.
     
  14. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Resorting to ad hominem attacks is just pathetic. Anyway the OP never asked about an LCD.
     
  15. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    Hey beb80be, such behavior is not tolerated on this forum.
    This is not your thread, so beat it.
     
  16. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    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
     
  17. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    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.
     
  18. be80be

    Senior Member

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

    Some really good reading on writing your own
    Code ( (Unknown Language)):
    1.  
    2. /**
    3.      * C++ version 0.4 char* style "itoa":
    4.      * Written by Lukás Chmela
    5.      * Released under GPLv3.
    6.      */
    7.     char* itoa(int value, char* result, int base) {
    8.         // check that the base if valid
    9.         if (base < 2 || base > 36) { *result = '\0'; return result; }
    10.    
    11.         char* ptr = result, *ptr1 = result, tmp_char;
    12.         int tmp_value;
    13.    
    14.         do {
    15.             tmp_value = value;
    16.             value /= base;
    17.             *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
    18.         } while ( value );
    19.    
    20.         // Apply negative sign
    21.         if (tmp_value < 0) *ptr++ = '-';
    22.         *ptr-- = '\0';
    23.         while(ptr1 < ptr) {
    24.             tmp_char = *ptr;
    25.             *ptr--= *ptr1;
    26.             *ptr1++ = tmp_char;
    27.         }
    28.         return result;
    29.     }
    30.    
    31.    
     
    bug13 likes this.
  19. vortmax

    Member

    Oct 10, 2012
    103
    18
    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.
     
    bug13 likes this.
Loading...