/* harjoitus 6 - tehtävä 3 */ #include #include void prn_message(); void prn_header(); void prn_data(); main() { prn_message(); prn_header(); prn_data(); } void prn_message() { printf("Will compute the expressions given " "in the exercise assignment.\n"); } void prn_header() { printf("%20s %20s %20s %20s %20s\n", "x", "sin(x)", "x^(-2.5)", "ceil(x)", "ln(x)"); } void prn_data() { double x; /* HUOM: Seuraava silmukka tulostaa vain x:n arvot 2.9:ään asti, vaikka selvästi näyttäisi siltä, että tilanne x = 3.0 lasketaan myös!! Syy on double (ja float) muuttujien äärellisessä tarkkuudessa -- kun tulostaa x:n riittävän suurella tarkkuudella, näkee, että sen tarkka arvo on aina hieman suurempi kuin 0.1:n moninkerta: for (x = -1.; x <= 3.; x += 0.1) { printf("%20.9lf %20.9lf %20.9lf %20.9lf %20.9lf\n", x, sin(x), pow(x, -2.5), ceil(x), log(x)); } /* tilanne on helppo korjata seuraavasti: */ for (x = -1.; x <= 3. + 0.05; x += 0.1) { printf("%20.9lf %20.9lf %20.9lf %20.9lf %20.9lf\n", x, sin(x), pow(x, -2.5), ceil(x), log(x)); } } /* ohjelman tulostus onpi seuraanvanlainen: */ /* Will compute the expressions given in the exercise assignment. x sin(x) x^(-2.5) ceil(x) ln(x) -1.000000000 -0.841470985 nan -1.000000000 -inf -0.900000000 -0.783326910 nan -0.000000000 -inf -0.800000000 -0.717356091 nan -0.000000000 -inf -0.700000000 -0.644217687 nan -0.000000000 -inf -0.600000000 -0.564642473 nan -0.000000000 -inf -0.500000000 -0.479425539 nan -0.000000000 -inf -0.400000000 -0.389418342 nan -0.000000000 -inf -0.300000000 -0.295520207 nan -0.000000000 -inf -0.200000000 -0.198669331 nan -0.000000000 -inf -0.100000000 -0.099833417 nan -0.000000000 -inf -0.000000000 -0.000000000 nan -0.000000000 -inf 0.100000000 0.099833417 316.227766017 1.000000000 -2.302585093 0.200000000 0.198669331 55.901699437 1.000000000 -1.609437912 0.300000000 0.295520207 20.286020648 1.000000000 -1.203972804 0.400000000 0.389418342 9.882117688 1.000000000 -0.916290732 0.500000000 0.479425539 5.656854249 1.000000000 -0.693147181 0.600000000 0.564642473 3.586095691 1.000000000 -0.510825624 0.700000000 0.644217687 2.439242060 1.000000000 -0.356674944 0.800000000 0.717356091 1.746928107 1.000000000 -0.223143551 0.900000000 0.783326910 1.301348831 1.000000000 -0.105360516 1.000000000 0.841470985 1.000000000 1.000000000 -0.000000000 1.100000000 0.891207360 0.787985611 2.000000000 0.095310180 1.200000000 0.932039086 0.633938145 2.000000000 0.182321557 1.300000000 0.963558185 0.518969242 2.000000000 0.262364264 1.400000000 0.985449730 0.431201150 2.000000000 0.336472237 1.500000000 0.997494987 0.362887369 2.000000000 0.405465108 1.600000000 0.999573603 0.308816178 2.000000000 0.470003629 1.700000000 0.991664810 0.265385809 2.000000000 0.530628251 1.800000000 0.973847631 0.230048146 2.000000000 0.587786665 1.900000000 0.946300088 0.200962950 2.000000000 0.641853886 2.000000000 0.909297427 0.176776695 3.000000000 0.693147181 2.100000000 0.863209367 0.156477451 3.000000000 0.741937345 2.200000000 0.808496404 0.139297492 3.000000000 0.788457360 2.300000000 0.745705212 0.124646592 3.000000000 0.832909123 2.400000000 0.675463181 0.112065490 3.000000000 0.875468737 2.500000000 0.598472144 0.101192885 3.000000000 0.916290732 2.600000000 0.515501372 0.091741668 3.000000000 0.955511445 2.700000000 0.427379880 0.083481566 3.000000000 0.993251773 2.800000000 0.334988150 0.076226314 3.000000000 1.029619417 2.900000000 0.239249329 0.069824045 3.000000000 1.064710737 3.000000000 0.141120008 0.064150030 4.000000000 1.098612289 */ /* huomaa, ettei negatiivisen luvun murtoluku potenssia ole määritelty, kuten ei negatiivisen luvun logaritmiakaan (olisi oikeastaan imaginaarinen) */