C++ Mathematic Functions Inverse Trigonometric Functions Purpose
The inverse trigonometric functions calculate the arcsin, arccos, or arctan of a value, returning the angle in radians. Header File math.h (asin, acos, atan, atan2) Prototypes double double double double
asin (double Angle); acos (double Angle); atan (double Angle); atan2 (double Y, double X);
Arguments Value The value of the function (sin, cos, or tan) X The abcissa of the angle Y The ordinate of the angle Return Values asin The principal arc sine of the argument (-π/2 .. π/2), in radians acos The principal arc cosine of the argument (0 .. π), in radians atan The principal arc tangent of the argument (-π/2 .. π/2), in radians atan2
The arc tangent of Y / X, in the proper quadrant (-π .. π), in radians Example #include <stdio.h> #include <math.h> #include <stdlib.h> void Display (double Value); void Display2 (double Y, double X); double Rad2Deg (double Angle); int main () { Display (.02 * (double)(rand() % 100) - 1.0); Display (.02 * (double)(rand() % 100) - 1.0); Display (.02 * (double)(rand() % 100) - 1.0); Display2 (1.0, 2.0); Display2 (1.0, -2.0); Display2 (-1.0, 2.0); Display2 (-1.0, -2.0); return 0; } double Rad2Deg (double Angle) { static double ratio = 180.0 / 3.141592653589793238; return Angle * ratio; } void Display (double Value) { double Radians; double Degrees; Radians = asin (Value); Degrees = Rad2Deg (Radians); printf ("The arc sine of %f is %f radians = %f degrees\n", Value, Radians, Degrees); Radians = acos (Value); Degrees = Rad2Deg (Radians); printf ("The arc cosine of %f is %f radians = %f degrees\n", Value, Radians, Degrees); Radians = atan (Value); Degrees = Rad2Deg (Radians); printf ("The arc tangent of %f is %f radians = %f degrees\n\n", Value, Radians, Degrees); } void Display2 (double Y, double X) { double Radians; double Degrees; Radians = atan2 (Y, X); Degrees = Rad2Deg (Radians); printf ("The angle having an absissa of %f\n", X); printf ("and an ordinate of %f\n", Y); printf ("is %f radians = %f degrees\n\n", Radians, Degrees); }
Output The arc sine of -0.400000 is -0.411517 radians = -23.578178 degrees The arc cosine of -0.400000 is 1.982313 radians = 113.578178 degrees The arc tangent of -0.400000 is -0.380506 radians = -21.801409 degrees The arc sine of 0.640000 is 0.694498 radians = 39.791819 degrees The arc cosine of 0.640000 is 0.876298 radians = 50.208181 degrees The arc tangent of 0.640000 is 0.569313 radians = 32.619243 degrees The arc sine of 0.800000 is 0.927295 radians = 53.130102 degrees The arc cosine of 0.800000 is 0.643501 radians = 36.869898 degrees The arc tangent of 0.800000 is 0.674741 radians = 38.659808 degrees The angle having an absissa of 2.000000 and an ordinate of 1.000000 is 0.463648 radians = 26.565051 degrees The angle having an absissa of -2.000000 and an ordinate of 1.000000 is 2.677945 radians = 153.434949 degrees The angle having an absissa of 2.000000 and an ordinate of -1.000000 is -0.463648 radians = -26.565051 degrees The angle having an absissa of -2.000000 and an ordinate of -1.000000 is -2.677945 radians = -153.434949 degrees
Latihan 1 : #include
#include #include <math.h>
void hitung_sinus (float a) { float sinus; sinus=sin(a); cout<<"\nSinus dari angka" << ' '<< a; cout <<' '<<"adalah :"<<sinus<<"\n"; cout <<"Tekan Esc untuk keluar program"<<endl; return ;} void hitung_cosinus(float a){
float cosinus; cosinus=cos(a); cout<<"\nCosinus dari angka" << ' '<< a; cout <<' '<<"adalah :"<
cout<<"\nTangen dari angka" << ' '<< a; cout <<' '<<"adalah :"<>a; cout<<"\nApa yang ingin anda cari? (1.Sinus 2.Cosinus 3.Tangen): "; cin>>x;endl; if(x==1) hitung_sinus(a); else if (x==2) hitung_cosinus(a); else if (x==3) hitung_tangen(a); else cout <<"\nTak ada dalam daftar, tekan Esc untuk keluar program"<<endl; for ( ; ;) { kar = getche();
if (kar == 27) break;} }
Latihan 2 :; #include #include <math.h> #include
class trigonometri{ friend ostream& operator<<(ostream&, const trigonometri&); friend istream& operator>>(istream&, trigonometri&); public : trigonometri(); void hitung_cosinus(){hasil1=cos((angka*3,14)/180);} void hitung_sinus(){hasil2=sin((angka*3,14)/180);} void hitung_tangen(){hasil3=tan((angka*3,14)/180);}
private : int angka; float hasil1; float hasil2; float hasil3;
};
trigonometri::trigonometri(){ cout<<"Program mencari trigonometri dari sebuah angka"<<endl; }
istream& operator>>(istream& in, trigonometri& masukan){ cout<<"masukkan angka : ";in >>masukan.angka; return in; } ostream& operator<<(ostream& out, const trigonometri& keluaran){ out<<"Sudut yang akan dikonversi : "<