さまざまな言語で数値計算
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 x2: x2 = x * x Dim d2: d2 = myExp(x, x2, 30, 0.0) '30:必要な精度が得られるよう, 6から始めて4ずつ増加させる '標準関数との差異 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 x2, ByVal n, ByVal t) t = x2 / (n + t) n = n - 4 If (n < 6) Then myExp = 1 + ((2 * x) / (2 - x + t)) Else myExp = myExp(x, x2, n, t) End If End Function
Z:\>cscript //nologo 0505.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.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
JScript
for (var i = -10; i <= 10; i++) { var x = i / 4.0; // 標準の指数関数 var d1 = Math.exp(x); // 自作の指数関数 var x2 = x * x; var d2 = myExp(x, x2, 30, 0.0); // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる // 標準関数との差異 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, x2, n, t) { t = x2 / (n + t); n -= 4; if (n < 6) return 1 + ((2 * x) / (2 - x + t)); else return myExp(x, x2, n, t); }
Z:\>cscript //nologo 0505.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.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
PowerShell
# 自作の指数関数 function myExp($x, $x2, $n, $t) { $t = $x2 / ($n + $t) $n -= 4 if ($n -lt 6) { 1 + ((2 * $x) / (2 - $x + $t)) } else { myExp $x $x2 $n $t } } foreach ($i in 0..20) { $x = ($i - 10) / 4.0 # 標準の指数関数 $d1 = [Math]::Exp($x) # 自作の指数関数 $x2 = $x * $x $d2 = myExp $x $x2 30 0.0 # 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる # 標準関数との差異 Write-Host ([string]::format("{0,5:F2} : {1,13:F10} - {2,13:F10} = {3,13:F10}", $x, $d1, $d2, $d1 - $d2)) }
Z:\>powershell -file 0505.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.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
Perl
# 自作の指数関数 sub myExp { my ($x, $x2, $n, $t) = @_; $t = $x2 / ($n + $t); $n -= 4; if ($n < 6) { 1 + ((2 * $x) / (2 - $x + $t)); } else { myExp($x, $x2, $n, $t); } } for $i (0..20) { $x = ($i - 10) / 4.0; # 標準の指数関数 $d1 = exp($x); # 自作の指数関数 $x2 = $x * $x; $d2 = myExp($x, $x2, 30, 0.0); # 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる # 標準関数との差異 printf("%5.2f : %13.10f - %13.10f = %13.10f\n", $x, $d1, $d2, $d1 - $d2); }
Z:\>perl 0505.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.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
PHP
<?php # 自作の指数関数 function myExp($x, $x2, $n, $t) { $t = $x2 / ($n + $t); $n -= 4; if ($n < 6) return 1 + ((2 * $x) / (2 - $x + $t)); else return myExp($x, $x2, $n, $t); } foreach (range(0, 20) as $i) { $x = ($i - 10) / 4.0; # 標準の指数関数 $d1 = exp($x); # 自作の指数関数 $x2 = $x * $x; $d2 = myExp($x, $x2, 30, 0.0); # 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる # 標準関数との差異 printf("%5.2f : %13.10f - %13.10f = %13.10f\n", $x, $d1, $d2, $d1 - $d2); } ?>
Z:\>php 0505.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.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
Python
# coding: Shift_JIS import math # 自作の指数関数 def myExp(x, x2, n, t): t = x2 / (n + t) n -= 4 if (n < 6): return 1 + ((2 * x) / (2 - x + t)) else: return myExp(x, x2, n, t) for i in range(0, 21): x = (i - 10) / 4.0 # 標準の指数関数 d1 = math.exp(x) # 自作の指数関数 x2 = x * x d2 = myExp(x, x2, 30, 0.0) # 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる # 標準関数との差異 print "%5.2f : %13.10f - %13.10f = %13.10f" % (x, d1, d2, d1 - d2)
Z:\>python 0505.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.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
Ruby
# 自作の指数関数 def myExp(x, x2, n, t) t = x2 / (n + t) n -= 4 if (n < 6) 1 + ((2 * x) / (2 - x + t)) else myExp(x, x2, n, t) end end (0..20).each do |i| x = (i - 10) / 4.0 # 標準の指数関数 d1 = Math.exp(x) # 自作の指数関数 x2 = x * x d2 = myExp(x, x2, 30, 0.0) # 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる # 標準関数との差異 printf("%5.2f : %13.10f - %13.10f = %13.10f\n", x, d1, d2, d1 - d2) end
Z:\>ruby 0505.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.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
Groovy
Pascal
Program Pas0505(arg); {$MODE delphi} uses SysUtils, Math; // 自作の指数関数 function myExp(x:Double; x2:Double; n:Integer; t:Double):Double; begin t := x2 / (n + t); n := n - 4; if (n < 6) then result := 1 + ((2 * x) / (2 - x + t)) else result := myExp(x, x2, n, t); end; var i: Integer; x: Double; x2: Double; d1: Double; d2: Double; begin for i := 0 to 20 do begin x := (i - 10) / 4.0; // 標準の指数関数 d1 := Exp(x); // 自作の指数関数 x2 := x * x; d2 := myExp(x, x2, 30, 0.0); // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる // 標準関数との差異 writeln(format('%5.2f : %13.10f - %13.10f = %13.10f', [x, d1, d2, d1 - d2])); end; end.
Z:\>fpc Pas0505.pp -v0 Free Pascal Compiler version 2.6.2 [2013/02/12] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Z:\>Pas0505 -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
Ada
VB.NET
Module VB0505 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 x2 As Double = x * x Dim d2 As Double = myExp(x, x2, 30, 0.0) '30:必要な精度が得られるよう, 6から始めて4ずつ増加させる '標準関数との差異 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 x2 As Double, ByVal n As Integer, ByVal t As Double) As Double t = x2 / (n + t) n -= 4 If (n < 6) Then Return 1 + ((2 * x) / (2 - x + t)) Else Return myExp(x, x2, n, t) End If End Function End Module
Z:\>vbc -nologo VB0505.vb Z:\>VB0505 -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
C#
using System; public class CS0505 { public static void Main() { for (int i = -10; i <= 10; i++) { double x = i / 4.0; // 標準の指数関数 double d1 = Math.Exp(x); // 自作の指数関数 double x2 = x * x; double d2 = myExp(x, x2, 30, 0.0); // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる // 標準関数との差異 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, double x2, int n, double t) { t = x2 / (n + t); n -= 4; if (n < 6) return 1 + ((2 * x) / (2 - x + t)); else return myExp(x, x2, n, t); } }
Z:\>csc -nologo CS0505.cs Z:\>CS0505 -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
Java
public class Java0505 { public static void main(String []args) { for (int i = -10; i <= 10; i++) { double x = i / 4.0; // 標準の指数関数 double d1 = Math.exp(x); // 自作の指数関数 double x2 = x * x; double d2 = myExp(x, x2, 30, 0.0); // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる // 標準関数との差異 System.out.println(String.format("%5.2f : %13.10f - %13.10f = %13.10f", x, d1, d2, d1 - d2)); } } // 自作の指数関数 private static double myExp(double x, double x2, int n, double t) { t = x2 / (n + t); n -= 4; if (n < 6) return 1 + ((2 * x) / (2 - x + t)); else return myExp(x, x2, n, t); } }
Z:\>javac Java0505.java Z:\>java Java0505 -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
C++
#include <iostream> #include <iomanip> #include <math> using namespace std; double myExp(double x, double x2, int n, double t); int main() { for (int i = -10; i <= 10; i++) { double x = i / 4.0; // 標準の指数関数 double d1 = exp(x); // 自作の指数関数 double x2 = x * x; double d2 = myExp(x, x2, 30, 0.0); // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる // 標準関数との差異 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, double x2, int n, double t) { t = x2 / (n + t); n -= 4; if (n < 6) return 1 + ((2 * x) / (2 - x + t)); else return myExp(x, x2, n, t); }
Z:\>bcc32 CP0505.cpp Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland CP0505.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Z:\>CP0505 -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, double x2, int n, double t); int main() { int i; for (i = -10; i <= 10; i++) { double x = i / 4.0; // 標準の指数関数 double d1 = exp(x); // 自作の指数関数 double x2 = x * x; double d2 = myExp(x, x2, 30, 0.0); // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる // 標準関数との差異 printf("%+04.2f : %+014.10f - %+014.10f = %+13.10f\n", x, d1, d2, d1 - d2); } return 0; } // 自作の指数関数 double myExp(double x, double x2, int n, double t) { t = x2 / (n + t); n -= 4; if (n < 6) return 1 + ((2 * x) / (2 - x + t)); else return myExp(x, x2, n, t); }
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.1737739435 = -0.0000000000 -1.50 : +00.2231301601 - +00.2231301601 = -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, x2:Double, n:Int, t:Double):Double = { val denom = x2 / (n + t) val num = n - 4 if (num < 6) 1 + ((2 * x) / (2 - x + denom)) else myExp(x, x2, num, denom) }
(0 to 20). map(n => (n - 10) / 4.0). foreach { x => // 標準の指数関数 val d1 = Math.exp(x) // 自作の指数関数 val x2 = x * x val d2 = myExp(x, x2, 30, 0.0) // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる // 標準関数との差異 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.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
終了
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) (x2:double) (n:int) (t:double):double = let denom = x2 / ((double n) + t) let num = n - 4; if num < 6 then 1.0 + ((2.0 * x) / (2.0 - x + denom)) else myExp x x2 num denom
[0..20] |> List.map (fun n -> (double (n - 10)) / 4.0) |> List.iter (fun x -> // 標準の指数関数 let d1 = System.Math.Exp(x) // 自作の指数関数 let x2 = x * x let d2 = (myExp x x2 30 0.0) // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる // 標準関数との差異 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.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 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 x2 n t] (def denom (/ x2 (+ n t))) (def nume (- n 4)) (if (< nume 6) (+ 1.0 (/ (* 2.0 x) (+ (- 2.0 x) denom))) (myExp x x2 nume denom)))
(doseq [x (map #(/ (- % 10) 4.0) (range 0 20))] (do ;標準の指数関数 (def d1 (. Math exp x)) ;自作の指数関数 (def x2 (* x x)) ;標準関数との差異 (def d2 (myExp x x2 30 0.0)) ;30:必要な精度が得られるよう, 6から始めて4ずつ増加させる (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.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 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->Double->Int->Double->Double; myExp x x2 n t = let denom = x2 / ((fromIntegral n) + t) num = n - 4 in if num < 6 then 1.0 + ((2.0 * x) / (2.0 - x + denom)) else myExp x x2 num denom
import Text.Printf import Control.Monad forM_ ( map (\n -> (fromIntegral (n - 10)) / 4.0) $ [0..20::Int] ) $ \x -> do -- 標準の指数関数 let d1 = exp(x) -- 自作の指数関数 let x2 = x * x let d2 = (myExp x x2 30 0.0) -- 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる -- 標準関数との差異 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.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
終了
Prelude> :quit Leaving GHCi.