Guest User

Untitled

a guest
Jun 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include "roots.h"
  2. #include <iostream>
  3.  
  4. float Roots::Pow(float x, int n) {
  5.   float result = 1;
  6.   for (int i = 0; i < n; i++)
  7.     result = result * x;
  8.   return result;
  9. }
  10.  
  11. float Roots::Abs(float x) {
  12.   if (x < 0) return -x;
  13.   return x;
  14. }
  15.  
  16. float Roots::GuessRoot(int n, float x) {
  17.   float finding = 1;
  18.   do { } while (Pow(finding++, n) < x);
  19.   return finding;
  20. }
  21.  
  22. float Roots::Root(float x, int n) {
  23.   float result = GuessRoot(n, x);
  24.   while(Abs(x - Pow(result, n)) > error_margin)
  25.     result = result - ((Pow(result, n)-x)/(n*Pow(result, n-1)));
  26.   return result;
  27. }
  28.  
  29. float Roots::SquareRoot(float x) {
  30.   return Root(x,2);
  31. }
Add Comment
Please, Sign In to add comment