C++ Bc. 8 cpp

Z GeoWikiCZ
#include <iostream>
#include <cmath>
#include <iomanip>

double vypocet(double x)
{
  double znam = 1;
  if (x < 0)
    {
      znam = -1;
      x    = -x;
    }
  
  // alternativni vypocet pro x > 0.5
  bool alt = false;
  if (x > 0.5)
    {
      alt = true;
      x = std::sqrt((1-x)/2);
    }

  // Tayloruv rozvoj
  const double tol = 1e-16;
  double i    = 1;
  double pom1 = 1, pom2 = 6;     
  double dif  = x, soucet = x;

  while(dif> tol)
    {
      dif    *=  x*x * pom1 / pom2;
      soucet +=  dif;
      i      +=  1;
      pom1   +=  2;
      pom2    =  2*i/pom1*(pom1+2);    
    }

  // alternativni vypocet pro x > 0.5 
  if (alt)
    {
      soucet = M_PI/2 - 2*soucet;
    }

  return znam*soucet;
}


int main()
{
  using namespace std;

  cout << "+-----------------------------------------+\n";
  
  for(double n=-1; n<=1; n+=0.1)
    {
      double h1  = asin(n), h2  = vypocet(n), dif = h2-h1;
      
      cout << "|" << ' ';   
      
      cout.setf(ios_base::fixed, ios_base::floatfield);          
      cout << setw(5) << setprecision(2) << n  << ' ';    
      cout << setprecision(8) << setw(11) << h1 << ' ';
      cout << setprecision(8) << setw(11) << h2 << ' ';
      
      cout.setf(ios_base::scientific, ios_base::floatfield);
      cout << setprecision(2) << setw(9) << dif;
      
      cout << ' ' << "|" << endl;
    }
  
  cout << "+-----------------------------------------+\n";
}
+-----------------------------------------+
| -1.00 -1.57079633 -1.57079633  0.00e+00 |
| -0.90 -1.11976951 -1.11976951  0.00e+00 |
| -0.80 -0.92729522 -0.92729522  1.11e-16 |
| -0.70 -0.77539750 -0.77539750 -2.22e-16 |
| -0.60 -0.64350111 -0.64350111  0.00e+00 |
| -0.50 -0.52359878 -0.52359878  3.33e-16 |
| -0.40 -0.41151685 -0.41151685  0.00e+00 |
| -0.30 -0.30469265 -0.30469265  5.55e-17 |
| -0.20 -0.20135792 -0.20135792 -2.78e-17 |
| -0.10 -0.10016742 -0.10016742  0.00e+00 |
| -0.00 -0.00000000 -0.00000000  0.00e+00 |
|  0.10  0.10016742  0.10016742  0.00e+00 |
|  0.20  0.20135792  0.20135792  2.78e-17 |
|  0.30  0.30469265  0.30469265  0.00e+00 |
|  0.40  0.41151685  0.41151685  5.55e-17 |
|  0.50  0.52359878  0.52359878  2.22e-16 |
|  0.60  0.64350111  0.64350111 -1.11e-16 |
|  0.70  0.77539750  0.77539750  1.11e-16 |
|  0.80  0.92729522  0.92729522 -1.11e-16 |
|  0.90  1.11976951  1.11976951  0.00e+00 |
|  1.00  1.57079631  1.57079631 -2.22e-16 |
+-----------------------------------------+

[ Zpět ]