source: trunk/Include/Classes/ActiveBasic/Math/Math.ab@ 464

Last change on this file since 464 was 426, checked in by イグトランス (egtra), 17 years ago

StreamReaderの実装開始。
Stringの+演算子で右辺がNullのときに起こるアクセス違反を回避。

File size: 3.3 KB
Line 
1'Classes/ActiveBasic/Math/Math.ab
2
3Namespace ActiveBasic
4Namespace Math
5
6'xの符号だけをyのものにした値を返す。
7'引数 x 元となる絶対値
8'引数 y 元となる符号
9Function CopySign(x As Double, y As Double) As Double
10 SetQWord(VarPtr(CopySign), (GetQWord(VarPtr(x)) And &h7fffffffffffffff) Or (GetQWord(VarPtr(y)) And &h8000000000000000))
11End Function
12
13Function CopySign(x As Single, y As Single) As Single
14 SetDWord(VarPtr(CopySign), (GetDWord(VarPtr(x)) And &h7fffffff) Or (GetDWord(VarPtr(y)) And &h80000000))
15End Function
16
17Function Hypot(x As Double, y As Double) As Double
18 If x = 0 Then
19 Hypot = Abs(y)
20 Else If y = 0 Then
21 Hypot = Abs(x)
22 Else
23 Dim ax = Abs(x)
24 Dim ay = Abs(y)
25 If ay > ax Then
26 Dim t = x / y
27 Hypot = ay * Sqr(1 + t * t)
28 Else
29 Dim t = y / x
30 Hypot = ax * Sqr(1 + t * t)
31 End If
32 End If
33End Function
34
35Function Log1p(x As Double) As Double
36 If x < -1 Or IsNaN(x) Then
37 Log1p = ActiveBasic.Math.Detail.GetNaN()
38 ElseIf x = 0 Then
39 x = 0
40 ElseIf IsInf(x) Then
41 Log1p = x
42 Else
43 Log1p = ActiveBasic.Math.Detail.Log1p(x)
44 End If
45End Function
46
47Function IsNaN(ByVal x As Double) As Boolean
48 Dim p = VarPtr(x) As *DWord
49 IsNaN = False
50 If (p[1] And &H7FF00000) = &H7FF00000 Then
51 If (p[0] <> 0) Or ((p[1] And &HFFFFF) <> 0) Then
52 IsNaN = True
53 End If
54 End If
55End Function
56
57Function IsInf(x As Double) As Boolean
58 Dim p = VarPtr(x) As *DWord
59 p[1] And= &h7fffffff
60 Dim inf = ActiveBasic.Math.Detail.GetInf(False)
61 IsInf = (memcmp(p As *Byte, VarPtr(inf), SizeOf (Double)) = 0)
62End Function
63
64Function IsFinite(x As Double) As Boolean
65 Dim p = VarPtr(x) As *DWord
66 p[1] And= &H7FF00000
67 IsFinite = ( p[1] And &H7FF00000 ) = &H7FF00000
68End Function
69
70Namespace Detail
71
72Function SetSign(x As Double, isNegative As Long) As Double
73#ifdef _WIN64
74 SetQWord(AddressOf(CopySign), (GetQWord(VarPtr(x)) And &h7fffffffffffffff) Or (isNegative << 63))
75#else
76 SetDWord(AddressOf(CopySign), GetDWord(VarPtr(x)))
77 SetDWord(AddressOf(CopySign) + SizeOf (DWord), GetQWord(VarPtr(x) + SizeOf (DWord)) And &h7fffffff Or (isNegative << 31))
78#endif
79End Function
80
81#ifdef _WIN64
82
83Function GetNaN() As Double
84 SetQWord(VarPtr(GetNaN) As *QWord, &H7FF8000000000000)
85End Function
86
87Function GetInf(sign As Boolean) As Double
88 Dim s = 0 As QWord
89 If sign Then s = 1 << 63
90 SetQWord(VarPtr(GetInf) As *QWord, &h7FF0000000000000 Or s)
91End Function
92
93#else
94
95Function GetNaN() As Double
96 Dim p = VarPtr(GetNaN) As *DWord
97 p[0] = 0
98 p[1] = &H7FF80000
99End Function
100
101Function GetInf(sign As Boolean) As Double
102 Dim s = 0 As DWord
103 If sign Then s = (1 As DWord) << 31
104 Dim p = VarPtr(GetInf) As *DWord
105 p[0] = 0
106 p[1] = &h7FF00000 Or s
107End Function
108
109#endif
110
111Function Log1p(x As Double) As Double
112 Dim s = 0 As Double
113 Dim i = 7 As Long
114 While i >= 1
115 Dim t = (i * x) As Double
116 s = t * (2 * i + 1 + s) / (2 + t)
117 i--
118 Wend
119 Return x / (1 + s)
120End Function
121
122Function _Support_tan(x As Double, ByRef k As Long) As Double
123 If x>=0 Then
124 k=Fix(x/(_System_PI/2)+0.5)
125 Else
126 k=Fix(x/(_System_PI/2)-0.5)
127 End If
128
129 x=(x-(CDbl(3217)/CDbl(2048))*k)+4.4544551033807686783083602485579e-6*k
130
131 Dim x2 = x * x
132 Dim t = 0 As Double
133
134 Dim i As Long
135 For i=19 To 3 Step -2
136 t=x2/(i-t)
137 Next
138
139 _Support_tan=x/(1-t)
140End Function
141
142End Namespace 'Detail
143End Namespace 'Math
144End Namespace 'ActiveBasic
Note: See TracBrowser for help on using the repository browser.