C/acos
DeklaracjaEdytuj
Deklaracje znajdują się w pliku math.h
Arcusy funkcji trygonometrycznychEdytuj
double acos (double x); float acosf (float x); long double acosl (long double x); double asin (double x); float asinf (float x); long double asinl (long double x); double atan (double x); float atanf (float x); long double atanl (long double x); double atan2 (double a, double b); float atan2f(float a, double b); long double atan2l(long double a, double b);
Funkcje trygonometryczneEdytuj
double cos (double x); float cosf (float x); long double cosl (long double x); double sin (double x); float sinf (float x); long double sinl (long double x); double tan (double x); float tanf (float x); long double tanl (long double x);
Area funkcji hiperbolicznychEdytuj
double acosh (double x); float acoshf(float x); long double acoshl(long double x); double asinh (double x); float asinhf(float x); long double asinhl(long double x); double atanh (double x); float atanhf(float x); long double atanhl(long double x);
Funkcje hiperboliczneEdytuj
double cosh (double x); float coshf(float x); long double coshl(long double x); double sinh (double x); float sinhf(float x); long double sinhl(long double x); double tanh (double x); float tanhf(float x); long double tanhl(long double x);
ArgumentyEdytuj
- x
- argument funkcji matematycznej. Miary kątów podawane są w radianach[1]
- a, b
- współrzędne punktu na okręgu
OpisEdytuj
Funkcje liczą wartości odpowiednich funkcji matematycznych zgodnie z ich nazwą:
- acos - arcus cosinus argumentu,
- asin - arcus sinus argumentu,
- atan - arcus tangens argumentu,
- atan2 - arcus tangens ilorazu argumentu,[2]
- cos - cosinus argumentu,
- sin - sinus argumentu,
- tan - tangens argumentu.
Przyrostki:
- Funkcje z przyrostkami "f" i "l" to inne wersje odpowiedniej funkcji bez przyrostka operujące na innych typach danych. Z tego powodu w poniższym opisie odwołanie do konkretnej funkcji będzie oznaczać odwołanie się do wszystkich 3 werji. Wersje funkcji z przyrostkiem "f" i "l" (tzn. wersje operujące na zmiennych typu float i long double) zostały wprowadzone dopiero w standardzie C99.
- Funkcje z przyrostkiem "h" obliczają wartości analogicznych funkcji hiperbolicznych i funkcji area.
Wartość pobierana (ang. input)Edytuj
Funkcja atan2 bierze pod uwagę znaki obu argumentów dzięki czemu może dokładnie obliczyć kąt. Funkcja atan nie posiada takich informacji przez co przedział zwracanych przez nią wartości jest dwa razy mniejszy.
Jeżeli wartości argumentu x jest spoza przedziału [-1; 1] funkcje acos i asin zwracają nieokreśloną wartość na zmienna errno ustawiona jest na EDOM. Podobnie, jeżeli argumenty a i b są jednocześnie równe 0 funkcja atan2 ustawia zmienną errno na EDOM.
Wartość zwracana (ang. output)Edytuj
Wartości odpowiednich funkcji matematycznych. Dodatkowo, dla arcusów funkcji trygonometrycznych zakres zwracanych wartości jest ograniczony. Dla funkcji:
- acos jest to przedział [0; Π],
- asin jest to przedział [-Π/2; Π/2],
- atan jest to przedział [-Π/2; Π/2], natomiast
- atan2 jest to przedział [-Π; Π].
Ponadto, jeżeli wystąpi nadmiar, funkcja zwraca w wyniku HUGE_VAL z odpowiednim znakiem i ustawia wartość zmiennej errno na ERANGE. Jeśli wystąpi niedomiar funkcja w wyniku zwraca zero, a to czy do zmiennej errno zostanie zapisana wartość ERANGE zależy od implementacji.
Jak używaćEdytuj
- dyrektywa
#include <math.h>
- opcja kompilacji
-lm
W przypadku użycia funkcji matematycznych może zaistnieć konieczność podania odpowiedniego argumentu linkerowi, aby ten połączył program z biblioteką matematyczną. Np. na systemach GNU/Linux jest to -lm
.
PrzykładyEdytuj
#include <math.h>
#include <stdio.h>
/* Program oblicza wychylenie, prędkość i przyśpieszenie
ciała, gdy drga ono harmonicznie */
int main()
{
double A, omega, t, x, V, a;
printf("A = ");
if (scanf("%lf", &A) != 1) return 1;
printf("omega = ");
if (scanf("%lf", &omega) != 1) return 1;
printf("t = ");
if (scanf("%lf", &t) != 1) return 1;
x = A * sin(omega * t);
V = A * omega * cos(omega * t);
a = - omega * omega * x;
printf("x = %f\nV = %f\na = %f\n", x, V, a);
return 0;
}
Funkcja atan2:
#include <stdio.h>
#include <math.h>
// http://en.cppreference.com/w/c/numeric/math/atan2
// gcc a.c -lm -Wall
int main()
{
// list of points (x,y) around origin in counterclockwise direction
double p[][2]={{ 1.0, 0.0},
{ 1.0, 0.1},
{ 1.0, 1.0},
{ 0.0, 1.0},
{-1.0, 1.0},
{-1.0, 0.0},
{-1.0,-0.1},
{-1.0,-1.0},
{ 0.0,-1.0},
{ 1.0,-1.0},
{ 1.0,-0.1}};
int length = sizeof(p)/sizeof(p[0]);
int i;
for ( i=0; i<length; i++){
//atan2(y,x) , result = the arc tangent of y/x in the range [-π ; +π] radians
printf ("atan2 of point (x,y) = (%.1f, %.1f) is atan2(y,x) = %f\n", p[i][0], p[i][1], atan2(p[i][1], p[i][0]));
}
return 0;
}
wynik:
atan2 of point (x,y) = (1.0, 0.0) is atan2(y,x) = 0.000000 atan2 of point (x,y) = (1.0, 0.1) is atan2(y,x) = 0.099669 atan2 of point (x,y) = (1.0, 1.0) is atan2(y,x) = 0.785398 atan2 of point (x,y) = (0.0, 1.0) is atan2(y,x) = 1.570796 atan2 of point (x,y) = (-1.0, 1.0) is atan2(y,x) = 2.356194 atan2 of point (x,y) = (-1.0, 0.0) is atan2(y,x) = 3.141593 atan2 of point (x,y) = (-1.0, -0.1) is atan2(y,x) = -3.041924 atan2 of point (x,y) = (-1.0, -1.0) is atan2(y,x) = -2.356194 atan2 of point (x,y) = (0.0, -1.0) is atan2(y,x) = -1.570796 atan2 of point (x,y) = (1.0, -1.0) is atan2(y,x) = -0.785398 atan2 of point (x,y) = (1.0, -0.1) is atan2(y,x) = -0.099669