さまざまな言語で数値計算
Only Do What Only You Can Do
双曲線余弦関数 (級数展開)
級数展開(テイラー展開)で $ \cosh x $ を求めます.
VBScript
Option Explicit Dim i For i = 0 To 20 Dim x: x = i - 10 '自作の双曲線余弦関数 Dim d1: d1 = myCosh(x, 1, 1.0, 1.0, 1.0) '標準の双曲線余弦関数はない Dim d2: d2 = (Exp(x) + Exp(-x)) / 2 '標準関数との差異 WScript.StdOut.Write Right(Space(3) & x, 3) & " : " WScript.StdOut.Write Right(Space(17) & FormatNumber(d1, 10, -1, 0, 0), 17) & " - " WScript.StdOut.Write Right(Space(17) & FormatNumber(d2, 10, -1, 0, 0), 17) & " = " WScript.StdOut.Write Right(Space(13) & FormatNumber(d1 - d2, 10, -1, 0, 0), 13) & vbNewLine Next '自作の双曲線余弦関数 Private Function myCosh(ByVal x, ByVal n, ByVal numerator, ByVal denominator, ByVal y) Dim m: m = 2 * n denominator = denominator * m * (m - 1) numerator = numerator * x * x Dim a: a = numerator / denominator '十分な精度になったら処理を抜ける If (Abs(a) <= 0.00000000001) Then myCosh = y Else myCosh = y + myCosh(x, n + 1, numerator, denominator, a) End If End Function
Z:\>cscript //nologo 0509.vbs -10 : 11013,2329201033 - 11013,2329201033 = -0,0000000000 -9 : 4051,5420254926 - 4051,5420254926 = -0,0000000000 -8 : 1490,4791612522 - 1490,4791612522 = -0,0000000000 -7 : 548,3170351552 - 548,3170351552 = -0,0000000000 -6 : 201,7156361225 - 201,7156361225 = -0,0000000000 -5 : 74,2099485248 - 74,2099485248 = -0,0000000000 -4 : 27,3082328360 - 27,3082328360 = -0,0000000000 -3 : 10,0676619958 - 10,0676619958 = -0,0000000000 -2 : 3,7621956911 - 3,7621956911 = -0,0000000000 -1 : 1,5430806348 - 1,5430806348 = -0,0000000000 0 : 1,0000000000 - 1,0000000000 = 0,0000000000 1 : 1,5430806348 - 1,5430806348 = -0,0000000000 2 : 3,7621956911 - 3,7621956911 = -0,0000000000 3 : 10,0676619958 - 10,0676619958 = -0,0000000000 4 : 27,3082328360 - 27,3082328360 = -0,0000000000 5 : 74,2099485248 - 74,2099485248 = -0,0000000000 6 : 201,7156361225 - 201,7156361225 = -0,0000000000 7 : 548,3170351552 - 548,3170351552 = -0,0000000000 8 : 1490,4791612522 - 1490,4791612522 = -0,0000000000 9 : 4051,5420254926 - 4051,5420254926 = -0,0000000000 10 : 11013,2329201033 - 11013,2329201033 = -0,0000000000
JScript
for (var x = -10; x <= 10; x++) { // 自作の双曲線余弦関数 var d1 = myCosh(x, 1, 1.0, 1.0, 1.0); // 標準の双曲線余弦関数はない var d2 = (Math.exp(x) + Math.exp(-x)) / 2; // 標準関数との差異 WScript.StdOut.Write((" " + x ).slice( -3) + " : "); WScript.StdOut.Write((" " + d1.toFixed(10) ).slice(-17) + " - "); WScript.StdOut.Write((" " + d2.toFixed(10) ).slice(-17) + " = "); WScript.StdOut.Write((" " + (d1 - d2).toFixed(10)).slice(-13) + "\n" ); } // 自作の双曲線余弦関数 function myCosh(x, n, numerator, denominator, y) { var m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; var a = numerator / denominator; // 十分な精度になったら処理を抜ける if (Math.abs(a) <= 0.00000000001) return y; else return y + myCosh(x, ++n, numerator, denominator, a); }
Z:\>cscript //nologo 0509.js -10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 -7 : 548.3170351552 - 548.3170351552 = -0.0000000000 -6 : 201.7156361225 - 201.7156361225 = -0.0000000000 -5 : 74.2099485248 - 74.2099485248 = -0.0000000000 -4 : 27.3082328360 - 27.3082328360 = -0.0000000000 -3 : 10.0676619958 - 10.0676619958 = -0.0000000000 -2 : 3.7621956911 - 3.7621956911 = -0.0000000000 -1 : 1.5430806348 - 1.5430806348 = -0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = -0.0000000000 2 : 3.7621956911 - 3.7621956911 = -0.0000000000 3 : 10.0676619958 - 10.0676619958 = -0.0000000000 4 : 27.3082328360 - 27.3082328360 = -0.0000000000 5 : 74.2099485248 - 74.2099485248 = -0.0000000000 6 : 201.7156361225 - 201.7156361225 = -0.0000000000 7 : 548.3170351552 - 548.3170351552 = -0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000
PowerShell
# 自作の双曲線余弦関数 function myCosh($x, $n, $numerator, $denominator, $y) { $m = 2 * $n $denominator = $denominator * $m * ($m - 1) $numerator = $numerator * $x * $x $a = $numerator / $denominator # 十分な精度になったら処理を抜ける if ([Math]::Abs($a) -le 0.00000000001) { $y } else { $y + (myCosh $x ($n + 1) $numerator $denominator $a) } } foreach ($i in 0..20) { $x = $i - 10 # 自作の双曲線余弦関数 $d1 = myCosh $x 1 1.0 1.0 1.0 # 標準の双曲線余弦関数 $d2 = [Math]::Cosh($x) # 標準関数との差異 Write-Host ([string]::format("{0,3:D} : {1,17:F10} - {2,17:F10} = {3,13:F10}", $x, $d1, $d2, $d1 - $d2)) }
Z:\>powershell -file 0509.ps1 -10 : 11013.2329201033 - 11013.2329201033 = 0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = 0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = 0.0000000000 -7 : 548.3170351552 - 548.3170351552 = 0.0000000000 -6 : 201.7156361225 - 201.7156361225 = 0.0000000000 -5 : 74.2099485248 - 74.2099485248 = 0.0000000000 -4 : 27.3082328360 - 27.3082328360 = 0.0000000000 -3 : 10.0676619958 - 10.0676619958 = 0.0000000000 -2 : 3.7621956911 - 3.7621956911 = 0.0000000000 -1 : 1.5430806348 - 1.5430806348 = 0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = 0.0000000000 2 : 3.7621956911 - 3.7621956911 = 0.0000000000 3 : 10.0676619958 - 10.0676619958 = 0.0000000000 4 : 27.3082328360 - 27.3082328360 = 0.0000000000 5 : 74.2099485248 - 74.2099485248 = 0.0000000000 6 : 201.7156361225 - 201.7156361225 = 0.0000000000 7 : 548.3170351552 - 548.3170351552 = 0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = 0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = 0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = 0.0000000000
Perl
use Math::Trig; # 自作の双曲線余弦関数 sub myCosh { my ($x, $n, $numerator, $denominator, $y) = @_; $m = 2 * $n; $denominator = $denominator * $m * ($m - 1); $numerator = $numerator * $x * $x; $a = $numerator / $denominator; # 十分な精度になったら処理を抜ける if (abs($a) <= 0.00000000001) { $y; } else { $y + myCosh($x, ++$n, $numerator, $denominator, $a); } } for $i (0..20) { $x = $i - 10; # 自作の双曲線余弦関数 $d1 = myCosh($x, 1, 1.0, 1.0, 1.0); # 標準の双曲線余弦関数 $d2 = cosh($x); # 標準関数との差異 printf("%3d : %17.10f - %17.10f = %13.10f\n", $x, $d1, $d2, $d1 - $d2); }
Z:\>perl 0509.pl -10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 -7 : 548.3170351552 - 548.3170351552 = -0.0000000000 -6 : 201.7156361225 - 201.7156361225 = -0.0000000000 -5 : 74.2099485248 - 74.2099485248 = -0.0000000000 -4 : 27.3082328360 - 27.3082328360 = -0.0000000000 -3 : 10.0676619958 - 10.0676619958 = -0.0000000000 -2 : 3.7621956911 - 3.7621956911 = -0.0000000000 -1 : 1.5430806348 - 1.5430806348 = -0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = -0.0000000000 2 : 3.7621956911 - 3.7621956911 = -0.0000000000 3 : 10.0676619958 - 10.0676619958 = -0.0000000000 4 : 27.3082328360 - 27.3082328360 = -0.0000000000 5 : 74.2099485248 - 74.2099485248 = -0.0000000000 6 : 201.7156361225 - 201.7156361225 = -0.0000000000 7 : 548.3170351552 - 548.3170351552 = -0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000
PHP
<?php # 自作の双曲線余弦関数 function myCosh($x, $n, $numerator, $denominator, $y) { $m = 2 * $n; $denominator = $denominator * $m * ($m - 1); $numerator = $numerator * $x * $x; $a = $numerator / $denominator; # 十分な精度になったら処理を抜ける if (abs($a) <= 0.00000000001) return $y; else return $y + myCosh($x, ++$n, $numerator, $denominator, $a); } foreach (range(0, 20) as $i) { $x = $i - 10; # 自作の双曲線余弦関数 $d1 = myCosh($x, 1, 1.0, 1.0, 1.0); # 標準の双曲線余弦関数 $d2 = cosh($x); # 標準関数との差異 printf("%3d : %17.10f - %17.10f = %13.10f\n", $x, $d1, $d2, $d1 - $d2); } ?>
Z:\>php 0509.php -10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 -7 : 548.3170351552 - 548.3170351552 = -0.0000000000 -6 : 201.7156361225 - 201.7156361225 = -0.0000000000 -5 : 74.2099485248 - 74.2099485248 = -0.0000000000 -4 : 27.3082328360 - 27.3082328360 = -0.0000000000 -3 : 10.0676619958 - 10.0676619958 = -0.0000000000 -2 : 3.7621956911 - 3.7621956911 = -0.0000000000 -1 : 1.5430806348 - 1.5430806348 = -0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = -0.0000000000 2 : 3.7621956911 - 3.7621956911 = -0.0000000000 3 : 10.0676619958 - 10.0676619958 = -0.0000000000 4 : 27.3082328360 - 27.3082328360 = -0.0000000000 5 : 74.2099485248 - 74.2099485248 = -0.0000000000 6 : 201.7156361225 - 201.7156361225 = -0.0000000000 7 : 548.3170351552 - 548.3170351552 = -0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000
Python
# coding: Shift_JIS import math # 自作の双曲線余弦関数 def myCosh(x, n, numerator, denominator, y): m = 2 * n denominator = denominator * m * (m - 1) numerator = numerator * x * x a = numerator / denominator # 十分な精度になったら処理を抜ける if (abs(a) <= 0.00000000001): return y else: return y + myCosh(x, n + 1, numerator, denominator, a) for i in range(0, 21): x = i - 10 # 自作の双曲線余弦関数 d1 = myCosh(x, 1, 1.0, 1.0, 1.0) # 標準の双曲線余弦関数 d2 = math.cosh(x) # 標準関数との差異 print "%3d : %17.10f - %17.10f = %13.10f" % (x, d1, d2, d1 - d2)
Z:\>python 0509.py -10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 -7 : 548.3170351552 - 548.3170351552 = -0.0000000000 -6 : 201.7156361225 - 201.7156361225 = -0.0000000000 -5 : 74.2099485248 - 74.2099485248 = -0.0000000000 -4 : 27.3082328360 - 27.3082328360 = -0.0000000000 -3 : 10.0676619958 - 10.0676619958 = -0.0000000000 -2 : 3.7621956911 - 3.7621956911 = -0.0000000000 -1 : 1.5430806348 - 1.5430806348 = -0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = -0.0000000000 2 : 3.7621956911 - 3.7621956911 = -0.0000000000 3 : 10.0676619958 - 10.0676619958 = -0.0000000000 4 : 27.3082328360 - 27.3082328360 = -0.0000000000 5 : 74.2099485248 - 74.2099485248 = -0.0000000000 6 : 201.7156361225 - 201.7156361225 = -0.0000000000 7 : 548.3170351552 - 548.3170351552 = -0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000
Ruby
# 自作の双曲線余弦関数 def myCosh(x, n, numerator, denominator, y) m = 2 * n denominator = denominator * m * (m - 1) numerator = numerator * x * x a = numerator / denominator # 十分な精度になったら処理を抜ける if (a.abs <= 0.00000000001) y else y + myCosh(x, n + 1, numerator, denominator, a) end end (0..20).each do |i| x = i - 10 # 自作の双曲線余弦関数 d1 = myCosh(x, 1, 1.0, 1.0, 1.0) # 標準の双曲線余弦関数 d2 = Math.cosh(x) # 標準関数との差異 printf("%3d : %17.10f - %17.10f = %13.10f\n", x, d1, d2, d1 - d2) end
Z:\>ruby 0509.rb -10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 -7 : 548.3170351552 - 548.3170351552 = -0.0000000000 -6 : 201.7156361225 - 201.7156361225 = -0.0000000000 -5 : 74.2099485248 - 74.2099485248 = -0.0000000000 -4 : 27.3082328360 - 27.3082328360 = -0.0000000000 -3 : 10.0676619958 - 10.0676619958 = -0.0000000000 -2 : 3.7621956911 - 3.7621956911 = -0.0000000000 -1 : 1.5430806348 - 1.5430806348 = -0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = -0.0000000000 2 : 3.7621956911 - 3.7621956911 = -0.0000000000 3 : 10.0676619958 - 10.0676619958 = -0.0000000000 4 : 27.3082328360 - 27.3082328360 = -0.0000000000 5 : 74.2099485248 - 74.2099485248 = -0.0000000000 6 : 201.7156361225 - 201.7156361225 = -0.0000000000 7 : 548.3170351552 - 548.3170351552 = -0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000
Groovy
Pascal
Program Pas0509(arg); {$MODE delphi} uses SysUtils, Math; // 自作の双曲線余弦関数 function myCosh(x:Double; n:Integer; numerator:Double; denominator:Double; y:Double):Double; var m: Integer; a: Double; begin m := 2 * n; denominator := denominator * m * (m - 1); numerator := numerator * x * x; a := numerator / denominator; // 十分な精度になったら処理を抜ける if (Abs(a) <= 0.00000000001) then result := y else result := y + myCosh(x, n + 1, numerator, denominator, a); end; var i: Integer; x: Integer; d1: Double; d2: Double; begin for i := 0 to 20 do begin x := i - 10; // 自作の双曲線余弦関数 d1 := myCosh(x, 1, 1.0, 1.0, 1.0); // 標準の双曲線余弦関数 d2 := Cosh(x); // 標準関数との差異 writeln(format('%3d : %17.10f - %17.10f = %13.10f', [x, d1, d2, d1 - d2])); end; end.
Z:\>fpc Pas0509.pp -v0 Free Pascal Compiler version 2.6.2 [2013/02/12] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Z:\>Pas0509 -10 : 11013.2329201033 - 11013.2329201033 = 0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = 0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = 0.0000000000 -7 : 548.3170351552 - 548.3170351552 = 0.0000000000 -6 : 201.7156361225 - 201.7156361225 = 0.0000000000 -5 : 74.2099485248 - 74.2099485248 = 0.0000000000 -4 : 27.3082328360 - 27.3082328360 = 0.0000000000 -3 : 10.0676619958 - 10.0676619958 = 0.0000000000 -2 : 3.7621956911 - 3.7621956911 = 0.0000000000 -1 : 1.5430806348 - 1.5430806348 = 0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = 0.0000000000 2 : 3.7621956911 - 3.7621956911 = 0.0000000000 3 : 10.0676619958 - 10.0676619958 = 0.0000000000 4 : 27.3082328360 - 27.3082328360 = 0.0000000000 5 : 74.2099485248 - 74.2099485248 = 0.0000000000 6 : 201.7156361225 - 201.7156361225 = 0.0000000000 7 : 548.3170351552 - 548.3170351552 = 0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = 0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = 0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = 0.0000000000
Ada
VB.NET
Module VB0509 Public Sub Main() For i As Integer = 0 To 20 Dim x As Integer = i - 10 '自作の双曲線余弦関数 Dim d1 As Double = myCosh(x, 1, 1.0, 1.0, 1.0) '標準の双曲線余弦関数 Dim d2 As Double = Math.Cosh(x) '標準関数との差異 Console.WriteLine(String.Format("{0,3:D} : {1,17:F10} - {2,17:F10} = {3,13:F10}", x, d1, d2, d1 - d2)) Next End Sub '自作の双曲線余弦関数 Private Function myCosh(ByVal x As Double, ByVal n As Integer, ByVal numerator As Double, ByVal denominator As Double, ByVal y As Double) As Double Dim m As Integer = 2 * n denominator = denominator * m * (m - 1) numerator = numerator * x * x Dim a As Double = numerator / denominator '十分な精度になったら処理を抜ける If (Math.Abs(a) <= 0.00000000001) Then Return y Else Return y + myCosh(x, n + 1, numerator, denominator, a) End If End Function End Module
Z:\>vbc -nologo VB0509.vb Z:\>VB0509 -10 : 11013.2329201033 - 11013.2329201033 = 0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = 0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = 0.0000000000 -7 : 548.3170351552 - 548.3170351552 = 0.0000000000 -6 : 201.7156361225 - 201.7156361225 = 0.0000000000 -5 : 74.2099485248 - 74.2099485248 = 0.0000000000 -4 : 27.3082328360 - 27.3082328360 = 0.0000000000 -3 : 10.0676619958 - 10.0676619958 = 0.0000000000 -2 : 3.7621956911 - 3.7621956911 = 0.0000000000 -1 : 1.5430806348 - 1.5430806348 = 0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = 0.0000000000 2 : 3.7621956911 - 3.7621956911 = 0.0000000000 3 : 10.0676619958 - 10.0676619958 = 0.0000000000 4 : 27.3082328360 - 27.3082328360 = 0.0000000000 5 : 74.2099485248 - 74.2099485248 = 0.0000000000 6 : 201.7156361225 - 201.7156361225 = 0.0000000000 7 : 548.3170351552 - 548.3170351552 = 0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = 0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = 0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = 0.0000000000
C#
using System; public class CS0509 { public static void Main() { for (int x = -10; x <= 10; x++) { // 自作の双曲線余弦関数 double d1 = myCosh(x, 1, 1.0, 1.0, 1.0); // 標準の双曲線余弦関数 double d2 = Math.Cosh(x); // 標準関数との差異 Console.WriteLine(string.Format("{0,3:D} : {1,17:F10} - {2,17:F10} = {3,13:F10}", x, d1, d2, d1 - d2)); } } // 自作の双曲線余弦関数 private static double myCosh(double x, int n, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (Math.Abs(a) <= 0.00000000001) return y; else return y + myCosh(x, ++n, numerator, denominator, a); } }
Z:\>csc -nologo CS0509.cs Z:\>CS0509 -10 : 11013.2329201033 - 11013.2329201033 = 0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = 0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = 0.0000000000 -7 : 548.3170351552 - 548.3170351552 = 0.0000000000 -6 : 201.7156361225 - 201.7156361225 = 0.0000000000 -5 : 74.2099485248 - 74.2099485248 = 0.0000000000 -4 : 27.3082328360 - 27.3082328360 = 0.0000000000 -3 : 10.0676619958 - 10.0676619958 = 0.0000000000 -2 : 3.7621956911 - 3.7621956911 = 0.0000000000 -1 : 1.5430806348 - 1.5430806348 = 0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = 0.0000000000 2 : 3.7621956911 - 3.7621956911 = 0.0000000000 3 : 10.0676619958 - 10.0676619958 = 0.0000000000 4 : 27.3082328360 - 27.3082328360 = 0.0000000000 5 : 74.2099485248 - 74.2099485248 = 0.0000000000 6 : 201.7156361225 - 201.7156361225 = 0.0000000000 7 : 548.3170351552 - 548.3170351552 = 0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = 0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = 0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = 0.0000000000
Java
public class Java0509 { public static void main(String []args) { for (int x = -10; x <= 10; x++) { // 自作の双曲線余弦関数 double d1 = myCosh(x, 1, 1.0, 1.0, 1.0); // 標準の双曲線余弦関数 double d2 = Math.cosh(x); // 標準関数との差異 System.out.println(String.format("%3d : %17.10f - %17.10f = %13.10f", x, d1, d2, d1 - d2)); } } // 自作の双曲線余弦関数 private static double myCosh(double x, int n, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (Math.abs(a) <= 0.00000000001) return y; else return y + myCosh(x, ++n, numerator, denominator, a); } }
Z:\>javac Java0509.java Z:\>java Java0509 -10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 -7 : 548.3170351552 - 548.3170351552 = -0.0000000000 -6 : 201.7156361225 - 201.7156361225 = -0.0000000000 -5 : 74.2099485248 - 74.2099485248 = -0.0000000000 -4 : 27.3082328360 - 27.3082328360 = -0.0000000000 -3 : 10.0676619958 - 10.0676619958 = -0.0000000000 -2 : 3.7621956911 - 3.7621956911 = -0.0000000000 -1 : 1.5430806348 - 1.5430806348 = -0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = -0.0000000000 2 : 3.7621956911 - 3.7621956911 = -0.0000000000 3 : 10.0676619958 - 10.0676619958 = -0.0000000000 4 : 27.3082328360 - 27.3082328360 = -0.0000000000 5 : 74.2099485248 - 74.2099485248 = -0.0000000000 6 : 201.7156361225 - 201.7156361225 = -0.0000000000 7 : 548.3170351552 - 548.3170351552 = -0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000
C++
#include <iostream> #include <iomanip> #include <math> using namespace std; double myCosh(double x, int n, double numerator, double denominator, double y); int main() { for (int x = -10; x <= 10; x++) { // 自作の双曲線余弦関数 double d1 = myCosh(x, 1, 1.0, 1.0, 1.0); // 標準の双曲線余弦関数 double d2 = cosh(x); // 標準関数との差異 cout << setw(3) << x << ":"; cout << setw(17) << fixed << setprecision(10) << d1 << " - "; cout << setw(17) << fixed << setprecision(10) << d2 << " = "; cout << setw(17) << fixed << setprecision(10) << d1 - d2 << endl; } return 0; } // 自作の双曲線余弦関数 double myCosh(double x, int n, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (fabs(a) <= 0.00000000001) return y; else return y + myCosh(x, ++n, numerator, denominator, a); }
Z:\>bcc32 CP0509.cpp Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland CP0509.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Z:\>CP0509 -10: 11013.2329201033 - 11013.2329201033 = -0.0000000000 -9: 4051.5420254926 - 4051.5420254926 = -0.0000000000 -8: 1490.4791612522 - 1490.4791612522 = -0.0000000000 -7: 548.3170351552 - 548.3170351552 = -0.0000000000 -6: 201.7156361225 - 201.7156361225 = -0.0000000000 -5: 74.2099485248 - 74.2099485248 = -0.0000000000 -4: 27.3082328360 - 27.3082328360 = -0.0000000000 -3: 10.0676619958 - 10.0676619958 = -0.0000000000 -2: 3.7621956911 - 3.7621956911 = -0.0000000000 -1: 1.5430806348 - 1.5430806348 = -0.0000000000 0: 1.0000000000 - 1.0000000000 = 0.0000000000 1: 1.5430806348 - 1.5430806348 = -0.0000000000 2: 3.7621956911 - 3.7621956911 = -0.0000000000 3: 10.0676619958 - 10.0676619958 = -0.0000000000 4: 27.3082328360 - 27.3082328360 = -0.0000000000 5: 74.2099485248 - 74.2099485248 = -0.0000000000 6: 201.7156361225 - 201.7156361225 = -0.0000000000 7: 548.3170351552 - 548.3170351552 = -0.0000000000 8: 1490.4791612522 - 1490.4791612522 = -0.0000000000 9: 4051.5420254926 - 4051.5420254926 = -0.0000000000 10: 11013.2329201033 - 11013.2329201033 = -0.0000000000
Objective-C
#import <Foundation/Foundation.h> double myCosh(double x, int n, double numerator, double denominator, double y); int main() { int x; for (x = -10; x <= 10; x++) { // 自作の双曲線余弦関数 double d1 = myCosh(x, 1, 1.0, 1.0, 1.0); // 標準の双曲線余弦関数 double d2 = cosh(x); // 標準関数との差異 printf("%+03d : %+017.10f - %+017.10f = %+13.10f\n", x, d1, d2, d1 - d2); } return 0; } // 自作の双曲線余弦関数 double myCosh(double x, int n, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (fabs(a) <= 0.00000000001) return y; else return y + myCosh(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 -10 : +11013.2329201033 - +11013.2329201033 = -0.0000000000 -09 : +04051.5420254926 - +04051.5420254926 = -0.0000000000 -08 : +01490.4791612522 - +01490.4791612522 = -0.0000000000 -07 : +00548.3170351552 - +00548.3170351552 = -0.0000000000 -06 : +00201.7156361225 - +00201.7156361225 = -0.0000000000 -05 : +00074.2099485248 - +00074.2099485248 = -0.0000000000 -04 : +00027.3082328360 - +00027.3082328360 = -0.0000000000 -03 : +00010.0676619958 - +00010.0676619958 = -0.0000000000 -02 : +00003.7621956911 - +00003.7621956911 = -0.0000000000 -01 : +00001.5430806348 - +00001.5430806348 = -0.0000000000 +00 : +00001.0000000000 - +00001.0000000000 = +0.0000000000 +01 : +00001.5430806348 - +00001.5430806348 = -0.0000000000 +02 : +00003.7621956911 - +00003.7621956911 = -0.0000000000 +03 : +00010.0676619958 - +00010.0676619958 = -0.0000000000 +04 : +00027.3082328360 - +00027.3082328360 = -0.0000000000 +05 : +00074.2099485248 - +00074.2099485248 = -0.0000000000 +06 : +00201.7156361225 - +00201.7156361225 = -0.0000000000 +07 : +00548.3170351552 - +00548.3170351552 = -0.0000000000 +08 : +01490.4791612522 - +01490.4791612522 = -0.0000000000 +09 : +04051.5420254926 - +04051.5420254926 = -0.0000000000 +10 : +11013.2329201033 - +11013.2329201033 = -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.
級数展開(テイラー展開)で cosh x を求める
// 自作の双曲線余弦関数 def myCosh(x:Double, n:Int, numerator:Double, denominator:Double, y:Double):Double = { val m = 2 * n val denom = denominator * m * (m - 1) val num = numerator * x * x val a = num / denom // 十分な精度になったら処理を抜ける if (Math.abs(a) <= 0.00000000001) y else y + myCosh(x, n + 1, num, denom, a) }
(0 to 20). map(_ - 10). foreach { x => // 自作の双曲線余弦関数 val d1 = myCosh(x, 1, 1.0, 1.0, 1.0) // 標準の双曲線余弦関数 val d2 = Math.cosh(x) // 標準関数との差異 System.out.println("%3d : %17.10f - %17.10f = %13.10f".format(x, d1, d2, d1 - d2)); }
-10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 -7 : 548.3170351552 - 548.3170351552 = -0.0000000000 -6 : 201.7156361225 - 201.7156361225 = -0.0000000000 -5 : 74.2099485248 - 74.2099485248 = -0.0000000000 -4 : 27.3082328360 - 27.3082328360 = -0.0000000000 -3 : 10.0676619958 - 10.0676619958 = -0.0000000000 -2 : 3.7621956911 - 3.7621956911 = -0.0000000000 -1 : 1.5430806348 - 1.5430806348 = -0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = -0.0000000000 2 : 3.7621956911 - 3.7621956911 = -0.0000000000 3 : 10.0676619958 - 10.0676619958 = -0.0000000000 4 : 27.3082328360 - 27.3082328360 = -0.0000000000 5 : 74.2099485248 - 74.2099485248 = -0.0000000000 6 : 201.7156361225 - 201.7156361225 = -0.0000000000 7 : 548.3170351552 - 548.3170351552 = -0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = -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;;
級数展開(テイラー展開)で cosh x を求める
// 自作の双曲線余弦関数 let rec myCosh (x:double) (n:int) (numerator:double) (denominator:double) (y:double):double = let m = 2 * n let denom = denominator * (double m) * (double (m - 1)) let num = numerator * x * x let a = num / denom // 十分な精度になったら処理を抜ける if abs(a) <= 0.00000000001 then y else y + (myCosh x (n + 1) num denom a)
[0..20] |> List.map((-) 10) |> List.iter (fun i -> let x = (double i) // 自作の双曲線余弦関数 let d1 = (myCosh x 1 1.0 1.0 1.0) // 標準の双曲線余弦関数 let d2 = System.Math.Cosh(x) // 標準関数との差異 printfn "%3d : %17.10f - %17.10f = %13.10f" i d1 d2 (d1 - d2) )
10 : 11013.2329201033 - 11013.2329201033 = 0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = 0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = 0.0000000000 7 : 548.3170351552 - 548.3170351552 = 0.0000000000 6 : 201.7156361225 - 201.7156361225 = 0.0000000000 5 : 74.2099485248 - 74.2099485248 = 0.0000000000 4 : 27.3082328360 - 27.3082328360 = 0.0000000000 3 : 10.0676619958 - 10.0676619958 = 0.0000000000 2 : 3.7621956911 - 3.7621956911 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = 0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 -1 : 1.5430806348 - 1.5430806348 = 0.0000000000 -2 : 3.7621956911 - 3.7621956911 = 0.0000000000 -3 : 10.0676619958 - 10.0676619958 = 0.0000000000 -4 : 27.3082328360 - 27.3082328360 = 0.0000000000 -5 : 74.2099485248 - 74.2099485248 = 0.0000000000 -6 : 201.7156361225 - 201.7156361225 = 0.0000000000 -7 : 548.3170351552 - 548.3170351552 = 0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = 0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = 0.0000000000 -10 : 11013.2329201033 - 11013.2329201033 = 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
級数展開(テイラー展開)で cosh x を求める
;自作の双曲線余弦関数 (defn myCosh [x n numerator denominator y] (def m (* 2 n)) (def denom (* denominator (* m (- m 1)))) (def nume (* numerator (* x x))) (def a (/ nume denom)) ;十分な精度になったら処理を抜ける (if (<= (. Math abs a) 0.00000000001) y (+ y (myCosh x (+ n 1) nume denom a))))
(doseq [i (map #(- % 10) (range 0 21))] (do (def x (double i)) ;自作の双曲線余弦関数 (def d1 (myCosh x 1 1.0 1.0 1.0)) ;標準の双曲線余弦関数 (def d2 (. Math cosh x)) ;標準関数との差異 (println (format "%3d : %17.10f - %17.10f = %13.10f" i d1 d2 (- d1 d2)))))
-10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 -7 : 548.3170351552 - 548.3170351552 = -0.0000000000 -6 : 201.7156361225 - 201.7156361225 = -0.0000000000 -5 : 74.2099485248 - 74.2099485248 = -0.0000000000 -4 : 27.3082328360 - 27.3082328360 = -0.0000000000 -3 : 10.0676619958 - 10.0676619958 = -0.0000000000 -2 : 3.7621956911 - 3.7621956911 = -0.0000000000 -1 : 1.5430806348 - 1.5430806348 = -0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 1 : 1.5430806348 - 1.5430806348 = -0.0000000000 2 : 3.7621956911 - 3.7621956911 = -0.0000000000 3 : 10.0676619958 - 10.0676619958 = -0.0000000000 4 : 27.3082328360 - 27.3082328360 = -0.0000000000 5 : 74.2099485248 - 74.2099485248 = -0.0000000000 6 : 201.7156361225 - 201.7156361225 = -0.0000000000 7 : 548.3170351552 - 548.3170351552 = -0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 10 : 11013.2329201033 - 11013.2329201033 = -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.
級数展開(テイラー展開)で cosh x を求める
-- 自作の双曲線余弦関数 myCosh::Double->Int->Double->Double->Double->Double; myCosh x n numerator denominator y = let m = 2 * n denom = denominator * (fromIntegral m) * (fromIntegral (m - 1)) num = numerator * x * x a = num / denom in -- 十分な精度になったら処理を抜ける if abs(a) <= 0.00000000001 then y else y + (myCosh x (n + 1) num denom a)
import Text.Printf import Control.Monad forM_ ( map ((-) 10) $ [0..20::Int] ) $ \i -> do -- 自作の双曲線余弦関数 let x = (fromIntegral i) let d1 = (myCosh x 1 1.0 1.0 1.0) -- 標準の双曲線余弦関数 let d2 = cosh(x) -- 標準関数との差異 printf "%3d : %17.10f - %17.10f = %13.10f\n" i d1 d2 (d1- d2)
10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000 9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 7 : 548.3170351552 - 548.3170351552 = -0.0000000000 6 : 201.7156361225 - 201.7156361225 = -0.0000000000 5 : 74.2099485248 - 74.2099485248 = -0.0000000000 4 : 27.3082328360 - 27.3082328360 = -0.0000000000 3 : 10.0676619958 - 10.0676619958 = -0.0000000000 2 : 3.7621956911 - 3.7621956911 = -0.0000000000 1 : 1.5430806348 - 1.5430806348 = -0.0000000000 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 -1 : 1.5430806348 - 1.5430806348 = -0.0000000000 -2 : 3.7621956911 - 3.7621956911 = -0.0000000000 -3 : 10.0676619958 - 10.0676619958 = -0.0000000000 -4 : 27.3082328360 - 27.3082328360 = -0.0000000000 -5 : 74.2099485248 - 74.2099485248 = -0.0000000000 -6 : 201.7156361225 - 201.7156361225 = -0.0000000000 -7 : 548.3170351552 - 548.3170351552 = -0.0000000000 -8 : 1490.4791612522 - 1490.4791612522 = -0.0000000000 -9 : 4051.5420254926 - 4051.5420254926 = -0.0000000000 -10 : 11013.2329201033 - 11013.2329201033 = -0.0000000000
終了
Prelude> :quit Leaving GHCi.