From 78506e3112204dd26fb2f68fd7ed867cd01c20ab Mon Sep 17 00:00:00 2001 From: bacbahieu Date: Fri, 15 Mar 2024 01:30:15 +0700 Subject: [PATCH] Bai_3_calculcus --- AdvProg_L2-Calculus/calculus.cpp | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/AdvProg_L2-Calculus/calculus.cpp b/AdvProg_L2-Calculus/calculus.cpp index 37d8639ea..7c8c06cda 100644 --- a/AdvProg_L2-Calculus/calculus.cpp +++ b/AdvProg_L2-Calculus/calculus.cpp @@ -2,11 +2,10 @@ #include #include -using std::string; using std::cout; using std::endl; using std::stod; - +using std::string; double mySin(double x); double myCos(double x); @@ -18,9 +17,10 @@ double mySqrt(double x); Returns: double: cosine of x ***/ -double myCos(double x) +double myCos(double x) { - return 0.0; + double ket_qua = 1 - pow(x, 2) / 2 + pow(x, 4) / 24 - pow(x, 6) / 720; + return ket_qua; } /*** @@ -31,22 +31,30 @@ double myCos(double x) ***/ double mySin(double x) { - return 0.0; + double ket_qua = x - pow(x, 3) / 6 + pow(x, 5) / 120 - pow(x, 7) / 5040; + return ket_qua; } - /*** Args: x (double): a number Returns: double: square root of x ***/ -double mySqrt(double x) { - if (x < 0) { +double mySqrt(double x) +{ + if (x < 0) + { cout << "Invalid argument" << endl; exit(1); } - - - return 0; + else + { + double ket_qua = x; + for (int i = 0; i < 5; i++) + { + ket_qua = (ket_qua + x / ket_qua) / 2; + } + return ket_qua; + } } \ No newline at end of file