====== 2. Reálná čísla a paměti SRAM a DRAM ====== {{ .:lekce02-numbers.pdf | PDF }} [[https://gitlab.fel.cvut.cz/b35apo/apo-slides/-/tree/main/02-numbers|TeX]] === Další materiály === {{..:..:en:lectures:02:b35apo_lecture02-float-mem-en.pdf|Real Numbers and Memory}} {{..:..:en:lectures:02:b35apo_lecture02-float-mem-en.pdf|ODP}} === Programové ukázky === Program add.c: #include int main() { unsigned char a=150u, b=120u, c; unsigned short as=40000u, bs=30000u, cs; char ac=-100, bc=-80, cc; short sas=-20000, sbs=-14000, scs; c = a+b; cs=as+bs; cc=ac+bc; scs=sas+sbs; printf("c=%u cs=%u cc=%i scs=%i\n", c, cs, cc, scs); } Program add_test_overflow.c: #include int main() { unsigned char a=150u, b=120u, c; unsigned short as=40000u, bs=30000u, cs; char ac=-100, bc=-80, cc; short sas=-20000, sbs=-14000, scs; if ( __builtin_add_overflow(a,b,&c)) { printf("Overflow detected a+b\n"); } else { printf("c=%u\n", c); } a=120u; if ( __builtin_add_overflow(a,b,&c)) { printf("Overflow detected a+b\n"); } else { printf("c=%u\n", c); } if ( __builtin_add_overflow(as,bs,&cs)) { printf("Overflow detected as+bs\n"); } else { printf("cs=%u\n", cs); } as=33333u; if ( __builtin_add_overflow(as,bs,&cs)) { printf("Overflow detected as+bs\n"); } else { printf("cs=%u\n", cs); } if ( __builtin_add_overflow(ac,bc,&cc)) { printf("Overflow detected ac+bc\n"); } else { printf("cc=%i\n", cc); } ac=-40; if ( __builtin_add_overflow(ac,bc,&cc)) { printf("Overflow detected ac+bc\n"); } else { printf("cc=%i\n", cc); } if ( __builtin_add_overflow(sas,sbs,&scs)) { printf("Overflow detected sas+sbs\n"); } else { printf("scs=%i\n", scs); } sas=-12000; if ( __builtin_add_overflow(sas,sbs,&scs)) { printf("Overflow detected sas+sbs\n"); } else { printf("scs=%i\n", scs); } } Program store.c: #include int main() { unsigned char p[] = {0,0,0xa0,0xbf}; printf("%i = %u = %f\n", *(int*)p, *(unsigned int *)p, *(float *)p); } Program little.c: #include int main() { unsigned char p[] = {0,0,0xa0,0xbf}; *(int*)p=10; printf("%02x,%02x,%02x,%02x\n", p[0],p[1],p[2],p[3]); } ===== Odkazy na další literaturu ===== * [[http://www.root.cz/autori/pavel-tisnovsky/|Tišnovský, Pavel]]: [[https://www.root.cz/clanky/interni-reprezentace-numerickych-hodnot-od-skutecneho-pocitacoveho-praveku-po-ieee-754-2008/|Interní reprezentace numerických hodnot: od skutečného počítačového pravěku po IEEE 754–2008]], článek na serveru [[http://root.cz|Root.cz]].