0 Daumen
1,7k Aufrufe

Task:
The number \(π\) can be defined through various infinite sums. The accuracy increases with the number of terms. Considering the follwowing sum, that we call sum 1:$$\frac{\pi}{4}=\sum_{0 \leq n} \frac{(-1)^{n}}{2 n+1}=1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\ldots$$

Write a program that computes and outputs an approximation of Pi, based on sum 1.
The input for your program is the number of terms \(m\) to be included in the calculation to be done.
The output is the approximation of \(π.\)

Input:

A number m ≥ 0.

Output: 
The approximation of \(π\) given by \(4 \sum_{0 \leq n<m} \frac{(-1)^{n}}{2 n+1}\) , rounded to 6 significant digits.
Note that 6 significant digits is the default precision of C++ for printing floating-point values.



Was habe ich bisher gemacht ?

Code:

#include <iostream>

float pi;
int m_terms;
float denominator;
int i = 0;
float sum1 = 0;

int main (){
  //Type in a number
  std::cout << "Type in a number for how many terms you want to compute pi : " << std::endl;
  std::cin >> m_terms;
 
 
  // Approximate pi based on sum1 with help of a loop
      while(i<=m_terms){
        //set denominator variable
        denominator = i;
        //consider the negative terms
        if(i%2==1){
          pi += (pi-1)/(2*denominator+1);}
        //consider the positive terms 
        else{
          pi += (pi+1)/(2*denominator+1);}
          ++i;
        }
        sum1=pi*4;
 
  std::cout << "pi equals: " << sum1 << std::endl;

  return 0;
}


Rep.it Code-Link: 

Avatar von

Gelöst! (Siehe Code unten): 
Ich habe es gelöst und das automatische Kontrollsystem sagt 1000% richtig. 

Frage:
Trotzdem frage ich mich bezüglich des 6-Digit-Roundings

(1)ob ich die Variabeln so als float lassen kann oder: 

float pi oder double pi ? 
float denominator oder double ? 
float sum oder double ? 

(2) Was ist der unterschied bezüglich dieser Aufgabe ? 

Code:

#include <iostream>

float pi = 0;
int m_terms;
float denominator;
float sum1 = 0;

int main (){
  //Type in a number
  std::cout << "Type in a number for how many terms m you want to compute pi : " << std::endl;
  std::cin >> m_terms;
 
 
  // Approximate pi/4 based on sum1 with help of a loop
      for(int i = 0 ; i<=(m_terms-1);++i){
        //set denominator variable
        denominator = i;
        //consider the negative terms
        if(i%2==1){
          pi += (-1)/(2*denominator+1);}
        //consider the positive terms 
        else{
          pi += (+1)/(2*denominator+1);}
        }
       
  //We now have the sum for pi/4, multiplying it by 4 on both sides gets us pi
  sum1=pi*4;
 
  //Output the result of pi-Approximation
  std::cout << "pi equals: " << sum1 << std::endl;

  return 0;
}

@limnade

Ich habe eine Frage bezüglich dem scope der Variabeln bei deiner gelösten Aufgabe. Warum kannst du die Variabel "pi" benutzen, sollte diese nicht nur im "if" bzw. im "else" Block gültig sein?

2 Antworten

+1 Daumen

Die Variable pi sollte vom Typ double sein, weil die Rechengenauigkeit steigt. Zwar kann man mit float ca. 8 gültige Ziffern darstellen, aber aufgrund der vielen Divisionen und Addition entstehen sehr schnell Rundungsfehler, die sich dann immer weiter fortplanzen.

Die Variable sum1 sollte ebenfalls vom Typ double sein, denn ansonsten macht man durch sum1=pi*4 die Rechengenauigkeit wieder zunichte.

Die Variable demoninator ist unnötig und kann durch i ersetzt werden (würde ich dann mit "n" bezeichnen). Um sicher zu gehen, dass der Compiler die Division in double ausführt, dann z.B. pi += -1.0 / (double)(2*n+1) schreiben.

Avatar von
+1 Daumen

Aloha :)

//Der Wert m vom Typ unsigned int für die Anzahl der Summanden sei bereits eingelsen.
float sum= 0.0f;
for (unsigned int n=0; n<m; ++n) sum+= (n&1?-1.0f:1.0f)/(2*n+1.0f);
std::cout << "pi = " << 4*sum << std::endl;
Da in modernen CPUs die Berechnung von float genauso lange dauert wie die Berechnung von double, kannst du als Datentyp auch double verwenden, um 12-stellige Genauigkeit zu erhalten.
Avatar von

Ein anderes Problem?

Stell deine Frage

Willkommen bei der Stacklounge! Stell deine Frage einfach und kostenlos

x
Made by a lovely community