さまざまな言語で数値計算
Only Do What Only You Can Do
対数関数 (級数展開)
級数展開(テイラー展開)で $ \log x $ を求めます.
VBScript
Option Explicit Dim i For i = 1 To 20 Dim x: x = i / 5.0 '標準の対数関数 Dim d1: d1 = Log(x) '自作の対数関数 Dim x2: x2 = (x - 1) / (x + 1) Dim d2: d2 = 2 * myLog(x2, x2, 1.0, x2) '標準関数との差異 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 myLog(ByVal x2, ByVal numerator, ByVal denominator, ByVal y) denominator = denominator + 2 numerator = numerator * x2 * x2 Dim a: a = numerator / denominator '十分な精度になったら処理を抜ける If (Abs(a) <= 0.00000000001) Then myLog = y Else myLog = y + myLog(x2, numerator, denominator, a) End If End Function
Z:\>cscript //nologo 0506.vbs 0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
JScript
for (var i = 1; i <= 20; i++) { var x = i / 5.0; // 標準の対数関数 var d1 = Math.log(x); // 自作の対数関数 var x2 = (x - 1) / (x + 1); var d2 = 2 * myLog(x2, x2, 1.0, x2); // 標準関数との差異 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 myLog(x2, numerator, denominator, y) { denominator = denominator + 2; numerator = numerator * x2 * x2; var a = numerator / denominator; // 十分な精度になったら処理を抜ける if (Math.abs(a) <= 0.00000000001) return y; else return y + myLog(x2, numerator, denominator, a); }
Z:\>cscript //nologo 0506.js 0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
PowerShell
# 自作の対数関数 function myLog($x2, $numerator, $denominator, $y) { $denominator = $denominator + 2 $numerator = $numerator * $x2 * $x2 $a = $numerator / $denominator # 十分な精度になったら処理を抜ける if ([Math]::Abs($a) -le 0.00000000001) { $y } else { $y + (myLog $x2 $numerator $denominator $a) } } foreach ($i in 1..20) { $x = $i / 5.0 # 標準の対数関数 $d1 = [Math]::Log($x) # 自作の対数関数 $x2 = ($x - 1) / ($x + 1) $d2 = 2 * (myLog $x2 $x2 1.0 $x2) # 標準関数との差異 Write-Host ([string]::format("{0,5:F2} : {1,13:F10} - {2,13:F10} = {3,13:F10}", $x, $d1, $d2, $d1 - $d2)) }
Z:\>powershell -file 0506.ps1 0.20 : -1.6094379124 - -1.6094379124 = 0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = 0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = 0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = 0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
Perl
# 自作の対数関数 sub myLog { my ($x2, $numerator, $denominator, $y) = @_; $denominator = $denominator + 2; $numerator = $numerator * $x2 * $x2; $a = $numerator / $denominator; # 十分な精度になったら処理を抜ける if (abs($a) <= 0.00000000001) { $y; } else { $y + myLog($x2, $numerator, $denominator, $a); } } for $i (1..20) { $x = $i / 5.0; # 標準の対数関数 $d1 = log($x); # 自作の対数関数 $x2 = ($x - 1) / ($x + 1); $d2 = 2 * myLog($x2, $x2, 1.0, $x2); # 標準関数との差異 printf("%5.2f : %13.10f - %13.10f = %13.10f\n", $x, $d1, $d2, $d1 - $d2); }
Z:\>perl 0506.pl 0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
PHP
<?php # 自作の対数関数 function myLog($x2, $numerator, $denominator, $y) { $denominator = $denominator + 2; $numerator = $numerator * $x2 * $x2; $a = $numerator / $denominator; # 十分な精度になったら処理を抜ける if (abs($a) <= 0.00000000001) return $y; else return $y + myLog($x2, $numerator, $denominator, $a); } foreach (range(1, 20) as $i) { $x = $i / 5.0; # 標準の対数関数 $d1 = log($x); # 自作の対数関数 $x2 = ($x - 1) / ($x + 1); $d2 = 2 * myLog($x2, $x2, 1.0, $x2); # 標準関数との差異 printf("%5.2f : %13.10f - %13.10f = %13.10f\n", $x, $d1, $d2, $d1 - $d2); } ?>
Z:\>php 0506.php 0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
Python
# coding: Shift_JIS import math # 自作の対数関数 def myLog(x2, numerator, denominator, y): denominator = denominator + 2 numerator = numerator * x2 * x2 a = numerator / denominator # 十分な精度になったら処理を抜ける if (abs(a) <= 0.00000000001): return y else: return y + myLog(x2, numerator, denominator, a) for i in range(1, 21): x = i / 5.0 # 標準の対数関数 d1 = math.log(x) # 自作の対数関数 x2 = (x - 1) / (x + 1) d2 = 2 * myLog(x2, x2, 1.0, x2) # 標準関数との差異 print "%5.2f : %13.10f - %13.10f = %13.10f" % (x, d1, d2, d1 - d2)
Z:\>python 0506.py 0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
Ruby
# 自作の対数関数 def myLog(x2, numerator, denominator, y) denominator = denominator + 2 numerator = numerator * x2 * x2 a = numerator / denominator # 十分な精度になったら処理を抜ける if (a.abs <= 0.00000000001) y else y + myLog(x2, numerator, denominator, a) end end (1..20).each do |i| x = i / 5.0 # 標準の対数関数 d1 = Math.log(x) # 自作の対数関数 x2 = (x - 1) / (x + 1) d2 = 2 * myLog(x2, x2, 1.0, x2) # 標準関数との差異 printf("%5.2f : %13.10f - %13.10f = %13.10f\n", x, d1, d2, d1 - d2) end
Z:\>ruby 0506.rb 0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
Groovy
Pascal
Program Pas0506(arg); {$MODE delphi} uses SysUtils, Math; // 自作の対数関数 function myLog(x2:Double; numerator:Double; denominator:Double; y:Double):Double; var a :Double; begin denominator := denominator + 2; numerator := numerator * x2 * x2; a := numerator / denominator; // 十分な精度になったら処理を抜ける if (Abs(a) <= 0.00000000001) then result := y else result := y + myLog(x2, numerator, denominator, a); end; var i: Integer; x: Double; x2: Double; d1: Double; d2: Double; begin for i := 1 to 20 do begin x := i / 5.0; // 標準の対数関数 d1 := Ln(x); // 自作の対数関数 x2 := (x - 1) / (x + 1); d2 := 2 * myLog(x2, x2, 1.0, x2); // 標準関数との差異 writeln(format('%5.2f : %13.10f - %13.10f = %13.10f', [x, d1, d2, d1 - d2])); end; end.
Z:\>fpc Pas0506.pp -v0 Free Pascal Compiler version 2.6.2 [2013/02/12] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Z:\>Pas0506 0.20 : -1.6094379124 - -1.6094379124 = 0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = 0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = 0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = 0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
Ada
VB.NET
Module VB0506 Public Sub Main() For i As Integer = 1 To 20 Dim x As Double = i / 5.0 '標準の対数関数 Dim d1 As Double = Math.Log(x) '自作の対数関数 Dim x2 As Double = (x - 1) / (x + 1) Dim d2 As Double = 2 * myLog(x2, x2, 1.0, x2) '標準関数との差異 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 myLog(ByVal x2 As Double, ByVal numerator As Double, ByVal denominator As Double, ByVal y As Double) As Double denominator = denominator + 2 numerator = numerator * x2 * x2 Dim a As Double = numerator / denominator '十分な精度になったら処理を抜ける If (Math.Abs(a) <= 0.00000000001) Then Return y Else Return y + myLog(x2, numerator, denominator, a) End If End Function End Module
Z:\>vbc -nologo VB0506.vb Z:\>VB0506 0.20 : -1.6094379124 - -1.6094379124 = 0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = 0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = 0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = 0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
C#
using System; public class CS0506 { public static void Main() { for (int i = 1; i <= 20; i++) { double x = i / 5.0; // 標準の対数関数 double d1 = Math.Log(x); // 自作の対数関数 double x2 = (x - 1) / (x + 1); double d2 = 2 * myLog(x2, x2, 1.0, x2); // 標準関数との差異 Console.WriteLine(string.Format("{0,5:F2} : {1,13:F10} - {2,13:F10} = {3,13:F10}", x, d1, d2, d1 - d2)); } } // 自作の対数関数 private static double myLog(double x2, double numerator, double denominator, double y) { denominator = denominator + 2; numerator = numerator * x2 * x2; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (Math.Abs(a) <= 0.00000000001) return y; else return y + myLog(x2, numerator, denominator, a); } }
Z:\>csc -nologo CS0506.cs Z:\>CS0506 0.20 : -1.6094379124 - -1.6094379124 = 0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = 0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = 0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = 0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
Java
public class Java0506 { public static void main(String []args) { for (int i = 1; i <= 20; i++) { double x = i / 5.0; // 標準の対数関数 double d1 = Math.log(x); // 自作の対数関数 double x2 = (x - 1) / (x + 1); double d2 = 2 * myLog(x2, x2, 1.0, x2); // 標準関数との差異 System.out.println(String.format("%5.2f : %13.10f - %13.10f = %13.10f", x, d1, d2, d1 - d2)); } } // 自作の対数関数 private static double myLog(double x2, double numerator, double denominator, double y) { denominator = denominator + 2; numerator = numerator * x2 * x2; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (Math.abs(a) <= 0.00000000001) return y; else return y + myLog(x2, numerator, denominator, a); } }
Z:\>javac Java0506.java Z:\>java Java0506 0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
C++
#include <iostream> #include <iomanip> #include <math> using namespace std; double myLog(double x2, double numerator, double denominator, double y); int main() { for (int i = 1; i <= 20; i++) { double x = i / 5.0; // 標準の対数関数 double d1 = log(x); // 自作の対数関数 double x2 = (x - 1) / (x + 1); double d2 = 2 * myLog(x2, x2, 1.0, x2); // 標準関数との差異 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 myLog(double x2, double numerator, double denominator, double y) { denominator = denominator + 2; numerator = numerator * x2 * x2; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (fabs(a) <= 0.00000000001) return y; else return y + myLog(x2, numerator, denominator, a); }
Z:\>bcc32 CP0506.cpp Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland CP0506.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Z:\>CP0506 0.20: -1.6094379124 - -1.6094379124 = -0.0000000000 0.40: -0.9162907319 - -0.9162907319 = -0.0000000000 0.60: -0.5108256238 - -0.5108256238 = -0.0000000000 0.80: -0.2231435513 - -0.2231435513 = -0.0000000000 1.00: 0.0000000000 - 0.0000000000 = 0.0000000000 1.20: 0.1823215568 - 0.1823215568 = 0.0000000000 1.40: 0.3364722366 - 0.3364722366 = 0.0000000000 1.60: 0.4700036292 - 0.4700036292 = 0.0000000000 1.80: 0.5877866649 - 0.5877866649 = 0.0000000000 2.00: 0.6931471806 - 0.6931471805 = 0.0000000000 2.20: 0.7884573604 - 0.7884573603 = 0.0000000000 2.40: 0.8754687374 - 0.8754687373 = 0.0000000000 2.60: 0.9555114450 - 0.9555114450 = 0.0000000000 2.80: 1.0296194172 - 1.0296194172 = 0.0000000000 3.00: 1.0986122887 - 1.0986122887 = 0.0000000000 3.20: 1.1631508098 - 1.1631508098 = 0.0000000000 3.40: 1.2237754316 - 1.2237754316 = 0.0000000000 3.60: 1.2809338455 - 1.2809338454 = 0.0000000000 3.80: 1.3350010667 - 1.3350010667 = 0.0000000000 4.00: 1.3862943611 - 1.3862943611 = 0.0000000000
Objective-C
#import <Foundation/Foundation.h> double myLog(double x2, double numerator, double denominator, double y); int main() { int i; for (i = 1; i <= 20; i++) { double x = i / 5.0; // 標準の対数関数 double d1 = log(x); // 自作の対数関数 double x2 = (x - 1) / (x + 1); double d2 = 2 * myLog(x2, x2, 1.0, x2); // 標準関数との差異 printf("%+04.2f : %+013.10f - %+013.10f = %+13.10f\n", x, d1, d2, d1 - d2); } return 0; } // 自作の対数関数 double myLog(double x2, double numerator, double denominator, double y) { denominator = denominator + 2; numerator = numerator * x2 * x2; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (fabs(a) <= 0.00000000001) return y; else return y + myLog(x2, 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 +0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 +0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 +0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 +0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 +1.00 : +0.0000000000 - +0.0000000000 = +0.0000000000 +1.20 : +0.1823215568 - +0.1823215568 = +0.0000000000 +1.40 : +0.3364722366 - +0.3364722366 = +0.0000000000 +1.60 : +0.4700036292 - +0.4700036292 = +0.0000000000 +1.80 : +0.5877866649 - +0.5877866649 = +0.0000000000 +2.00 : +0.6931471806 - +0.6931471805 = +0.0000000000 +2.20 : +0.7884573604 - +0.7884573603 = +0.0000000000 +2.40 : +0.8754687374 - +0.8754687373 = +0.0000000000 +2.60 : +0.9555114450 - +0.9555114450 = +0.0000000000 +2.80 : +1.0296194172 - +1.0296194172 = +0.0000000000 +3.00 : +1.0986122887 - +1.0986122887 = +0.0000000000 +3.20 : +1.1631508098 - +1.1631508098 = +0.0000000000 +3.40 : +1.2237754316 - +1.2237754316 = +0.0000000000 +3.60 : +1.2809338455 - +1.2809338454 = +0.0000000000 +3.80 : +1.3350010667 - +1.3350010667 = +0.0000000000 +4.00 : +1.3862943611 - +1.3862943611 = +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.
級数展開(テイラー展開)で log x を求める
// 自作の対数関数 def myLog(x2:Double, numerator:Double, denominator:Double, y:Double):Double = { val denom = denominator + 2 val num = numerator * x2 * x2 val a = num / denom // 十分な精度になったら処理を抜ける if (Math.abs(a) <= 0.00000000001) y else y + myLog(x2, num, denom, a) }
(1 to 20). map(_ / 5.0). foreach { x => // 標準の対数関数 val d1 = Math.log(x) // 自作の対数関数 val x2 = (x - 1) / (x + 1) val d2 = 2 * myLog(x2, x2, 1.0, x2) // 標準関数との差異 System.out.println("%5.2f : %13.10f - %13.10f = %13.10f".format(x, d1, d2, d1 - d2)) }
0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 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;;
級数展開(テイラー展開)で log x を求める
// 自作の対数関数 let rec myLog (x2:double) (numerator:double) (denominator:double) (y:double):double = let denom = denominator + 2.0 let num = numerator * x2 * x2 let a = num / denom // 十分な精度になったら処理を抜ける if abs(a) <= 0.00000000001 then y else y + (myLog x2 num denom a)
[1..20] |> List.map (fun n -> (double n) / 5.0) |> List.iter (fun x -> // 標準の対数関数 let d1 = System.Math.Log(x) // 自作の対数関数 let x2 = (x - 1.0) / (x + 1.0) let d2 = 2.0 * (myLog x2 x2 1.0 x2) // 標準関数との差異 printfn "%5.2f : %13.10f - %13.10f = %13.10f" x d1 d2 (d1 - d2) )
0.20 : -1.6094379124 - -1.6094379124 = 0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = 0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = 0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = 0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 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
級数展開(テイラー展開)で log x を求める
;自作の対数関数 (defn myLog [x numerator denominator y] (def denom (+ denominator 2.0)) (def nume (* numerator (* x2 x2))) (def a (/ nume denom)) ;十分な精度になったら処理を抜ける (if (<= (. Math abs a) 0.00000000001) y (+ y (myLog x nume denom a))))
(doseq [x (map #(/ % 5.0) (range 1 21))] (do ;標準の対数関数 (def d1 (. Math log x)) ;自作の対数関数 (def x2 (/ (- x 1.0) (+ x 1.0))) ;標準関数との差異 (def d2 (* 2.0 (myLog x2 x2 1.0 x2))) (println (format "%5.2f : %13.10f - %13.10f = %13.10f" x d1 d2 (- d1 d2)))))
0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 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.
級数展開(テイラー展開)で log x を求める
-- 自作の対数関数 myLog::Double->Double->Double->Double->Double; myLog x2 numerator denominator y = let denom = denominator + 2.0 num = numerator * x2 * x2 a = num / denom in -- 十分な精度になったら処理を抜ける if abs(a) <= 0.00000000001 then y else y + (myLog x2 num denom a)
import Text.Printf import Control.Monad forM_ ( map (\n -> (fromIntegral n) / 5.0) $ [1..20::Int] ) $ \x -> do -- 標準の対数関数 let d1 = log(x) -- 自作の対数関数 let x2 = (x - 1.0) / (x + 1.0) let d2 = 2.0 * (myLog x2 x2 1.0 x2) -- 標準関数との差異 printf "%5.2f : %13.10f - %13.10f = %13.10f\n" x d1 d2 (d1- d2)
0.20 : -1.6094379124 - -1.6094379124 = -0.0000000000 0.40 : -0.9162907319 - -0.9162907319 = -0.0000000000 0.60 : -0.5108256238 - -0.5108256238 = -0.0000000000 0.80 : -0.2231435513 - -0.2231435513 = -0.0000000000 1.00 : 0.0000000000 - 0.0000000000 = 0.0000000000 1.20 : 0.1823215568 - 0.1823215568 = 0.0000000000 1.40 : 0.3364722366 - 0.3364722366 = 0.0000000000 1.60 : 0.4700036292 - 0.4700036292 = 0.0000000000 1.80 : 0.5877866649 - 0.5877866649 = 0.0000000000 2.00 : 0.6931471806 - 0.6931471805 = 0.0000000000 2.20 : 0.7884573604 - 0.7884573603 = 0.0000000000 2.40 : 0.8754687374 - 0.8754687373 = 0.0000000000 2.60 : 0.9555114450 - 0.9555114450 = 0.0000000000 2.80 : 1.0296194172 - 1.0296194172 = 0.0000000000 3.00 : 1.0986122887 - 1.0986122887 = 0.0000000000 3.20 : 1.1631508098 - 1.1631508098 = 0.0000000000 3.40 : 1.2237754316 - 1.2237754316 = 0.0000000000 3.60 : 1.2809338455 - 1.2809338454 = 0.0000000000 3.80 : 1.3350010667 - 1.3350010667 = 0.0000000000 4.00 : 1.3862943611 - 1.3862943611 = 0.0000000000
終了
Prelude> :quit Leaving GHCi.