さまざまな言語で数値計算
Only Do What Only You Can Do
余弦関数(級数展開)
級数展開(テイラー展開)で $ \cos x $ を求めます.
VBScript
Option Explicit Const PI = 3.14159265359 Dim degree For degree = 0 To 360 Step 15 If (degree Mod 30 = 0 Or degree Mod 45 = 0) Then Dim radian: radian = degree * PI / 180.0 '自作の余弦関数 Dim d1: d1 = myCos(radian, 1, False, 1.0, 1.0, 1.0) '標準の余弦関数 Dim d2: d2 = Cos(radian) '標準関数との差異 WScript.StdOut.Write Right(Space(3) & degree, 3) & " : " 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 End If Next '自作の余弦関数 Private Function myCos(ByVal x, ByVal n, ByVal nega, 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 (a <= 0.00000000001) Then myCos = y Else If Not nega Then a = -a myCos = y + myCos(x, n + 1, Not nega, numerator, denominator, a) End If End Function
Z:\>cscript //nologo 0502.vbs 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = -0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = -0.0000000000 90 : 0.0000000000 - -0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : 0.0000000000 - 0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = -0.0000000000 315 : 0.7071067812 - 0.7071067812 = -0.0000000000 330 : 0.8660254038 - 0.8660254038 = -0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
JScript
for (var degree = 0; degree <= 360; degree += 15) { if (degree % 30 == 0 || degree % 45 == 0) { var radian = degree * Math.PI / 180.0; // 自作の余弦関数 var d1 = myCos(radian, 1, false, 1.0, 1.0, 1.0); // 標準の余弦関数 var d2 = Math.cos(radian); // 標準関数との差異 WScript.StdOut.Write((" " + degree ).slice( -3) + " : "); 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 myCos(x, n, nega, numerator, denominator, y) { var m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; var a = numerator / denominator; // 十分な精度になったら処理を抜ける if (a <= 0.00000000001) return y; else return y + myCos(x, ++n, !nega, numerator, denominator, nega ? a : -a); }
Z:\>cscript //nologo 0502.js 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = -0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = -0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : 0.0000000000 - -0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = -0.0000000000 315 : 0.7071067812 - 0.7071067812 = -0.0000000000 330 : 0.8660254038 - 0.8660254038 = -0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
PowerShell
# 自作の余弦関数 function myCos($x, $n, $nega, $numerator, $denominator, $y) { $m = 2 * $n $denominator = $denominator * $m * ($m - 1) $numerator = $numerator * $x * $x $a = $numerator / $denominator # 十分な精度になったら処理を抜ける if ($a -le 0.00000000001) { $y } else { if (-not $nega) { $a = -$a} $y + (myCos $x ($n + 1) (-not $nega) $numerator $denominator $a) } } foreach ($i in 0..24) { $degree = $i * 15 if ($degree % 30 -eq 0 -or $degree % 45 -eq 0) { $radian = $degree * [Math]::PI / 180.0 # 自作の余弦関数 $d1 = myCos $radian 1 $false 1.0 1.0 1.0 # 標準の余弦関数 $d2 = [Math]::Cos($radian) # 標準関数との差異 Write-Host ([string]::format("{0,3:D} : {1,13:F10} - {2,13:F10} = {3,13:F10}", $degree, $d1, $d2, $d1 - $d2)) } }
Z:\>powershell -file 0502.ps1 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = 0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = 0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = 0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = 0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = 0.0000000000 270 : 0.0000000000 - 0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = 0.0000000000 315 : 0.7071067812 - 0.7071067812 = 0.0000000000 330 : 0.8660254038 - 0.8660254038 = 0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
Perl
use Math::Trig qw'deg2rad tan'; # 自作の余弦関数 sub myCos { my ($x, $n, $nega, $numerator, $denominator, $y) = @_; $m = 2 * $n; $denominator = $denominator * $m * ($m - 1); $numerator = $numerator * $x * $x; $a = $numerator / $denominator; # 十分な精度になったら処理を抜ける if ($a <= 0.00000000001) { $y; } else { $y + myCos($x, ++$n, !$nega, $numerator, $denominator, $nega ? $a : -$a); } } for $i (0..24) { $degree = $i * 15; if ($degree % 30 == 0 || $degree % 45 == 0) { $radian = deg2rad($degree); # 自作の余弦関数 $d1 = myCos($radian, 1, 0, 1.0, 1.0, 1.0); # 標準の余弦関数 $d2 = cos($radian); # 標準関数との差異 printf("%3d : %13.10f - %13.10f = %13.10f\n", $degree, $d1, $d2, $d1 - $d2); } }
Z:\>perl 0502.pl 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = -0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = -0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : 0.0000000000 - -0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = -0.0000000000 315 : 0.7071067812 - 0.7071067812 = -0.0000000000 330 : 0.8660254038 - 0.8660254038 = -0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
PHP
<?php # 自作の余弦関数 function myCos($x, $n, $nega, $numerator, $denominator, $y) { $m = 2 * $n; $denominator = $denominator * $m * ($m - 1); $numerator = $numerator * $x * $x; $a = $numerator / $denominator; # 十分な精度になったら処理を抜ける if ($a <= 0.00000000001) return $y; else return $y + myCos($x, ++$n, !$nega, $numerator, $denominator, $nega ? $a : -$a); } foreach (range(0, 24) as $i) { $degree = $i * 15; if ($degree % 30 == 0 || $degree % 45 == 0) { $radian = deg2rad($degree); # 自作の余弦関数 $d1 = myCos($radian, 1, false, 1.0, 1.0, 1.0); # 標準の余弦関数 $d2 = cos($radian); # 標準関数との差異 printf("%3d : %13.10f - %13.10f = %13.10f\n", $degree, $d1, $d2, $d1 - $d2); } } ?>
Z:\>php 0502.php 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = -0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = -0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : 0.0000000000 - -0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = -0.0000000000 315 : 0.7071067812 - 0.7071067812 = -0.0000000000 330 : 0.8660254038 - 0.8660254038 = -0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
Python
# coding: Shift_JIS import math # 自作の余弦関数 def myCos(x, n, nega, numerator, denominator, y): m = 2 * n denominator = denominator * m * (m - 1) numerator = numerator * x * x a = numerator / denominator # 十分な精度になったら処理を抜ける if (a <= 0.00000000001): return y else: return y + myCos(x, n + 1, not nega, numerator, denominator, a if nega else -a) for i in range(0, 25): degree = i * 15 if degree % 30 == 0 or degree % 45 == 0: radian = math.radians(degree) # 自作の余弦関数 d1 = myCos(radian, 1, False, 1.0, 1.0, 1.0) # 標準の余弦関数 d2 = math.cos(radian) # 標準関数との差異 print "%3d : %13.10f - %13.10f = %13.10f" % (degree, d1, d2, d1 - d2)
Z:\>python 0502.py 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = -0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = -0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : 0.0000000000 - -0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = -0.0000000000 315 : 0.7071067812 - 0.7071067812 = -0.0000000000 330 : 0.8660254038 - 0.8660254038 = -0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
Ruby
# 自作の余弦関数 def myCos(x, n, nega, numerator, denominator, y) m = 2 * n denominator = denominator * m * (m - 1) numerator = numerator * x * x a = numerator / denominator # 十分な精度になったら処理を抜ける if (a <= 0.00000000001) y else y + myCos(x, n + 1, !nega, numerator, denominator, nega ? a : -a) end end (0..24).each do |i| degree = i * 15 if degree % 30 == 0 || degree % 45 == 0 radian = degree * Math::PI / 180 # 自作の余弦関数 d1 = myCos(radian, 1, false, 1.0, 1.0, 1.0) # 標準の余弦関数 d2 = Math.cos(radian) # 標準関数との差異 printf("%3d : %13.10f - %13.10f = %13.10f\n", degree, d1, d2, d1 - d2) end end
Z:\>ruby 0502.rb 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = -0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = -0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : 0.0000000000 - -0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = -0.0000000000 315 : 0.7071067812 - 0.7071067812 = -0.0000000000 330 : 0.8660254038 - 0.8660254038 = -0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
Groovy
Pascal
Program Pas0502(arg); {$MODE delphi} uses SysUtils, Math; // 自作の余弦関数 function myCos(x:Double; n:Integer; nega:Boolean; 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 (a <= 0.00000000001) then result := y else begin if not nega then a := -a; result := y + myCos(x, n + 1, not nega, numerator, denominator, a); end; end; var i: Integer; degree: Integer; radian: Double; d1: Double; d2: Double; begin for i := 0 to 24 do begin degree := i * 15; if (degree mod 30 = 0) or (degree mod 45 = 0) then begin radian := DegToRad(degree); // 自作の余弦関数 d1 := myCos(radian, 1, false, 1.0, 1.0, 1.0); // 標準の余弦関数 d2 := Cos(radian); // 標準関数との差異 writeln(format('%3d : %13.10f - %13.10f = %13.10f', [degree, d1, d2, d1 - d2])); end; end; end.
Z:\>fpc Pas0502.pp -v0 Free Pascal Compiler version 2.6.2 [2013/02/12] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Z:\>Pas0502 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = 0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = 0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = 0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = 0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = 0.0000000000 270 : 0.0000000000 - 0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = 0.0000000000 315 : 0.7071067812 - 0.7071067812 = 0.0000000000 330 : 0.8660254038 - 0.8660254038 = 0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
Ada
VB.NET
Module VB0502 Public Sub Main() For degree As Integer = 0 To 360 Step 15 If (degree Mod 30 = 0 OrElse degree Mod 45 = 0) Then Dim radian As Double = degree * Math.PI / 180.0 '自作の余弦関数 Dim d1 As Double = myCos(radian, 1, False, 1.0, 1.0, 1.0) '標準の余弦関数 Dim d2 As Double = Math.Cos(radian) '標準関数との差異 Console.WriteLine(String.Format("{0,3:D} : {1,13:F10} - {2,13:F10} = {3,13:F10}", degree, d1, d2, d1 - d2)) End If Next End Sub '自作の余弦関数 Private Function myCos(ByVal x As Double, ByVal n As Integer, ByVal nega As Boolean, 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 (a <= 0.00000000001) Then Return y Else Return y + myCos(x, n + 1, Not nega, numerator, denominator, If(nega, a, -a)) End If End Function End Module
Z:\>vbc -nologo VB0502.vb Z:\>VB0502 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = 0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = 0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = 0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = 0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = 0.0000000000 270 : 0.0000000000 - 0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = 0.0000000000 315 : 0.7071067812 - 0.7071067812 = 0.0000000000 330 : 0.8660254038 - 0.8660254038 = 0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
C#
using System; public class CS0502 { public static void Main() { for (int degree = 0; degree <= 360; degree += 15) { if (degree % 30 == 0 || degree % 45 == 0) { double radian = degree * Math.PI / 180.0; // 自作の余弦関数 double d1 = myCos(radian, 1, false, 1.0, 1.0, 1.0); // 標準の余弦関数 double d2 = Math.Cos(radian); // 標準関数との差異 Console.WriteLine(string.Format("{0,3:D} : {1,13:F10} - {2,13:F10} = {3,13:F10}", degree, d1, d2, d1 - d2)); } } } // 自作の余弦関数 private static double myCos(double x, int n, bool nega, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (a <= 0.00000000001) return y; else return y + myCos(x, ++n, !nega, numerator, denominator, nega ? a : -a); } }
Z:\>csc -nologo CS0502.cs Z:\>CS0502 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = 0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = 0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = 0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = 0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = 0.0000000000 270 : 0.0000000000 - 0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = 0.0000000000 315 : 0.7071067812 - 0.7071067812 = 0.0000000000 330 : 0.8660254038 - 0.8660254038 = 0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
Java
public class Java0502 { public static void main(String []args) { for (int degree = 0; degree <= 360; degree += 15) { if (degree % 30 == 0 || degree % 45 == 0) { double radian = Math.toRadians(degree); // 自作の余弦関数 double d1 = myCos(radian, 1, false, 1.0, 1.0, 1.0); // 標準の余弦関数 double d2 = Math.cos(radian); // 標準関数との差異 System.out.println(String.format("%3d : %13.10f - %13.10f = %13.10f", degree, d1, d2, d1 - d2)); } } } // 自作の余弦関数 private static double myCos(double x, int n, boolean nega, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (a <= 0.00000000001) return y; else return y + myCos(x, ++n, !nega, numerator, denominator, nega ? a : -a); } }
Z:\>javac Java0502.java Z:\>java Java0502 0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = -0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = -0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : 0.0000000000 - -0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = -0.0000000000 315 : 0.7071067812 - 0.7071067812 = -0.0000000000 330 : 0.8660254038 - 0.8660254038 = -0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
C++
#include <iostream> #include <iomanip> #include <math> using namespace std; double myCos(double x, int n, bool nega, double numerator, double denominator, double y); int main() { for (int degree = 0; degree <= 360; degree += 15) { if (degree % 30 == 0 || degree % 45 == 0) { double radian = degree * M_PI / 180.0; // 自作の余弦関数 double d1 = myCos(radian, 1, false, 1.0, 1.0, 1.0); // 標準の余弦関数 double d2 = cos(radian); // 標準関数との差異 cout << setw(3) << degree << ":"; 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 myCos(double x, int n, bool nega, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (a <= 0.00000000001) return y; else return y + myCos(x, ++n, !nega, numerator, denominator, nega ? a : -a); }
Z:\>bcc32 CP0502.cpp Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland CP0502.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Z:\>CP0502 0: 1.0000000000 - 1.0000000000 = 0.0000000000 30: 0.8660254038 - 0.8660254038 = -0.0000000000 45: 0.7071067812 - 0.7071067812 = 0.0000000000 60: 0.5000000000 - 0.5000000000 = -0.0000000000 90: 0.0000000000 - 0.0000000000 = 0.0000000000 120: -0.5000000000 - -0.5000000000 = -0.0000000000 135: -0.7071067812 - -0.7071067812 = 0.0000000000 150: -0.8660254038 - -0.8660254038 = 0.0000000000 180: -1.0000000000 - -1.0000000000 = -0.0000000000 210: -0.8660254038 - -0.8660254038 = 0.0000000000 225: -0.7071067812 - -0.7071067812 = 0.0000000000 240: -0.5000000000 - -0.5000000000 = -0.0000000000 270: 0.0000000000 - -0.0000000000 = 0.0000000000 300: 0.5000000000 - 0.5000000000 = -0.0000000000 315: 0.7071067812 - 0.7071067812 = -0.0000000000 330: 0.8660254038 - 0.8660254038 = -0.0000000000 360: 1.0000000000 - 1.0000000000 = 0.0000000000
Objective-C
#import <Foundation/Foundation.h> double myCos(double x, int n, bool nega, double numerator, double denominator, double y); int main() { int degree; for (degree = 0; degree <= 360; degree += 15) { if (degree % 30 == 0 || degree % 45 == 0) { double radian = degree * M_PI / 180.0; // 自作の余弦関数 double d1 = myCos(radian, 1, false, 1.0, 1.0, 1.0); // 標準の余弦関数 double d2 = cos(radian); // 標準関数との差異 printf("%03d : %+13.10f - %+13.10f = %+13.10f\n", degree, d1, d2, d1 - d2); } } return 0; } // 自作の余弦関数 double myCos(double x, int n, bool nega, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (a <= 0.00000000001) return y; else return y + myCos(x, ++n, !nega, numerator, denominator, nega ? a : -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 000 : +1.0000000000 - +1.0000000000 = +0.0000000000 030 : +0.8660254038 - +0.8660254038 = -0.0000000000 045 : +0.7071067812 - +0.7071067812 = +0.0000000000 060 : +0.5000000000 - +0.5000000000 = -0.0000000000 090 : +0.0000000000 - +0.0000000000 = +0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = +0.0000000000 150 : -0.8660254038 - -0.8660254038 = +0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = +0.0000000000 225 : -0.7071067812 - -0.7071067812 = +0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : +0.0000000000 - -0.0000000000 = +0.0000000000 300 : +0.5000000000 - +0.5000000000 = -0.0000000000 315 : +0.7071067812 - +0.7071067812 = -0.0000000000 330 : +0.8660254038 - +0.8660254038 = -0.0000000000 360 : +1.0000000000 - +1.0000000000 = +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.
級数展開(テイラー展開)で cos x を求める
// 自作の余弦関数 def myCos(x:Double, n:Int, nega:Boolean, 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 (a <= 0.00000000001) y else y + myCos(x, n + 1, !nega, num, denom, if (nega) a else -a) }
(0 to 24). map(_ * 15). filter(n => (n % 30 == 0) || (n % 45 == 0)). foreach { degree => val radian = Math.toRadians(degree) // 自作の余弦関数 val d1 = myCos(radian, 1, false, 1.0, 1.0, 1.0) // 標準の余弦関数 val d2 = Math.cos(radian) // 標準関数との差異 System.out.println("%3d : %13.10f - %13.10f = %13.10f".format(degree, d1, d2, d1 - d2)); }
0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = -0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = -0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : 0.0000000000 - -0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = -0.0000000000 315 : 0.7071067812 - 0.7071067812 = -0.0000000000 330 : 0.8660254038 - 0.8660254038 = -0.0000000000 360 : 1.0000000000 - 1.0000000000 = 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;;
級数展開(テイラー展開)で cos x を求める
// 自作の余弦関数 let rec myCos (x:double) (n:int) (nega:bool) (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 a <= 0.00000000001 then y else y + (myCos x (n + 1) (not nega) num denom (if nega then a else -a) )
[0..24] |> List.map ((*) 15) |> List.filter (fun n -> (n % 30 = 0) || (n % 45 = 0)) |> List.iter (fun degree -> let radian = (double degree) * System.Math.PI / 180.0 // 自作の余弦関数 let d1 = (myCos radian 1 false 1.0 1.0 1.0) // 標準の余弦関数 let d2 = System.Math.Cos(radian) // 標準関数との差異 printfn "%3d : %13.10f - %13.10f = %13.10f" degree d1 d2 (d1 - d2) )
0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = 0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = 0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = 0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = 0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = 0.0000000000 270 : 0.0000000000 - 0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = 0.0000000000 315 : 0.7071067812 - 0.7071067812 = 0.0000000000 330 : 0.8660254038 - 0.8660254038 = 0.0000000000 360 : 1.0000000000 - 1.0000000000 = 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
級数展開(テイラー展開)で cos x を求める
;自作の余弦関数 (defn myCos [x n nega numerator denominator y] (def m (* 2 n)) (def denom (* denominator (* m (- m 1)))) (def nume (* numerator (* x x))) (def a (/ nume denom)) ;十分な精度になったら処理を抜ける (if (<= a 0.00000000001) y (+ y (myCos x (+ n 1) (not nega) nume denom (if nega a (- a))))))
(doseq [degree (filter #(or (= (mod % 30) 0) (= (mod % 45) 0)) (map #(* % 15) (range 0 25)))] (do (def radian (/ (* degree (. Math PI)) 180.0)) ;自作の余弦関数 (def d1 (myCos radian 1 false 1.0 1.0 1.0)) ;標準の余弦関数 (def d2 (. Math cos radian)) ;標準関数との差異 (println (format "%3d : %13.10f - %13.10f = %13.10f" degree d1 d2 (- d1 d2)))))
0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = -0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = -0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : 0.0000000000 - -0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = -0.0000000000 315 : 0.7071067812 - 0.7071067812 = -0.0000000000 330 : 0.8660254038 - 0.8660254038 = -0.0000000000 360 : 1.0000000000 - 1.0000000000 = 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.
級数展開(テイラー展開)で cos x を求める
-- 自作の余弦関数 myCos::Double->Int->Bool->Double->Double->Double->Double myCos x n nega numerator denominator y = let m = 2 * n denom = denominator * (fromIntegral m) * (fromIntegral (m - 1)) num = numerator * x * x a = num / denom in -- 十分な精度になったら処理を抜ける if a <= 0.00000000001 then y else y + (myCos x (n + 1) (not nega) num denom (if nega then a else -a))
import Text.Printf import Control.Monad forM_ ( filter (\n -> (mod n 30 == 0 || mod n 45 == 0)) $ map (* 15) $ [0..24::Int] ) $ \degree -> do let radian = (fromIntegral degree) * pi / 180.0 -- 自作の余弦関数 let d1 = (myCos radian 1 False 1.0 1.0 1.0) -- 標準の余弦関数 let d2 = cos(radian) -- 標準関数との差異 printf "%3d : %13.10f - %13.10f = %13.10f\n" degree d1 d2 (d1- d2)
0 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.8660254038 - 0.8660254038 = -0.0000000000 45 : 0.7071067812 - 0.7071067812 = 0.0000000000 60 : 0.5000000000 - 0.5000000000 = -0.0000000000 90 : 0.0000000000 - 0.0000000000 = 0.0000000000 120 : -0.5000000000 - -0.5000000000 = -0.0000000000 135 : -0.7071067812 - -0.7071067812 = 0.0000000000 150 : -0.8660254038 - -0.8660254038 = 0.0000000000 180 : -1.0000000000 - -1.0000000000 = -0.0000000000 210 : -0.8660254038 - -0.8660254038 = 0.0000000000 225 : -0.7071067812 - -0.7071067812 = 0.0000000000 240 : -0.5000000000 - -0.5000000000 = -0.0000000000 270 : 0.0000000000 - -0.0000000000 = 0.0000000000 300 : 0.5000000000 - 0.5000000000 = -0.0000000000 315 : 0.7071067812 - 0.7071067812 = -0.0000000000 330 : 0.8660254038 - 0.8660254038 = -0.0000000000 360 : 1.0000000000 - 1.0000000000 = 0.0000000000
終了
Prelude> :quit Leaving GHCi.