さまざまな言語で数値計算
Only Do What Only You Can Do
指数関数 (級数展開)
級数展開(テイラー展開)で $ e^x $ を求めます.
VBScript
Option Explicit Dim i For i = 0 To 20 Dim x: x = (i - 10) / 4.0 '標準の指数関数 Dim d1: d1 = Exp(x) '自作の指数関数 Dim d2: d2 = myExp(x, 1, 1.0, 1.0, 1.0) '標準関数との差異 WScript.StdOut.Write Right(Space(5) & FormatNumber(x, 2, -1, 0, 0), 5) & " : " WScript.StdOut.Write Right(Space(13) & FormatNumber(d1, 10, -1, 0, 0), 13) & " - " WScript.StdOut.Write Right(Space(13) & FormatNumber(d2, 10, -1, 0, 0), 13) & " = " WScript.StdOut.Write Right(Space(13) & FormatNumber(d1 - d2, 10, -1, 0, 0), 13) & vbNewLine Next '自作の指数関数 Private Function myExp(ByVal x, ByVal n, ByVal numerator, ByVal denominator, ByVal y) denominator = denominator * n numerator = numerator * x Dim a: a = numerator / denominator '十分な精度になったら処理を抜ける If (Abs(a) <= 0.00000000001) Then myExp = y Else myExp = y + myExp(x, n + 1, numerator, denominator, a) End If End Function
Z:\>cscript //nologo 0504.vbs -2.50 : 0.0820849986 - 0.0820849986 = -0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = -0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = -0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = -0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = -0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
JScript
for (var i = -10; i <= 10; i++) { var x = i / 4.0; // 標準の指数関数 var d1 = Math.exp(x); // 自作の指数関数 var d2 = myExp(x, 1, 1.0, 1.0, 1.0); // 標準関数との差異 WScript.StdOut.Write((" " + x.toFixed(2) ).slice(-5) + " : "); WScript.StdOut.Write((" " + d1.toFixed(10) ).slice(-13) + " - "); WScript.StdOut.Write((" " + d2.toFixed(10) ).slice(-13) + " = "); WScript.StdOut.Write((" " + (d1 - d2).toFixed(10)).slice(-13) + "\n" ); } // 自作の指数関数 function myExp(x, n, numerator, denominator, y) { denominator = denominator * n; numerator = numerator * x; var a = numerator / denominator; // 十分な精度になったら処理を抜ける if (Math.abs(a) <= 0.00000000001) return y; else return y + myExp(x, ++n, numerator, denominator, a); }
Z:\>cscript //nologo 0504.js -2.50 : 0.0820849986 - 0.0820849986 = -0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = -0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = -0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = -0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = -0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
PowerShell
# 自作の指数関数 function myExp($x, $n, $numerator, $denominator, $y) { $denominator = $denominator * $n $numerator = $numerator * $x $a = $numerator / $denominator # 十分な精度になったら処理を抜ける if ([Math]::Abs($a) -le 0.00000000001) { $y } else { $y + (myExp $x ($n + 1) $numerator $denominator $a) } } foreach ($i in 0..20) { $x = ($i - 10) / 4.0 # 標準の指数関数 $d1 = [Math]::Exp($x) # 自作の指数関数 $d2 = myExp $x 1 1.0 1.0 1.0 # 標準関数との差異 Write-Host ([string]::format("{0,5:F2} : {1,13:F10} - {2,13:F10} = {3,13:F10}", $x, $d1, $d2, $d1 - $d2)) }
Z:\>powershell -file 0504.ps1 -2.50 : 0.0820849986 - 0.0820849986 = 0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = 0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = 0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = 0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = 0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
Perl
# 自作の指数関数 sub myExp { my ($x, $n, $numerator, $denominator, $y) = @_; $denominator = $denominator * $n; $numerator = $numerator * $x; $a = $numerator / $denominator; # 十分な精度になったら処理を抜ける if (abs($a) <= 0.00000000001) { $y; } else { $y + myExp($x, ++$n, $numerator, $denominator, $a); } } for $i (0..20) { $x = ($i - 10) / 4.0; # 標準の指数関数 $d1 = exp($x); # 自作の指数関数 $d2 = myExp($x, 1, 1.0, 1.0, 1.0); # 標準関数との差異 printf("%5.2f : %13.10f - %13.10f = %13.10f\n", $x, $d1, $d2, $d1 - $d2); }
Z:\>perl 0504.pl -2.50 : 0.0820849986 - 0.0820849986 = -0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = -0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = -0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = -0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = -0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
PHP
<?php # 自作の指数関数 function myExp($x, $n, $numerator, $denominator, $y) { $denominator = $denominator * $n; $numerator = $numerator * $x; $a = $numerator / $denominator; # 十分な精度になったら処理を抜ける if (abs($a) <= 0.00000000001) return $y; else return $y + myExp($x, ++$n, $numerator, $denominator, $a); } foreach (range(0, 20) as $i) { $x = ($i - 10) / 4.0; # 標準の指数関数 $d1 = exp($x); # 自作の指数関数 $d2 = myExp($x, 1, 1.0, 1.0, 1.0); # 標準関数との差異 printf("%5.2f : %13.10f - %13.10f = %13.10f\n", $x, $d1, $d2, $d1 - $d2); } ?>
Z:\>php 0504.php -2.50 : 0.0820849986 - 0.0820849986 = -0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = -0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = -0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = -0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = -0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
Python
# coding: Shift_JIS import math # 自作の指数関数 def myExp(x, n, numerator, denominator, y): denominator = denominator * n numerator = numerator * x a = numerator / denominator # 十分な精度になったら処理を抜ける if (abs(a) <= 0.00000000001): return y else: return y + myExp(x, n + 1, numerator, denominator, a) for i in range(0, 21): x = (i - 10) / 4.0 # 標準の指数関数 d1 = math.exp(x) # 自作の指数関数 d2 = myExp(x, 1, 1.0, 1.0, 1.0) # 標準関数との差異 print "%5.2f : %13.10f - %13.10f = %13.10f" % (x, d1, d2, d1 - d2)
Z:\>python 0504.py -2.50 : 0.0820849986 - 0.0820849986 = -0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = -0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = -0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = -0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = -0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
Ruby
# 自作の指数関数 def myExp(x, n, numerator, denominator, y) denominator = denominator * n numerator = numerator * x a = numerator / denominator; # 十分な精度になったら処理を抜ける if (a.abs <= 0.00000000001) y else y + myExp(x, n + 1, numerator, denominator, a) end end (0..20).each do |i| x = (i - 10) / 4.0 # 標準の指数関数 d1 = Math.exp(x) # 自作の指数関数 d2 = myExp(x, 1, 1.0, 1.0, 1.0) # 標準関数との差異 printf("%5.2f : %13.10f - %13.10f = %13.10f\n", x, d1, d2, d1 - d2) end
Z:\>ruby 0504.rb -2.50 : 0.0820849986 - 0.0820849986 = -0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = -0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = -0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = -0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = -0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
Groovy
Pascal
Program Pas0504(arg); {$MODE delphi} uses SysUtils, Math; // 自作の指数関数 function myExp(x:Double; n:Integer; numerator:Double; denominator:Double; y:Double):Double; var a: Double; begin denominator := denominator * n; numerator := numerator * x; a := numerator / denominator; // 十分な精度になったら処理を抜ける if (Abs(a) <= 0.00000000001) then result := y else result := y + myExp(x, n + 1, numerator, denominator, a); end; var i: Integer; x: Double; d1: Double; d2: Double; begin for i := 0 to 20 do begin x := (i - 10) / 4.0; // 標準の指数関数 d1 := Exp(x); // 自作の指数関数 d2 := myExp(x, 1, 1.0, 1.0, 1.0); // 標準関数との差異 writeln(format('%5.2f : %13.10f - %13.10f = %13.10f', [x, d1, d2, d1 - d2])); end; end.
Z:\>fpc Pas0504.pp -v0 Free Pascal Compiler version 2.6.2 [2013/02/12] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Z:\>Pas0504 -2.50 : 0.0820849986 - 0.0820849986 = 0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = 0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = 0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = 0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = 0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
Ada
VB.NET
Module VB0504 Public Sub Main() For i As Integer = 0 To 20 Dim x As Double = (i - 10) / 4.0 '標準の指数関数 Dim d1 As Double = Math.Exp(x) '自作の指数関数 Dim d2 As Double = myExp(x, 1, 1.0, 1.0, 1.0) '標準関数との差異 Console.WriteLine(String.Format("{0,5:F2} : {1,13:F10} - {2,13:F10} = {3,13:F10}", x, d1, d2, d1 - d2)) Next End Sub '自作の指数関数 Private Function myExp(ByVal x As Double, ByVal n As Integer, ByVal numerator As Double, ByVal denominator As Double, ByVal y As Double) As Double denominator = denominator * n numerator = numerator * x Dim a As Double = numerator / denominator '十分な精度になったら処理を抜ける If (Math.Abs(a) <= 0.00000000001) Then Return y Else Return y + myExp(x, n + 1, numerator, denominator, a) End If End Function End Module
Z:\>vbc -nologo VB0504.vb Z:\>VB0504 -2.50 : 0.0820849986 - 0.0820849986 = 0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = 0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = 0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = 0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = 0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
C#
using System; public class CS0504 { public static void Main() { for (int i = -10; i <= 10; i++) { double x = i / 4.0; // 標準の指数関数 double d1 = Math.Exp(x); // 自作の指数関数 double d2 = myExp(x, 1, 1.0, 1.0, 1.0); // 標準関数との差異 Console.WriteLine(string.Format("{0,5:F2} : {1,13:F10} - {2,13:F10} = {3,13:F10}", x, d1, d2, d1 - d2)); } } // 自作の指数関数 private static double myExp(double x, int n, double numerator, double denominator, double y) { denominator = denominator * n; numerator = numerator * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (Math.Abs(a) <= 0.00000000001) return y; else return y + myExp(x, ++n, numerator, denominator, a); } }
Z:\>csc -nologo CS0504.cs Z:\>CS0504 -2.50 : 0.0820849986 - 0.0820849986 = 0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = 0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = 0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = 0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = 0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
Java
public class Java0504 { public static void main(String []args) { for (int i = -10; i <= 10; i++) { double x = i / 4.0; // 標準の指数関数 double d1 = Math.exp(x); // 自作の指数関数 double d2 = myExp(x, 1, 1.0, 1.0, 1.0); // 標準関数との差異 System.out.println(String.format("%5.2f : %13.10f - %13.10f = %13.10f", x, d1, d2, d1 - d2)); } } // 自作の指数関数 private static double myExp(double x, int n, double numerator, double denominator, double y) { denominator = denominator * n; numerator = numerator * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (Math.abs(a) <= 0.00000000001) return y; else return y + myExp(x, ++n, numerator, denominator, a); } }
Z:\>javac Java0504.java Z:\>java Java0504 -2.50 : 0.0820849986 - 0.0820849986 = -0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = -0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = -0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = -0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = -0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
C++
#include <iostream> #include <iomanip> #include <math> using namespace std; double myExp(double x, int n, double numerator, double denominator, double y); int main() { for (int i = -10; i <= 10; i++) { double x = i / 4.0; // 標準の指数関数 double d1 = exp(x); // 自作の指数関数 double d2 = myExp(x, 1, 1.0, 1.0, 1.0); // 標準関数との差異 cout << setw(5) << fixed << setprecision(2) << x << ":"; cout << setw(14) << fixed << setprecision(10) << d1 << " - "; cout << setw(14) << fixed << setprecision(10) << d2 << " = "; cout << setw(14) << fixed << setprecision(10) << d1 - d2 << endl; } return 0; } // 自作の指数関数 double myExp(double x, int n, double numerator, double denominator, double y) { denominator = denominator * n; numerator = numerator * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (fabs(a) <= 0.00000000001) return y; else return y + myExp(x, ++n, numerator, denominator, a); }
Z:\>bcc32 CP0504.cpp Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland CP0504.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Z:\>CP0504 -2.50: 0.0820849986 - 0.0820849986 = 0.0000000000 -2.25: 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00: 0.1353352832 - 0.1353352832 = 0.0000000000 -1.75: 0.1737739435 - 0.1737739435 = 0.0000000000 -1.50: 0.2231301601 - 0.2231301601 = 0.0000000000 -1.25: 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00: 0.3678794412 - 0.3678794412 = 0.0000000000 -0.75: 0.4723665527 - 0.4723665527 = 0.0000000000 -0.50: 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25: 0.7788007831 - 0.7788007831 = 0.0000000000 0.00: 1.0000000000 - 1.0000000000 = 0.0000000000 0.25: 1.2840254167 - 1.2840254167 = 0.0000000000 0.50: 1.6487212707 - 1.6487212707 = 0.0000000000 0.75: 2.1170000166 - 2.1170000166 = 0.0000000000 1.00: 2.7182818285 - 2.7182818285 = 0.0000000000 1.25: 3.4903429575 - 3.4903429575 = 0.0000000000 1.50: 4.4816890703 - 4.4816890703 = -0.0000000000 1.75: 5.7546026760 - 5.7546026760 = 0.0000000000 2.00: 7.3890560989 - 7.3890560989 = 0.0000000000 2.25: 9.4877358364 - 9.4877358364 = 0.0000000000 2.50: 12.1824939607 - 12.1824939607 = -0.0000000000
Objective-C
#import <Foundation/Foundation.h> double myExp(double x, int n, double numerator, double denominator, double y); int main() { int i; for (i = -10; i <= 10; i++) { double x = i / 4.0; // 標準の指数関数 double d1 = exp(x); // 自作の指数関数 double d2 = myExp(x, 1, 1.0, 1.0, 1.0); // 標準関数との差異 printf("%+04.2f : %+014.10f - %+014.10f = %+13.10f\n", x, d1, d2, d1 - d2); } return 0; } // 自作の指数関数 double myExp(double x, int n, double numerator, double denominator, double y) { denominator = denominator * n; numerator = numerator * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (fabs(a) <= 0.00000000001) return y; else return y + myExp(x, ++n, numerator, denominator, a); }
Compiling the source code.... $gcc `gnustep-config --objc-flags` -L/usr/GNUstep/System/Library/Libraries -lgnustep-base main.m -o demo -lm -pthread -lgmpxx -lreadline 2>&1 Executing the program.... $demo -2.50 : +00.0820849986 - +00.0820849986 = -0.0000000000 -2.25 : +00.1053992246 - +00.1053992246 = +0.0000000000 -2.00 : +00.1353352832 - +00.1353352832 = -0.0000000000 -1.75 : +00.1737739435 - +00.1737739434 = +0.0000000000 -1.50 : +00.2231301601 - +00.2231301602 = -0.0000000000 -1.25 : +00.2865047969 - +00.2865047969 = +0.0000000000 -1.00 : +00.3678794412 - +00.3678794412 = -0.0000000000 -0.75 : +00.4723665527 - +00.4723665527 = -0.0000000000 -0.50 : +00.6065306597 - +00.6065306597 = +0.0000000000 -0.25 : +00.7788007831 - +00.7788007831 = +0.0000000000 +0.00 : +01.0000000000 - +01.0000000000 = +0.0000000000 +0.25 : +01.2840254167 - +01.2840254167 = +0.0000000000 +0.50 : +01.6487212707 - +01.6487212707 = +0.0000000000 +0.75 : +02.1170000166 - +02.1170000166 = +0.0000000000 +1.00 : +02.7182818285 - +02.7182818285 = +0.0000000000 +1.25 : +03.4903429575 - +03.4903429575 = +0.0000000000 +1.50 : +04.4816890703 - +04.4816890703 = +0.0000000000 +1.75 : +05.7546026760 - +05.7546026760 = +0.0000000000 +2.00 : +07.3890560989 - +07.3890560989 = +0.0000000000 +2.25 : +09.4877358364 - +09.4877358364 = +0.0000000000 +2.50 : +12.1824939607 - +12.1824939607 = +0.0000000000
D
Go
Scala
対話型実行環境を起動
Z:\>scala Welcome to Scala version 2.10.2 (Java HotSpot(TM) Client VM, Java 1.7.0_21). Type in expressions to have them evaluated. Type :help for more information.
級数展開(テイラー展開)で ex を求める
// 自作の指数関数 def myExp(x:Double, n:Int, numerator:Double, denominator:Double, y:Double):Double = { val denom = denominator * n val num = numerator * x val a = num / denom // 十分な精度になったら処理を抜ける if (Math.abs(a) <= 0.00000000001) y else y + myExp(x, n + 1, num, denom, a) }
(0 to 20). map(n => (n - 10) / 4.0). foreach { x => // 標準の指数関数 val d1 = Math.exp(x) // 自作の指数関数 val d2 = myExp(x, 1, 1.0, 1.0, 1.0) // 標準関数との差異 System.out.println("%5.2f : %13.10f - %13.10f = %13.10f".format(x, d1, d2, d1 - d2)); }
-2.50 : 0.0820849986 - 0.0820849986 = -0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = -0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = -0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = -0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = -0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
終了
scala> :quit
F#
対話型実行環境を起動
Z:\>fsi Microsoft (R) F# 2.0 Interactive build 4.0.40219.1 Copyright (c) Microsoft Corporation. All Rights Reserved. For help type #help;;
級数展開(テイラー展開)で ex を求める
// 自作の指数関数 let rec myExp (x:double) (n:int) (numerator:double) (denominator:double) (y:double):double = let denom = denominator * (double n) let num = numerator * x let a = num / denom // 十分な精度になったら処理を抜ける if abs(a) <= 0.00000000001 then y else y + (myExp x (n + 1) num denom a)
[0..20] |> List.map (fun n -> (double (n - 10)) / 4.0) |> List.iter (fun x -> // 標準の指数関数 let d1 = System.Math.Exp(x) // 自作の指数関数 let d2 = (myExp x 1 1.0 1.0 1.0) // 標準関数との差異 printfn "%5.2f : %13.10f - %13.10f = %13.10f" x d1 d2 (d1 - d2) )
-2.50 : 0.0820849986 - 0.0820849986 = 0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = 0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = 0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = 0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = 0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000 val it : unit = ()
終了
> #quit;;
Clojure
対話型実行環境を起動
Z:\>java -cp C:\ProgramFiles\clojure-1.5.1\clojure-1.5.1.jar clojure.main Clojure 1.5.1
級数展開(テイラー展開)で ex を求める
;自作の指数関数 (defn myExp [x n numerator denominator y] (def denom (* denominator n)) (def nume (* numerator x)) (def a (/ nume denom)) ;十分な精度になったら処理を抜ける (if (<= (. Math abs a) 0.00000000001) y (+ y (myExp x (+ n 1) nume denom a))))
(doseq [x (map #(/ (- % 10) 4.0) (range 0 21))] (do ;標準の指数関数 (def d1 (. Math exp x)) ;自作の指数関数 (def d2 (myExp x 1 1.0 1.0 1.0)) ;標準関数との差異 (println (format "%5.2f : %13.10f - %13.10f = %13.10f" x d1 d2 (- d1 d2)))))
-2.50 : 0.0820849986 - 0.0820849986 = -0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = -0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = -0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = -0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = -0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000 nil
終了
user=> (. System exit 0)
Haskell
対話型実行環境を起動
Z:\>ghci GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done.
級数展開(テイラー展開)で ex を求める
-- 自作の指数関数 myExp::Double->Int->Double->Double->Double->Double myExp x n numerator denominator y = let denom = denominator * (fromIntegral n) num = numerator * x a = num / denom in -- 十分な精度になったら処理を抜ける if abs(a) <= 0.00000000001 then y else y + (myExp x (n + 1) num denom a)
import Text.Printf import Control.Monad forM_ ( map (\n -> (fromIntegral (n - 10)) / 4.0) $ [0..20::Int] ) $ \x -> do -- 標準の指数関数 let d1 = exp(x) -- 自作の指数関数 let d2 = (myExp x 1 1.0 1.0 1.0) -- 標準関数との差異 printf "%5.2f : %13.10f - %13.10f = %13.10f\n" x d1 d2 (d1- d2)
-2.50 : 0.0820849986 - 0.0820849986 = -0.0000000000 -2.25 : 0.1053992246 - 0.1053992246 = 0.0000000000 -2.00 : 0.1353352832 - 0.1353352832 = -0.0000000000 -1.75 : 0.1737739435 - 0.1737739434 = 0.0000000000 -1.50 : 0.2231301601 - 0.2231301602 = -0.0000000000 -1.25 : 0.2865047969 - 0.2865047969 = 0.0000000000 -1.00 : 0.3678794412 - 0.3678794412 = -0.0000000000 -0.75 : 0.4723665527 - 0.4723665527 = -0.0000000000 -0.50 : 0.6065306597 - 0.6065306597 = 0.0000000000 -0.25 : 0.7788007831 - 0.7788007831 = 0.0000000000 0.00 : 1.0000000000 - 1.0000000000 = 0.0000000000 0.25 : 1.2840254167 - 1.2840254167 = 0.0000000000 0.50 : 1.6487212707 - 1.6487212707 = 0.0000000000 0.75 : 2.1170000166 - 2.1170000166 = 0.0000000000 1.00 : 2.7182818285 - 2.7182818285 = 0.0000000000 1.25 : 3.4903429575 - 3.4903429575 = 0.0000000000 1.50 : 4.4816890703 - 4.4816890703 = 0.0000000000 1.75 : 5.7546026760 - 5.7546026760 = 0.0000000000 2.00 : 7.3890560989 - 7.3890560989 = 0.0000000000 2.25 : 9.4877358364 - 9.4877358364 = 0.0000000000 2.50 : 12.1824939607 - 12.1824939607 = 0.0000000000
終了
Prelude> :quit Leaving GHCi.