This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Trouble with ints, floats and double

I'm using the MCBSTM32C evaluation board. I would like to calculate the angle of the board in relation to the gravity vector.
I have no prolems reading the accelerometer. I basically have the gravity vector components as int values. To do what I want to do I have to calculate the length of the vector using the sqrt function in math.h. The sqrt function expects double numbers as input and produces a double result as far as I can tell. I than would like to print the result onto the screen. As soon as I print to result onto the screen the program crashes. I'm not sure if it is the sqrt function which causes the trouble or the print function. Below is some code that leads to a crash. I can print any other value to screen just not double.

double acc_length;

acc_length = sqrt(45.98745);

sprintf(print_LCD[0], " %lf ", acc_length);
GLCD_DisplayString (1, 0, 1, print_LCD[0]);

Thanks,
Karsten

Parents
  • "I have to calculate ... using the sqrt function in math.h"

    No, you don't have to!

    You can - but you don't have to.

    "The sqrt function expects double numbers as input and produces a double result as far as I can tell"

    What do you mean, "as far as I can tell" - doesn't the manual specify this precisely?

    "I'm not sure if it is the sqrt function which causes the trouble or the print function"

    So what Debugging have you done to find out?

    sprintf( print_LCD[0], " %lf ", acc_length );
    GLCD_DisplayString( 1, 0, 1, print_LCD[0] );
    

    You haven't showed your definitions of either print_LCD or GLCD_DisplayString - so nobody can debug this for you!

    It should be easy to single-step, check values, and see where it goes wrong - surely...?

    But, before you do that, have you carefully checked those function calls?
    Are you sure that all the parameters are of the correct types...?

Reply
  • "I have to calculate ... using the sqrt function in math.h"

    No, you don't have to!

    You can - but you don't have to.

    "The sqrt function expects double numbers as input and produces a double result as far as I can tell"

    What do you mean, "as far as I can tell" - doesn't the manual specify this precisely?

    "I'm not sure if it is the sqrt function which causes the trouble or the print function"

    So what Debugging have you done to find out?

    sprintf( print_LCD[0], " %lf ", acc_length );
    GLCD_DisplayString( 1, 0, 1, print_LCD[0] );
    

    You haven't showed your definitions of either print_LCD or GLCD_DisplayString - so nobody can debug this for you!

    It should be easy to single-step, check values, and see where it goes wrong - surely...?

    But, before you do that, have you carefully checked those function calls?
    Are you sure that all the parameters are of the correct types...?

Children
No data