-1

When I write on my calculator Cos45 I get a decimal number = 0.707

How do I produce such a number in C.

I tested this:

printf ("type a degree between 0 - 360:\n");
scanf ("%f",&float1);
printf ("cosphi = %f",cosf(float1));

but it gave an off number. It produced cosphi = 0.52

3 Answers3

4

Your calculator is configured to compute trigonometric functions in degrees.

C's trig functions work in radians. (A full circle is 360 degrees, 2*pi radians.)

If you want to treat the input as degrees, you need to convert the value to radians before passing it to cosf(), by multiplying it by 180/pi.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Thank you . something like this. find cos45. pi radians / 4 = 180 / 4. when I put : printf ("cosphi = %f",cosf(3.14/4)); the print is 0.707 that is correct . – Radar Blue Jul 31 '16 at 20:23
0

I got it to work. Thanks a million :)


#include <stdio.h>
int main ()
//Convert Trigonometric Angles into Decimals and Radians.
//Radians are number of Radiuses that are wrapped around the circumference.
//Pi for half the circle, Radius is wrapped 3.14 times on 180 degrees. r2=d1 .
//Circumference = 2radius * Pi = Diameter * Pi = 2Pi * radius .
//KHO2016.no2. mingw (TDM-GCC-32) . c-ansi .
{
//Declare
float flp1, flp2, flp3, flp4, pi;
int int1;

//valuate
pi = 3.141592654;
int1 = 180;

//calculate
printf ("type a degree between 0 - 360 : ");
scanf ("%f",&flp1);
flp2=int1/flp1;  // 180 is divided by desired angle
flp3=pi/flp2;    // Pi is divided by the result of 180 / desired angle = Radians
flp4=cosf(flp3); // Result of Pi divided by Radians and fed into the Cosf Radian modulus
printf ("The Decimal value of Cosinus %.1f degrees = %.3f\n",flp1,flp4);
printf ("Angle typed in Radians = %f",flp3);

//Terminate
return 0;

}

  • 1
    Consider using variable names that are more descriptive than "flp123". Also, you can just use literal values like 3.141592 and 180 without assigning them to variables. – nibot Jul 31 '16 at 22:59
  • 1
    `double angle_deg; scanf("%f", &angle_deg); printf("cos %.1f deg = %f\n", angle_deg, cos(angle_deg * 180.0 / M_PI);` – nibot Jul 31 '16 at 22:59
  • 2
    If you `#include ` then you'll get M_PI. – nibot Jul 31 '16 at 23:02
  • @nibot `M_PI` is often included in ``, but that is not specified to be there. Still, `M_PI`, is a good idea when available. See http://stackoverflow.com/a/5008376/2410359 for details. – chux - Reinstate Monica Oct 06 '16 at 14:32
0

As answer well by @Keith Thompson, the C function works in radians and so a degrees to radian conversion is needed.

#ifndef M_PI
  #define M_PI 3.1415926535897932384626433832795
#endif
float angle_radians = angle_degrees * (float) (M_PI/180.0);

Yet rather that directly scale by pi/180.0, code will get more precise answers, for angles outside a primary range, if code first does range reduction and then scales by pi/180.0. This is because the scaling of pi/180.0 is inexact as machine pi float pi = 3.14159265; is not exactly mathematical π. The radian range reduction, performed by cos(), is in error as the computed radian value given to it is inexact to begin with. With degrees, range reduction can be done exactly.

// Benefit with angles outside -360° to +360°
float angle_degrees_reduce = fmodf(angle_degrees, 360.0f);
float angle_radians = angle_degrees_reduce * M_PI/180.0;

sind() is a sine example that performs even better by reducing to a narrower interval. With angles outside the -45° to +45° range, there is benefit.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256