This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.
Released under the GNU General Public License.
sin
Prototype
#include <math.h>
double sin(double x);
Description
Returns the sine of x
, where x
is given in radians.
The return value is in the range
[-1,+1].
Example
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159265358979323846
#define G 9.80665
/* Calculate initial speed. */
double initial_speed(double distance, double angle)
{
return sqrt((.5 * G * distance * distance) /
(tan(angle) * distance * pow(cos(angle), 2)));
}
/* Calculate fly time. */
double fly_time(double speed, double angle)
{
return 2 * speed * sin(angle) / G;
}
/* Calculate maximum height. */
double maximum_height(doble speed, double angle)
{
return pow(speed * sin(angle), 2) / (2 * G);
}
/*
* cannon.c: calculate the initial speed, fly time and maximum
* height of a cannon ball that must hit a target at the
* specified distance.
* usage: cannon <distance> [angle]
*/
int main(int argc, char **argv)
{
double distance, angle, speed, time, height;
if (argc < 2 || argc > 3) {
fprintf(stderr, "usage: cannon <distance> [angle]\n"
"\tdistance is expressed in meters\n"
"\tangle is expressed in degrees "
"(optional, default is 45)\n");
return EXIT_FAILURE;
}
distance = atof(argv[1]);
if (argc == 2) /* No angle specified. */
angle = 45;
else
angle = atof(argv[2]);
/* Convert angle into radians. */
angle = PI/180 * angle;
/* Calculate values. */
speed = initial_speed(distance, angle);
time = fly_time(speed, angle);
height = maximum_height(speed, angle);
printf("distance: %.2f m, angle: %.2f rad\n"
"initial speed: %.2f m/s, "
"fly time: %.2f s, maximum height: %.2f m\n",
distance, angle, speed, time, height);
return EXIT_SUCCESS;
}
References
ISO C 9899:1990 XXX