This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Mathematical Functions

Other Parts Discussed in Thread: MSP430F5438A

Hi, 

I am using MSP430F5438A and Code Composer Studio 6.0.1.00040. I am trying to do the following 

#include <msp430.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <msp430.h>
#include <hal_lcd.h>
#include <hal_lcd_fonts.h>
#include <hal_board.h>
#include <hal_MSP-EXP430F5438.h>
#include <hal_lcd.c>
#include <hal_lcd_fonts.c>
#include <string.h>
#define Num_of_Results 8

void LCD_DIS (void);

void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
volatile int var1 = 0;
volatile int var2;
volatile int var3 = 655;
volatile int var4;
char buf[0];
volatile i;
i= sin(90);

}

But the compiler is showing i =0 in the Expression Watch. I do not understan dit. It should have shown me "1". Please help!

Melissa

  • Anyone advise please!

    Melissa

  • I don't know if fixing these will give the desired result, but they're worth a look.

    First of all, "volatile i" doesn't specify a type, so it defaults to int. If you assign a non-integral value to "i" it will get rounded. You probably want a type that can handle fractional values if you're assigning the result of calling sin() to it. Try using "volatile float i" instead. Just be aware that MSP430 doesn't have floating point hardware, so using float will cause a significant performance hit as well as bloating your code quite a bit.

    Second, the C standard library function sin() takes its angle parameter in radians, not degrees. Try passing Pi/2 = 1.5707 (approx) instead of 90.

  • Hi, 

    I tried the following 

    double i,x;
    double c;
    c=1.570796; // PI/2; PI=3.1415
    i= sin(c);
    x = sqrt(63.9);

    Square Root Function works fine evertime. But "sin" is giving me "Watch Expression shows 0.9999999999999466 (Decimal)"

    Why square root works and sin() does not

    mel

  • melissa walraven said:

    Square Root Function works fine evertime. But "sin" is giving me "Watch Expression shows 0.9999999999999466 (Decimal)"

    Why square root works and sin() does not

    Functions like "sin" are implemented using an algorithm that approximates the result using other mathematical curves like polynoms. Unfortunately, this mean that the result could be slightly off even though there is a mathematical exact definition. (In this case, it returns 0.9999999999.... rather than 1.0.)

    The reason why the variable "i" in your original program got the value "0" was due to the rounding rules of C. Whenever you convert a floating-point number to an integer, this is done using TRUNCATION. In other words, when you convert 0.999999999999 to an integer, it's rounded to 0. (Note that "i" implicitly was an integer, defined using "volatile i".)

        -- Anders Lindgren

  • Not to mention that PI has an endless period, so you can’t exactly express PI or PI/2. And any parameter to SIN that isn’t exactly PI/2 won’t give exactly 1.
    And any float/double result is not rounded, but rather truncated when cast to an int. So the integer result of SIN() is always 0.

**Attention** This is a public forum