[269] | 1 | 'Classes/ActiveBasic/Math/Math.ab
|
---|
| 2 |
|
---|
| 3 | Namespace ActiveBasic
|
---|
| 4 | Namespace Math
|
---|
| 5 |
|
---|
| 6 | 'xの符号だけをyのものにした値を返す。
|
---|
| 7 | '引数 x 元となる絶対値
|
---|
| 8 | '引数 y 元となる符号
|
---|
| 9 | Function CopySign(x As Double, y As Double) As Double
|
---|
| 10 | SetQWord(VarPtr(CopySign), (GetQWord(VarPtr(x)) And &h7fffffffffffffff) Or (GetQWord(VarPtr(y)) And &h8000000000000000))
|
---|
| 11 | End Function
|
---|
| 12 |
|
---|
| 13 | Function CopySign(x As Single, y As Single) As Single
|
---|
| 14 | SetDWord(VarPtr(CopySign), (GetDWord(VarPtr(x)) And &h7fffffff) Or (GetDWord(VarPtr(y)) And &h80000000))
|
---|
| 15 | End Function
|
---|
| 16 |
|
---|
[285] | 17 | Function 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
|
---|
| 33 | End Function
|
---|
| 34 |
|
---|
[269] | 35 | Function 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
|
---|
| 45 | End Function
|
---|
| 46 |
|
---|
| 47 | Function IsNaN(ByVal x As Double) As Boolean
|
---|
[426] | 48 | Dim p = VarPtr(x) As *DWord
|
---|
[269] | 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
|
---|
| 55 | End Function
|
---|
| 56 |
|
---|
| 57 | Function IsInf(x As Double) As Boolean
|
---|
[285] | 58 | Dim p = VarPtr(x) As *DWord
|
---|
[269] | 59 | p[1] And= &h7fffffff
|
---|
[285] | 60 | Dim inf = ActiveBasic.Math.Detail.GetInf(False)
|
---|
| 61 | IsInf = (memcmp(p As *Byte, VarPtr(inf), SizeOf (Double)) = 0)
|
---|
[269] | 62 | End Function
|
---|
| 63 |
|
---|
| 64 | Function IsFinite(x As Double) As Boolean
|
---|
[285] | 65 | Dim p = VarPtr(x) As *DWord
|
---|
[269] | 66 | p[1] And= &H7FF00000
|
---|
[289] | 67 | IsFinite = ( p[1] And &H7FF00000 ) = &H7FF00000
|
---|
[269] | 68 | End Function
|
---|
| 69 |
|
---|
| 70 | Namespace Detail
|
---|
| 71 |
|
---|
| 72 | Function 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
|
---|
| 79 | End Function
|
---|
| 80 |
|
---|
| 81 | #ifdef _WIN64
|
---|
| 82 |
|
---|
| 83 | Function GetNaN() As Double
|
---|
[276] | 84 | SetQWord(VarPtr(GetNaN) As *QWord, &H7FF8000000000000)
|
---|
[269] | 85 | End Function
|
---|
| 86 |
|
---|
| 87 | Function GetInf(sign As Boolean) As Double
|
---|
| 88 | Dim s = 0 As QWord
|
---|
| 89 | If sign Then s = 1 << 63
|
---|
[276] | 90 | SetQWord(VarPtr(GetInf) As *QWord, &h7FF0000000000000 Or s)
|
---|
[269] | 91 | End Function
|
---|
| 92 |
|
---|
| 93 | #else
|
---|
| 94 |
|
---|
| 95 | Function GetNaN() As Double
|
---|
[285] | 96 | Dim p = VarPtr(GetNaN) As *DWord
|
---|
[269] | 97 | p[0] = 0
|
---|
| 98 | p[1] = &H7FF80000
|
---|
| 99 | End Function
|
---|
| 100 |
|
---|
| 101 | Function GetInf(sign As Boolean) As Double
|
---|
| 102 | Dim s = 0 As DWord
|
---|
| 103 | If sign Then s = (1 As DWord) << 31
|
---|
[426] | 104 | Dim p = VarPtr(GetInf) As *DWord
|
---|
[269] | 105 | p[0] = 0
|
---|
| 106 | p[1] = &h7FF00000 Or s
|
---|
| 107 | End Function
|
---|
| 108 |
|
---|
| 109 | #endif
|
---|
| 110 |
|
---|
| 111 | Function 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
|
---|
[285] | 116 | s = t * (2 * i + 1 + s) / (2 + t)
|
---|
[269] | 117 | i--
|
---|
| 118 | Wend
|
---|
| 119 | Return x / (1 + s)
|
---|
| 120 | End Function
|
---|
| 121 |
|
---|
| 122 | Function _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 |
|
---|
[285] | 131 | Dim x2 = x * x
|
---|
| 132 | Dim t = 0 As Double
|
---|
[269] | 133 |
|
---|
[285] | 134 | Dim i As Long
|
---|
[269] | 135 | For i=19 To 3 Step -2
|
---|
| 136 | t=x2/(i-t)
|
---|
| 137 | Next
|
---|
| 138 |
|
---|
| 139 | _Support_tan=x/(1-t)
|
---|
| 140 | End Function
|
---|
| 141 |
|
---|
| 142 | End Namespace 'Detail
|
---|
| 143 | End Namespace 'Math
|
---|
| 144 | End Namespace 'ActiveBasic
|
---|