0

I am making a calculator using the formula on this link: http://cereference.com/book/surveying-and-transportation-engineering/simple-curves-or-circular-curves#sthash.qrD1VOm6.08csgYq9.dpbs

and

https://www.easycalculation.com/engineering/civil/highways-horizontal-curve.php

EDITED QUESTION!

So I used the math.h library in order to use the sin, tan, cos, and sec function but the answers are not right based on my formula... So to test, lets say I have an angle of 36 and a radius of 286... so the answer for the tangent (utangent) must be 92.927. and my next question is that how to use the sec function? I commented it because it wont compile... Also with tan,sin and cos.

#include<stdio.h>
#include<conio.h>
#include<math.h>


int main(){

double length, angle, radius, tangent, chord, midordinate, external, degree;
double pcurve, pintersection, ptangent;
double ulength, uangle, uradius, utangent, uchord, umidordinate, uexternal;
double pi;
double choice, choice2, given;

pi = 3.14159;

printf("Enter radius: ");
scanf("%lf",&radius);

printf("Enter angle: ");
scanf("%lf",&angle);

utangent = radius * (tan(angle/2)); 
uchord = 2*radius*(sin(angle/2));
umidordinate = radius - (radius*(cos(angle/2)));
//uexternal = radius * (sec(angle/2)) - radius;

printf("tangent = %lf\n",utangent);
printf("chord = %lf\n",uchord);
printf("ordinate = %lf\n",umidordinate);
//printf("%lf\n",uexternal);

getch();
return 0;
}
user3767918
  • 63
  • 1
  • 2
  • 7
  • 1
    possible duplicate of [What does floating point error -1.#J mean?](http://stackoverflow.com/questions/840081/what-does-floating-point-error-1-j-mean) – melpomene Jul 26 '15 at 14:19
  • 4
    You are using the wrong format specifier. `%lf` must be used to scanf a `double`. (`%f` should be used to printf, however). Also, check the return value of scanf to detect errors. – M.M Jul 26 '15 at 14:20
  • 3.14159 is far from double precision. use `M_PI` or `boost::math::constants::pi` – phuclv Jul 26 '15 at 15:54

1 Answers1

5

If you compile your code with warnings, which you absolutely should, you may see something like:

sintancos.c:15:13: warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat]
scanf("%f", &angle);
       ~~   ^~~~~~
       %lf

This is fixed by rewriting it to scanf("%lf", &angle); as suggested by the warning message.

I assume you need to recalculate the input from degrees to radians, since you're asking for degrees. And, of course, before outputting it again you need to change it back to degrees.

It's often done in C with macros, but I prefer functions.

double to_degrees(double rad)
{
  return rad * 180.0 / M_PI;
}
double to_radians(double deg)
{
  return deg * M_PI / 180.0;
}

M_PI is almost always defined in math.h, with higher precision than your pi. You should also move your input and calculation to its own functions, so it is easier to read and test.

sec is not a standard C function, so you have to define it yourself. It'll be something like this:

#include<stdio.h>
#include<math.h>

double to_degrees(double rad)
{
  return rad * 180.0 / M_PI;
}

double to_radians(double deg)
{
  return deg * M_PI / 180.0;
}

double sec(double z_r)
{
  return 1 / cos(z_r);
}

int main(){

  double angle, radius, angle_r;
  double utangent, uchord, umidordinate, uexternal;


  //printf("Enter radius: ");
  //scanf("%lf",&radius);
  radius = 286;

  //printf("Enter angle: ");
  //scanf("%lf",&angle);
  angle = 36;

  angle_r = to_radians(angle);

  utangent = radius * (tan(angle_r/2)); 
  uchord = 2*radius*(sin(angle_r/2));
  umidordinate = radius - (radius*(cos(angle_r/2)));
  uexternal = radius * (sec(angle_r/2)) - radius;

  printf("\nResults:\n");
  printf("tangent = %lf\n",utangent);
  printf("chord = %lf\n",uchord);
  printf("ordinate = %lf\n",umidordinate);
  printf("external %lf\n",uexternal);

  return 0;
}
audun
  • 134
  • 1
  • 7