fork download
  1. /*Napisac program liczacy kolejne wyrazy ciagu:
  2. x{n+1}= x{n} + 3.0 * x{n}* (1 - x{n})
  3. startujac z punktu x{0} = 0.01. Wykonac to zadanie dla roznych reprezentacji liczb (float, double). Dlaczego wyniki sie rozbiegaja?
  4. */
  5.  
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. void double_sequence(double x0,int q){
  10. double tmp,tmp2;
  11. int i;
  12. cout<<"Double"<<endl;
  13. for(i=0; i<q; ++i){
  14. tmp = x0 + 3.0*x0*(1 - x0);
  15. printf("%e\n",tmp);
  16. x0 = tmp;
  17. }
  18. }
  19.  
  20. void float_sequence(float x0,int q){
  21. float tmp,tmp2;
  22. int i;
  23. cout<<"Float"<<endl;
  24. for(i=0; i<q; ++i){
  25. tmp = x0 + 3.0*x0*(1 - x0);
  26. printf("%d: f=%f, d=%f\n", tmp);
  27.  
  28. x0 = tmp;
  29. }
  30. }
  31.  
  32. int main(void){
  33. float_sequence(0.01,20);
  34. double_sequence(0.01,20);
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5520KB
stdin
Standard input is empty
stdout
Float
0: f=0.039700, d=0.039700
0: f=0.154072, d=0.154072
0: f=0.545073, d=0.545073
0: f=1.288978, d=1.288978
0: f=0.171519, d=0.171519
0: f=0.597820, d=0.597820
0: f=1.319114, d=1.319114
0: f=0.056271, d=0.056271
0: f=0.215586, d=0.215586
0: f=0.722912, d=0.722912
0: f=1.323843, d=1.323843
0: f=0.037692, d=0.037692
0: f=0.146506, d=0.146506
0: f=0.521633, d=0.521633
0: f=1.270229, d=1.270229
0: f=0.240472, d=0.240472
0: f=0.788408, d=0.788408
0: f=1.288870, d=1.288870
0: f=0.171922, d=0.171922
0: f=0.599015, d=0.599015
Double
3.970000e-02
1.540717e-01
5.450726e-01
1.288978e+00
1.715191e-01
5.978201e-01
1.319114e+00
5.627158e-02
2.155868e-01
7.229143e-01
1.323842e+00
3.769530e-02
1.465184e-01
5.216706e-01
1.270262e+00
2.403522e-01
7.881012e-01
1.289094e+00
1.710848e-01
5.965293e-01