C/Zaawansowane operacje matematyczne: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Linia 129:
*** błędy zaokrąglania <ref>[http://www.exploringbinary.com/double-rounding-errors-in-floating-point-conversions/ Double Rounding Errors in Floating-Point Conversions By Rick Regan (Published August 11th, 2010)]</ref>
*** przekroczenie limitu ( underflow, overflow)<ref>[http://stackoverflow.com/questions/42277132/when-does-underflow-occur#comment71710792_42277132 stackoverflow question when-does-underflow-occur]</ref><ref>[http://stackoverflow.com/questions/27439503/how-to-define-underflow-for-an-implementationieee754-which-support-subnormal-n stackoverflow question: how-to-define-underflow-for-an-implementationieee754-which-support-subnormal-n]</ref>
*** mnożenie
 
 
 
Efekt:
* zachowanie niezdefiniowane : ( ang. undefined behavior = UB)<ref>[[:w:en:Undefined_behavior|Undefined_behavior w ang. wikipedii]]</ref> <ref>[https://www.nayuki.io/page/undefined-behavior-in-c-and-cplusplus-programs undefined-behavior-in-c-and-cplusplus-programs by Nayuki]</ref>
===Mnożenie ===
 
<source lang=c>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
 
https://math.stackexchange.com/questions/2453939/is-this-characteristic-of-tent-map-usually-observed
 
 
 
 
 
*/
 
 
 
/* ------------ constans ---------------------------- */
double m = 2.0; /* parameter of tent map */
double a = 1.0; /* upper bound for randum number generator */
int iMax = 100;
/* ------------------- functions --------------------------- */
 
 
/*
 
tent map
https://en.wikipedia.org/wiki/Tent_map
 
*/
double f(double x0, double m){
 
double x1;
if (x0 < 0.5)
x1 = m*x0;
else x1 = m*(1.0 - x0);
return x1;
 
}
 
 
 
/* random double from 0.0 to a
https://stackoverflow.com/questions/13408990/how-to-generate-random-float-number-in-c
 
 
*/
double GiveRandom(double a){
srand((unsigned int)time(NULL));
return (((double)rand()/(double)(RAND_MAX)) * a);
 
}
 
int main(void){
 
int i = 0;
double x = GiveRandom(a); /* x0 = random */
for (i = 0; i<iMax; i++){
printf("i = %3d \t x = %.16f\n",i, x);
x = f(x,m); /* iteration of the tent map */
}
 
return 0;
}
</source>
 
Kompilacja i uruchomienie:
 
gcc t.c -Wall
./a.out
 
Wynik:
 
 
 
 
 
<pre>
i = 0 x = 0.1720333817284710
i = 1 x = 0.3440667634569419
i = 2 x = 0.6881335269138839
i = 3 x = 0.6237329461722323
i = 4 x = 0.7525341076555354
i = 5 x = 0.4949317846889292
i = 6 x = 0.9898635693778584
i = 7 x = 0.0202728612442833
i = 8 x = 0.0405457224885666
i = 9 x = 0.0810914449771332
i = 10 x = 0.1621828899542663
i = 11 x = 0.3243657799085327
i = 12 x = 0.6487315598170653
i = 13 x = 0.7025368803658694
i = 14 x = 0.5949262392682613
i = 15 x = 0.8101475214634775
i = 16 x = 0.3797049570730451
i = 17 x = 0.7594099141460902
i = 18 x = 0.4811801717078197
i = 19 x = 0.9623603434156394
i = 20 x = 0.0752793131687213
i = 21 x = 0.1505586263374425
i = 22 x = 0.3011172526748851
i = 23 x = 0.6022345053497702
i = 24 x = 0.7955309893004596
i = 25 x = 0.4089380213990808
i = 26 x = 0.8178760427981615
i = 27 x = 0.3642479144036770
i = 28 x = 0.7284958288073540
i = 29 x = 0.5430083423852921
i = 30 x = 0.9139833152294159
i = 31 x = 0.1720333695411682
i = 32 x = 0.3440667390823364
i = 33 x = 0.6881334781646729
i = 34 x = 0.6237330436706543
i = 35 x = 0.7525339126586914
i = 36 x = 0.4949321746826172
i = 37 x = 0.9898643493652344
i = 38 x = 0.0202713012695312
i = 39 x = 0.0405426025390625
i = 40 x = 0.0810852050781250
i = 41 x = 0.1621704101562500
i = 42 x = 0.3243408203125000
i = 43 x = 0.6486816406250000
i = 44 x = 0.7026367187500000
i = 45 x = 0.5947265625000000
i = 46 x = 0.8105468750000000
i = 47 x = 0.3789062500000000
i = 48 x = 0.7578125000000000
i = 49 x = 0.4843750000000000
i = 50 x = 0.9687500000000000
i = 51 x = 0.0625000000000000
i = 52 x = 0.1250000000000000
i = 53 x = 0.2500000000000000
i = 54 x = 0.5000000000000000
i = 55 x = 1.0000000000000000
i = 56 x = 0.0000000000000000
i = 57 x = 0.0000000000000000
i = 58 x = 0.0000000000000000
i = 59 x = 0.0000000000000000
i = 60 x = 0.0000000000000000
i = 61 x = 0.0000000000000000
i = 62 x = 0.0000000000000000
i = 63 x = 0.0000000000000000
i = 64 x = 0.0000000000000000
i = 65 x = 0.0000000000000000
i = 66 x = 0.0000000000000000
i = 67 x = 0.0000000000000000
i = 68 x = 0.0000000000000000
i = 69 x = 0.0000000000000000
i = 70 x = 0.0000000000000000
i = 71 x = 0.0000000000000000
i = 72 x = 0.0000000000000000
i = 73 x = 0.0000000000000000
i = 74 x = 0.0000000000000000
i = 75 x = 0.0000000000000000
i = 76 x = 0.0000000000000000
i = 77 x = 0.0000000000000000
i = 78 x = 0.0000000000000000
i = 79 x = 0.0000000000000000
i = 80 x = 0.0000000000000000
i = 81 x = 0.0000000000000000
i = 82 x = 0.0000000000000000
i = 83 x = 0.0000000000000000
i = 84 x = 0.0000000000000000
i = 85 x = 0.0000000000000000
i = 86 x = 0.0000000000000000
i = 87 x = 0.0000000000000000
i = 88 x = 0.0000000000000000
i = 89 x = 0.0000000000000000
i = 90 x = 0.0000000000000000
i = 91 x = 0.0000000000000000
i = 92 x = 0.0000000000000000
i = 93 x = 0.0000000000000000
i = 94 x = 0.0000000000000000
i = 95 x = 0.0000000000000000
i = 96 x = 0.0000000000000000
i = 97 x = 0.0000000000000000
i = 98 x = 0.0000000000000000
i = 99 x = 0.0000000000000000
 
</pre>
 
 
====Porównywanie====