COSINE not working properly

Good morning everyone,
I'm writing my first program using windows form. i have included math.h.
when evaluation a cosinus of a number. the value i get is wrong, i have searched all over the internet for a solution for that. i need help, here is a portion of my code:

y=ib*cos(phi);

if my phi is 1.57 and ib is 2 i should get 1.59e-03 for y. i get something like 3.58e-009.

Please i need help debuging this, i'am a mechanical engineer. this is my first program. i have basics in c++ but it's limited to function and loops. this first program i first coded it in c++ in a console windows. everything workd fine. as soon as i wanted to give it a graphical interface, it's not working as it should. i'm using visual c++ by the way.
Last edited on
Are you mixing randians and degrees?
Thanks for replying,
the phi is in degrees. i converted it into radian using phi*pi/180.
either ways, when i put that on my calculator, it gives me a different value, than the 3.58e-009. (radians or degrees)
Can you show us the whole code?

Ps. It is not mixing radians and degrees, I calculated it :)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <math.h>
#include <stdio.h>

int main()
{
        double phi = 1.57;
        double ib = 2.0;

        printf("using degrees: %f\n", ib * cos( phi * M_PI / 180.0));
        printf("using radians: %f\n", ib * cos( phi ));

        return 0;
}
Last edited on
the code is big... here is a portion of interest when i debug the program. i put a break point in the line below u=cos(phi).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#pragma once
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <fstream>
#define PI 3.14159265
#include <Eigen/Dense>

using namespace Eigen;

using namespace std;
using Eigen::MatrixXd;
double iNcoef;
  double phi;
  double alpha;
  double Alpha0L;
    //construction de la matrice d'equation algebrique
iNcoef=4;
    for(int i=1; i<iNcoef+1;i++)
    //point de collocation de 0 a Pi/2
    {
         phi=i*((PI/2/iNcoef));
        		double u=cos(phi);


again, the problem lies in u having a wrong value.
nevermind, problem, thanks for your support
If you guys come to do cfd sometimes i'll be glad to help:

http://www.cfd-online.com/Forums/members/diamondx.html
I make 2.0*cos(1.57) approx 1.59e-03

But 2.0*cos(3.14159265/2.0) approx 3.58e-09

So the error comes from your (innaccurate) #define for PI

Use M_PI to get a full double precision representation of PI

Then the answer will be zero

General debugging tip is to assume that functions like cos are never wrong - just keep at it
Topic archived. No new replies allowed.