Created
February 11, 2010 02:58
-
-
Save clairvy/301159 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| napier |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| EXE = napier | |
| all: do | |
| do: build | |
| ./$(EXE) | |
| build: $(EXE) | |
| test: build | |
| prove ./test.pl | |
| clean: | |
| $(RM) $(RMF) $(EXE) | |
| # Haskell 版はオマケ | |
| hs: | |
| runhaskell napier.hs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <math.h> // for FUNC:exp(x) | |
| // http://ja.wikipedia.org/wiki/%E3%83%8D%E3%82%A4%E3%83%94%E3%82%A2%E6%95%B0 | |
| // e = exp(1) when e is Napier's constant | |
| // exp(x) = sum{n=0}{inf} {x^n} / {n!} | |
| int kaijyou(int p) | |
| { | |
| // 階乗を求める関数 | |
| int i; | |
| double product = 1; | |
| for (i = 0; i < p; i++) { | |
| product *= (i+1); | |
| } | |
| return product; | |
| } | |
| double napier(int p) | |
| { | |
| // 第P項までのネイピア数の近似値を求める関数 | |
| double sum = 0; | |
| int n; | |
| double x = 1; // napier 式の引数 | |
| for (n = 0; n <= p; n++) { | |
| sum += pow(x, n) / kaijyou(n); | |
| } | |
| return sum; | |
| } | |
| int main() | |
| { | |
| int n; | |
| int cnt; | |
| double answer; | |
| double e = exp(1); | |
| printf("計算する最大の項nを入力してください:"); | |
| scanf("%d", &n); | |
| for (cnt = 1; cnt <= n; cnt++) { | |
| answer = napier(cnt); | |
| printf("第%3d項までの近似値:%.13f 真値:%.13f 差:%.13f\n", cnt, answer, e, answer - e); | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- e = 2.71828 18284 59045 23536 02874 71352 | |
| -- a = e when (d/dx) a^x = a^x | |
| -- exp(x) = sum{n=0}{inf} {x^n} / {n!} | |
| fac n = product [1..n] | |
| napier m x = sum $ [ (x ** n) / (fac n) | n <- [0..m] ] | |
| main = print $ map (\x -> (x, napier x 1, (napier x 1) - exp(1))) [1..10] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use strict; | |
| use warnings; | |
| use Test::More tests => 1; | |
| my $expect = <<EOL; | |
| 計算する最大の項nを入力してください: | |
| 第 1項までの近似値:2.0000000000000 真値:2.7182818284590 差:-0.7182818284590 | |
| 第 2項までの近似値:2.5000000000000 真値:2.7182818284590 差:-0.2182818284590 | |
| 第 3項までの近似値:2.6666666666667 真値:2.7182818284590 差:-0.0516151617924 | |
| 第 4項までの近似値:2.7083333333333 真値:2.7182818284590 差:-0.0099484951257 | |
| 第 5項までの近似値:2.7166666666667 真値:2.7182818284590 差:-0.0016151617924 | |
| 第 6項までの近似値:2.7180555555556 真値:2.7182818284590 差:-0.0002262729035 | |
| 第 7項までの近似値:2.7182539682540 真値:2.7182818284590 差:-0.0000278602051 | |
| 第 8項までの近似値:2.7182787698413 真値:2.7182818284590 差:-0.0000030586178 | |
| 第 9項までの近似値:2.7182815255732 真値:2.7182818284590 差:-0.0000003028859 | |
| 第 10項までの近似値:2.7182818011464 真値:2.7182818284590 差:-0.0000000273127 | |
| EOL | |
| $expect =~ s/\n//; | |
| is(`echo 10 | ./napier`, $expect); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment