- Timestamp:
- Aug 15, 2008, 1:00:15 AM (16 years ago)
- Location:
- trunk/ab5.0/ablib/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ab5.0/ablib/src/Classes/ActiveBasic/Math/Math.ab
r426 r589 3 3 Namespace ActiveBasic 4 4 Namespace Math 5 '---- 6 '浮動小数点数補助 7 Function ldexp(x As Double, n As Long) As Double 8 If x = 0 Then 9 ldexp = 0 10 Exit Function 11 End If 12 Dim pSrc = VarPtr(x) As *QWord 13 Dim pDest = VarPtr(ldexp) As *QWord 14 n += (pSrc[0] >> 52) As DWord And &h7FF 15 pDest[0] = n << 52 Or (pSrc[0] And &h800FFFFFFFFFFFFF) 16 End Function 17 18 Function frexp(x As Double, ByRef n As Long) As Double 19 If x = 0 Then 20 n = 0 21 frexp = 0 22 Exit Function 23 End If 24 25 Dim pSrc = VarPtr(x) As *QWord 26 Dim pDest = VarPtr(frexp) As *QWord 27 n = ((pSrc[0] >> 52) As DWord And &h7FF) - 1022 28 pDest[0] = (pSrc[0] And &h800FFFFFFFFFFFFF) Or &h3FE0000000000000 29 End Function 30 31 Function frexp(x As Single, ByRef n As Long) As Single 32 If x = 0 Then 33 n = 0 34 frexp = 0 35 Exit Function 36 End If 37 38 Dim pSrc As *DWord, pDest As *DWord 39 pSrc = VarPtr(x) As *DWord 40 pDest = VarPtr(frexp) As *DWord 41 n = ((pSrc[0] >> 23) And &hFF) - 126 42 pDest[0] = (pSrc[0] And &h807FFFFF) Or &h7E000000 43 End Function 44 45 '---- 46 '冪乗 47 Function pow(x As Double, n As Long) As Double 48 Dim abs_n As Long 49 Dim r = 1 As Double 50 51 abs_n=Abs(n) As Long 52 While abs_n<>0 53 If abs_n and 1 Then r *= x 54 x = x * x 55 abs_n >>= 1 ' abs_n \= 2 56 Wend 57 58 If n>=0 Then 59 pow=r 60 Else 61 pow=1/r 62 End If 63 End Function 64 65 Function pow(x As Double, y As Double) As Double 66 Dim yi = y As Long 67 If y = yi Then 68 pow = pow(x, yi) 69 ElseIf x>0 Then 70 pow = Exp(y * Log(x)) 71 Exit Function 72 ElseIf x<>0 or y<=0 Then 73 pow = Detail.GetNaN() 74 Else 75 pow = 0 76 End If 77 End Function 78 79 Function Sqrt(x As Double) As Double 80 If x > 0 Then 81 If IsInf(x) Then 82 Sqrt = x 83 Else 84 Sqrt = x 85 Dim i = (VarPtr(Sqrt) + 6) As *Word 86 Dim jj = GetWord(i) As Long 87 Dim j = jj >> 5 As Long 88 Dim k = (jj And &h0000001f) As Long 89 j = (j + 511) << 4 + k 90 SetWord(i, j) 91 Dim last As Double 92 Do 93 last = Sqrt 94 Sqrt = (x / Sqrt + Sqrt) * 0.5 95 Loop While Sqrt <> last 96 End If 97 ElseIf x < 0 Then 98 Sqrt = Detail.GetNaN() 99 Else 100 'x = 0 Or NaN 101 Sqrt = x 102 End If 103 End Function 5 104 6 105 'xの符号だけをyのものにした値を返す。 … … 15 114 End Function 16 115 116 '---- 117 '絶対値 118 Function Abs(n As Double) As Double 119 If n < 0 Then 120 Abs = -n 121 Else 122 Abs = n 123 End If 124 End Function 125 126 Function Abs(n As Single) As Single 127 If n < 0 Then 128 Abs = -n 129 Else 130 Abs = n 131 End If 132 End Function 133 134 Function Abs(n As Int64) As Int64 135 If n < 0 Then 136 Abs = -n 137 Else 138 Abs = n 139 End If 140 End Function 141 142 Function Abs(n As Long) As Long 143 If n < 0 Then 144 Abs = -n 145 Else 146 Abs = n 147 End If 148 End Function 149 150 Function Abs(n As Integer) As Integer 151 If n < 0 Then 152 Abs = -n 153 Else 154 Abs = n 155 End If 156 End Function 157 158 Function Abs(n As SByte) As SByte 159 If n < 0 Then 160 Abs = -n 161 Else 162 Abs = n 163 End If 164 End Function 165 166 '---- 167 '指数・対数 168 169 Function Exp(x As Double) As Double 170 If IsNaN(x) Then 171 Return x 172 Else If IsInf(x) Then 173 If 0 > x Then 174 Return 0 175 Else 176 Return x 177 End If 178 End If 179 Dim k As Long 180 If x >= 0 Then 181 k = Fix(x / Detail._System_LOG2 + 0.5) 182 Else 183 k = Fix(x / Detail._System_LOG2 - 0.5) 184 End If 185 186 x -= k * Detail._System_LOG2 187 188 Dim x2 = x * x 189 Dim w = x2 / 22 190 191 Dim i = 18 192 While i >= 6 193 w = x2 / (w + i) 194 i -= 4 195 Wend 196 197 Return ldexp((2 + w + x) / (2 + w - x), k) 198 End Function 199 200 Function Log1p(x As Double) As Double 201 If x < -1 Or IsNaN(x) Then 202 Log1p = Detail.GetNaN() 203 ElseIf x = 0 Then 204 x = 0 205 ElseIf IsInf(x) Then 206 Log1p = x 207 Else 208 Log1p = Detail.Log1p(x) 209 End If 210 End Function 211 212 Function Log(x As Double) As Double 213 If x = 0 Then 214 Log = Detail.GetInf(True) 215 ElseIf x < 0 Or IsNaN(x) Then 216 Log = Detail.GetNaN() 217 ElseIf IsInf(x) Then 218 Log = x 219 Else 220 Dim tmp = x * Detail._System_InverseSqrt2 221 Dim p = VarPtr(tmp) As *QWord 222 Dim m = GetQWord(p) And &h7FF0000000000000 223 Dim k = ((m >> 52) As DWord) As Long - 1022 224 SetQWord(p, m + &h0010000000000000) 225 x /= tmp 226 Log = Detail._System_LOG2 * k + Detail.Log1p(x - 1) 227 End If 228 End Function 229 230 Function Log10(x As Double) As Double 231 Return Log(x) * Detail._System_InverseLn10 232 End Function 233 234 '---- 235 '三角関数 236 Function Sin(x As Double) As Double 237 If IsNaN(x) Then 238 Return x 239 ElseIf IsInf(x) Then 240 Return Detail.GetNaN() 241 Exit Function 242 End If 243 244 Dim k As Long 245 Dim t As Double 246 247 t = Detail._Support_tan((x * 0.5) As Double, k) 248 t = 2 * t / (1 + t * t) 249 If (k And 1) = 0 Then 'k mod 2 = 0 Then 250 Return t 251 Else 252 Return -t 253 End If 254 End Function 255 256 Function Cos(x As Double) As Double 257 If IsNaN(x) Then 258 Return x 259 ElseIf IsInf(x) Then 260 Return Detail.GetNaN() 261 End If 262 263 Return Sin((Detail._System_HalfPI - Abs(x)) As Double) 264 End Function 265 266 Function Tan(x As Double) As Double 267 If IsNaN(x) Then 268 Tan = x 269 Exit Function 270 ElseIf IsInf(x) Then 271 Tan = Detail.GetNaN() 272 Exit Function 273 End If 274 275 Dim k As Long 276 Dim t As Double 277 t = Detail._Support_tan(x, k) 278 If (k And 1) = 0 Then 'k mod 2 = 0 Then 279 Return t 280 ElseIf t <> 0 Then 281 Return -1 / t 282 Else 283 Return CopySign(Detail.GetInf(False), -t) 284 End If 285 End Function 286 287 '-- 288 '三角関数の逆関数 289 Function Asin(x As Double) As Double 290 If x < -1 Or x > 1 Then 291 Asin = Detail.GetNaN() 292 Else 293 Asin = Atan(x / Sqrt(1 - x * x)) 294 End If 295 End Function 296 297 Function Acos(x As Double) As Double 298 If x < -1 Or x > 1 Then 299 Acos = Detail.GetNaN() 300 Else 301 Acos = Detail._System_HalfPI - Asin(x) 302 End If 303 End Function 304 305 Function Atan(x As Double) As Double 306 If IsNaN(x) Then 307 Atan = x 308 Exit Function 309 ElseIf IsInf(x) Then 310 Atan = CopySign(_System_PI, x) 311 Exit Function 312 End If 313 Dim i As Long 314 Dim sgn As Long 315 Dim dbl = 0 As Double 316 317 If x > 1 Then 318 sgn = 1 319 x = 1 / x 320 ElseIf x < -1 Then 321 sgn = -1 322 x = 1 / x 323 Else 324 sgn = 0 325 End If 326 327 For i = Detail._System_Atan_N To 1 Step -1 328 Dim t As Double 329 t = i * x 330 dbl = (t * t) / (2 * i + 1 + dbl) 331 Next 332 333 If sgn > 0 Then 334 Atan = Detail._System_HalfPI - x / (1 + dbl) 335 ElseIf sgn < 0 Then 336 Atan = -Detail._System_HalfPI - x / (1 + dbl) 337 Else 338 Atan = x / (1 + dbl) 339 End If 340 End Function 341 342 Function Atan2(y As Double, x As Double) As Double 343 If x = 0 Then 344 Atan2 = Sgn(y) * Detail._System_HalfPI 345 Else 346 Atan2 = Atn(y / x) 347 If x < 0 Then 348 Atan2 += CopySign(_System_PI, y) 349 End If 350 End If 351 End Function 352 353 '---- 354 '双曲線関数 355 Function Sinh(x As Double) As Double 356 If Abs(x) > Detail._System_EPS5 Then 357 Dim t As Double 358 t = Exp(x) 359 Return (t - 1 / t) * 0.5 360 Else 361 Return x * (1 + x * x * 0.166666666666667) ' x * (1 + x * x / 6) 362 End If 363 End Function 364 365 Function Tanh(x As Double) As Double 366 If x > Detail._System_EPS5 Then 367 Return 2 / (1 + Exp(-2 * x)) - 1 368 ElseIf x < -Detail._System_EPS5 Then 369 Return 1 - 2 / (Exp(2 * x) + 1) 370 Else 371 Return x * (1 - x * x * 0.333333333333333) 'x * (1 - x * x / 3) 372 End If 373 End Function 374 375 376 '---- 377 '浮動小数点数判定 378 Function IsNaN(ByVal x As Double) As Boolean 379 Dim p = VarPtr(x) As *DWord 380 IsNaN = False 381 If (p[1] And &H7FF00000) = &H7FF00000 Then 382 If (p[0] <> 0) Or ((p[1] And &HFFFFF) <> 0) Then 383 IsNaN = True 384 End If 385 End If 386 End Function 387 388 Function IsInf(x As Double) As Boolean 389 Dim p = VarPtr(x) As *DWord 390 p[1] And= &h7fffffff 391 Dim inf = Detail.GetInf(False) 392 IsInf = (memcmp(p As *Byte, VarPtr(inf), SizeOf (Double)) = 0) 393 End Function 394 395 Function IsFinite(x As Double) As Boolean 396 Dim p = VarPtr(x) As *DWord 397 p[1] And= &H7FF00000 398 IsFinite = ( p[1] And &H7FF00000 ) = &H7FF00000 399 End Function 400 401 '---- 402 'その他 17 403 Function Hypot(x As Double, y As Double) As Double 18 404 If x = 0 Then … … 33 419 End Function 34 420 35 Function Log1p(x As Double) As Double36 If x < -1 Or IsNaN(x) Then37 Log1p = ActiveBasic.Math.Detail.GetNaN()38 ElseIf x = 0 Then39 x = 040 ElseIf IsInf(x) Then41 Log1p = x42 Else43 Log1p = ActiveBasic.Math.Detail.Log1p(x)44 End If45 End Function46 47 Function IsNaN(ByVal x As Double) As Boolean48 Dim p = VarPtr(x) As *DWord49 IsNaN = False50 If (p[1] And &H7FF00000) = &H7FF00000 Then51 If (p[0] <> 0) Or ((p[1] And &HFFFFF) <> 0) Then52 IsNaN = True53 End If54 End If55 End Function56 57 Function IsInf(x As Double) As Boolean58 Dim p = VarPtr(x) As *DWord59 p[1] And= &h7fffffff60 Dim inf = ActiveBasic.Math.Detail.GetInf(False)61 IsInf = (memcmp(p As *Byte, VarPtr(inf), SizeOf (Double)) = 0)62 End Function63 64 Function IsFinite(x As Double) As Boolean65 Dim p = VarPtr(x) As *DWord66 p[1] And= &H7FF0000067 IsFinite = ( p[1] And &H7FF00000 ) = &H7FF0000068 End Function69 70 421 Namespace Detail 71 422 … … 111 462 Function Log1p(x As Double) As Double 112 463 Dim s = 0 As Double 113 Dim i = 7As Long464 Dim i = _System_Log_N As Long 114 465 While i >= 1 115 466 Dim t = (i * x) As Double … … 121 472 122 473 Function _Support_tan(x As Double, ByRef k As Long) As Double 123 If x >=0 Then124 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*k474 If x >= 0 Then 475 k = ( Fix(x * _System_InverseHalfPI) + 0.5 ) As Long 476 Else 477 k = ( Fix(x * _System_InverseHalfPI) - 0.5 ) As Long 478 End If 479 480 x = (x - (3217.0 / 2048.0) * k) + _System_D * k 130 481 131 482 Dim x2 = x * x … … 133 484 134 485 Dim i As Long 135 For i =19To 3 Step -2136 t =x2/(i-t)486 For i = _System_UrTan_N To 3 Step -2 487 t = x2 / (i - t) 137 488 Next 138 489 139 _Support_tan=x/(1-t) 140 End Function 490 _Support_tan = x / (1 - t) 491 End Function 492 493 Const _System_D = 4.4544551033807686783083602485579e-6 As Double 494 Const _System_UrTan_N = 19 As Long 495 Const _System_EPS5 = 0.001 As Double 496 Const _System_Atan_N = 20 As Long 497 Const _System_HalfPI = (_System_PI * 0.5) 498 Const _System_InverseHalfPI = (2 / _System_PI) '1 / (PI / 2) 499 Const _System_InverseLn10 = 0.43429448190325182765112891891661 '1 / (ln 10) 500 Const _System_InverseSqrt2 = 0.70710678118654752440084436210485 '1 / (√2) 501 Const _System_LOG2 = 0.6931471805599453094172321214581765680755 502 Const _System_Log_N = 7 As Long 141 503 142 504 End Namespace 'Detail -
trunk/ab5.0/ablib/src/Classes/ActiveBasic/Strings/SPrintF.ab
r521 r589 376 376 Sub FormatFloatG_RemoveLowDigit(sb As System.Text.StringBuilder, start As Long, flags As FormatFlags) 377 377 Imports ActiveBasic.Strings 378 378 379 379 Dim count = sb.Length 380 380 If (flags And Alt) = 0 Then … … 694 694 */ 695 695 Function IntegerD_Convert(buf As *Char, xq As QWord, flags As FormatFlags) As DWord 696 Return IntegerU_Convert(buf, Abs((xq As DWord) As Long) As DWord, flags)696 Return IntegerU_Convert(buf, Math.Abs((xq As DWord) As Long) As DWord, flags) 697 697 End Function 698 698 … … 703 703 */ 704 704 Function IntegerLD_Convert(buf As *Char, x As QWord, flags As FormatFlags) As DWord 705 Return IntegerLU_Convert(buf, Abs(x As Int64) As QWord, flags)705 Return IntegerLU_Convert(buf, Math.Abs(x As Int64) As QWord, flags) 706 706 End Function 707 707 -
trunk/ab5.0/ablib/src/Classes/System/Math.ab
r497 r589 24 24 25 25 Static Function Abs(value As SByte) As SByte 26 If value<0 then26 If value<0 Then 27 27 return -value 28 28 Else … … 32 32 33 33 Static Function Abs(value As Integer) As Integer 34 If value<0 then34 If value<0 Then 35 35 return -value 36 36 Else … … 40 40 41 41 Static Function Abs(value As Long) As Long 42 If value<0 then42 If value<0 Then 43 43 return -value 44 44 Else … … 48 48 49 49 Static Function Abs(value As Int64) As Int64 50 If value<0 then50 If value<0 Then 51 51 return -value 52 52 Else … … 56 56 57 57 Static Function Acos(x As Double) As Double 58 If x < -1 Or x > 1 Then 59 Acos = ActiveBasic.Math.Detail.GetNaN() 60 Else 61 Acos = _System_HalfPI - Asin(x) 62 End If 58 Acos = ActiveBasic.Math.Acos(x) 63 59 End Function 64 60 65 61 Static Function Asin(x As Double) As Double 66 If x < -1 Or x > 1 Then 67 Asin = ActiveBasic.Math.Detail.GetNaN() 68 Else 69 Asin = Math.Atan(x / Sqrt(1 - x * x)) 70 End If 62 Asin = ActiveBasic.Math.Asin(x) 71 63 End Function 72 64 73 65 Static Function Atan(x As Double) As Double 74 If ActiveBasic.Math.IsNaN(x) Then 75 Atan = x 76 Exit Function 77 ElseIf ActiveBasic.Math.IsInf(x) Then 78 Atan = ActiveBasic.Math.CopySign(_System_PI, x) 79 Exit Function 80 End If 81 Dim i As Long 82 Dim sgn As Long 83 Dim dbl = 0 As Double 84 85 If x > 1 Then 86 sgn = 1 87 x = 1 / x 88 ElseIf x < -1 Then 89 sgn = -1 90 x = 1 / x 91 Else 92 sgn = 0 93 End If 94 95 For i = _System_Atan_N To 1 Step -1 96 Dim t As Double 97 t = i * x 98 dbl = (t * t) / (2 * i + 1 + dbl) 99 Next 100 101 If sgn > 0 Then 102 Atan = _System_HalfPI - x / (1 + dbl) 103 ElseIf sgn < 0 Then 104 Atan = -_System_HalfPI - x / (1 + dbl) 105 Else 106 Atan = x / (1 + dbl) 107 End If 66 Atan = ActiveBasic.Math.Atan(x) 108 67 End Function 109 68 110 69 Static Function Atan2(y As Double, x As Double) As Double 111 If x = 0 Then 112 Atan2 = Sgn(y) * _System_HalfPI 113 Else 114 Atan2 = Atn(y / x) 115 If x < 0 Then 116 Atan2 += ActiveBasic.Math.CopySign(_System_PI, y) 117 End If 118 End If 70 Atan2 = ActiveBasic.Math.Atan2(y, x) 119 71 End Function 120 72 … … 131 83 132 84 Static Function Cos(x As Double) As Double 133 If ActiveBasic.Math.IsNaN(x) Then 134 Return x 135 ElseIf ActiveBasic.Math.IsInf(x) Then 136 Return ActiveBasic.Math.Detail.GetNaN() 137 End If 138 139 Return Math.Sin((_System_HalfPI - Math.Abs(x)) As Double) 85 Cos = ActiveBasic.Math.Cos(x) 140 86 End Function 141 87 142 88 Static Function Cosh(value As Double) As Double 143 89 Dim t = Math.Exp(value) 144 return (t + 1 / t) * 0.590 Return (t + 1 / t) * 0.5 145 91 End Function 146 92 147 93 Static Function DivRem(x As Long, y As Long, ByRef ret As Long) As Long 148 94 ret = x Mod y 149 return x \ y95 Return x \ y 150 96 End Function 151 97 … … 155 101 End Function 156 102 157 'Equals158 159 103 Static Function Exp(x As Double) As Double 160 If ActiveBasic.Math.IsNaN(x) Then 161 Return x 162 Else If ActiveBasic.Math.IsInf(x) Then 163 If 0 > x Then 164 Return 0 165 Else 166 Return x 167 End If 168 End If 169 Dim k As Long 170 If x >= 0 Then 171 k = Fix(x / _System_LOG2 + 0.5) 172 Else 173 k = Fix(x / _System_LOG2 - 0.5) 174 End If 175 176 x -= k * _System_LOG2 177 178 Dim x2 = x * x 179 Dim w = x2 / 22 180 181 Dim i = 18 182 While i >= 6 183 w = x2 / (w + i) 184 i -= 4 185 Wend 186 187 Return ldexp((2 + w + x) / (2 + w - x), k) 104 Exp = ActiveBasic.Math.Exp(x) 188 105 End Function 189 106 … … 216 133 217 134 Static Function Log(x As Double) As Double 218 If x = 0 Then 219 Log = ActiveBasic.Math.Detail.GetInf(True) 220 ElseIf x < 0 Or ActiveBasic.Math.IsNaN(x) Then 221 Log = ActiveBasic.Math.Detail.GetNaN() 222 ElseIf ActiveBasic.Math.IsInf(x) Then 223 Log = x 224 Else 225 Dim tmp = x * _System_InverseSqrt2 226 Dim p = VarPtr(tmp) As *QWord 227 Dim m = GetQWord(p) And &h7FF0000000000000 228 Dim k = ((m >> 52) As DWord) As Long - 1022 229 SetQWord(p, m + &h0010000000000000) 230 x /= tmp 231 Log = _System_LOG2 * k + ActiveBasic.Math.Detail.Log1p(x - 1) 232 End If 135 Log = ActiveBasic.Math.Log(x) 233 136 End Function 234 137 235 138 Static Function Log10(x As Double) As Double 236 Return Math.Log(x) * _System_InverseLn10139 Log = ActiveBasic.Math.Log10(x) 237 140 End Function 238 141 239 142 Static Function Max(value1 As Byte, value2 As Byte) As Byte 240 If value1>value2 then143 If value1>value2 Then 241 144 return value1 242 145 Else … … 246 149 247 150 Static Function Max(value1 As SByte, value2 As SByte) As SByte 248 If value1>value2 then151 If value1>value2 Then 249 152 return value1 250 153 Else … … 254 157 255 158 Static Function Max(value1 As Word, value2 As Word) As Word 256 If value1>value2 then159 If value1>value2 Then 257 160 return value1 258 161 Else … … 262 165 263 166 Static Function Max(value1 As Integer, value2 As Integer) As Integer 264 If value1>value2 then167 If value1>value2 Then 265 168 return value1 266 169 Else … … 270 173 271 174 Static Function Max(value1 As DWord, value2 As DWord) As DWord 272 If value1>value2 then175 If value1>value2 Then 273 176 return value1 274 177 Else … … 278 181 279 182 Static Function Max(value1 As Long, value2 As Long) As Long 280 If value1>value2 then183 If value1>value2 Then 281 184 return value1 282 185 Else … … 286 189 287 190 Static Function Max(value1 As QWord, value2 As QWord) As QWord 288 If value1>value2 then191 If value1>value2 Then 289 192 return value1 290 193 Else … … 294 197 295 198 Static Function Max(value1 As Int64, value2 As Int64) As Int64 296 If value1>value2 then199 If value1>value2 Then 297 200 return value1 298 201 Else … … 302 205 303 206 Static Function Max(value1 As Single, value2 As Single) As Single 304 If value1>value2 then207 If value1>value2 Then 305 208 return value1 306 209 Else … … 310 213 311 214 Static Function Max(value1 As Double, value2 As Double) As Double 312 If value1>value2 then215 If value1>value2 Then 313 216 return value1 314 217 Else … … 318 221 319 222 Static Function Min(value1 As Byte, value2 As Byte) As Byte 320 If value1<value2 then223 If value1<value2 Then 321 224 return value1 322 225 Else … … 326 229 327 230 Static Function Min(value1 As SByte, value2 As SByte) As SByte 328 If value1<value2 then231 If value1<value2 Then 329 232 return value1 330 233 Else … … 334 237 335 238 Static Function Min(value1 As Word, value2 As Word) As Word 336 If value1<value2 then239 If value1<value2 Then 337 240 return value1 338 241 Else … … 342 245 343 246 Static Function Min(value1 As Integer, value2 As Integer) As Integer 344 If value1<value2 then247 If value1<value2 Then 345 248 return value1 346 249 Else … … 350 253 351 254 Static Function Min(value1 As DWord, value2 As DWord) As DWord 352 If value1<value2 then255 If value1<value2 Then 353 256 return value1 354 257 Else … … 358 261 359 262 Static Function Min(value1 As Long, value2 As Long) As Long 360 If value1<value2 then263 If value1<value2 Then 361 264 return value1 362 265 Else … … 366 269 367 270 Static Function Min(value1 As QWord, value2 As QWord) As QWord 368 If value1<value2 then271 If value1<value2 Then 369 272 return value1 370 273 Else … … 374 277 375 278 Static Function Min(value1 As Int64, value2 As Int64) As Int64 376 If value1<value2 then279 If value1<value2 Then 377 280 return value1 378 281 Else … … 382 285 383 286 Static Function Min(value1 As Single, value2 As Single) As Single 384 If value1<value2 then287 If value1<value2 Then 385 288 return value1 386 289 Else … … 390 293 391 294 Static Function Min(value1 As Double, value2 As Double) As Double 392 If value1<value2 then295 If value1<value2 Then 393 296 return value1 394 297 Else … … 404 307 405 308 Static Function Round(value As Double) As Double'他のバージョン、誰か頼む。 406 If value+0.5<>Int(value+0.5) then309 If value+0.5<>Int(value+0.5) Then 407 310 value=Int(value+0.5) 408 ElseIf Int(value+0.5)=Int(value*2+1)/2 then311 ElseIf Int(value+0.5)=Int(value*2+1)/2 Then 409 312 value=Int(value+0.5) 410 313 Else … … 414 317 415 318 Static Function Sign(value As Double) As Long 416 If value = 0 then417 return 0 418 ElseIf value > 0 then419 return 1 420 Else 421 return -1 422 End If 423 End Function 424 319 If value = 0 Then 320 return 0 321 ElseIf value > 0 Then 322 return 1 323 Else 324 return -1 325 End If 326 End Function 327 /* 425 328 Static Function Sign(value As SByte) As Long 426 If value = 0 then427 return 0 428 ElseIf value > 0 then329 If value = 0 Then 330 return 0 331 ElseIf value > 0 Then 429 332 return 1 430 333 Else … … 434 337 435 338 Static Function Sign(value As Integer) As Long 436 If value = 0 then437 return 0 438 ElseIf value > 0 then339 If value = 0 Then 340 return 0 341 ElseIf value > 0 Then 439 342 return 1 440 343 Else … … 444 347 445 348 Static Function Sign(value As Long) As Long 446 If value = 0 then447 return 0 448 ElseIf value > 0 then349 If value = 0 Then 350 return 0 351 ElseIf value > 0 Then 449 352 return 1 450 353 Else … … 454 357 455 358 Static Function Sign(value As Int64) As Long 456 If value = 0 then457 return 0 458 ElseIf value > 0 then359 If value = 0 Then 360 return 0 361 ElseIf value > 0 Then 459 362 return 1 460 363 Else … … 464 367 465 368 Static Function Sign(value As Single) As Long 466 If value = 0 then 467 return 0 468 ElseIf value > 0 then 469 return 1 470 Else 471 return -1 472 End If 473 End Function 474 475 Static Function Sin(value As Double) As Double 476 If ActiveBasic.Math.IsNaN(value) Then 477 Return value 478 ElseIf ActiveBasic.Math.IsInf(value) Then 479 Return ActiveBasic.Math.Detail.GetNaN() 480 Exit Function 481 End If 482 483 Dim k As Long 484 Dim t As Double 485 486 t = urTan((value * 0.5) As Double, k) 487 t = 2 * t / (1 + t * t) 488 If (k And 1) = 0 Then 'k mod 2 = 0 Then 489 Return t 490 Else 491 Return -t 492 End If 369 If value = 0 Then 370 return 0 371 ElseIf value > 0 Then 372 return 1 373 Else 374 return -1 375 End If 376 End Function 377 */ 378 Static Function Sin(x As Double) As Double 379 Sin = ActiveBasic.Math.Sin(x) 493 380 End Function 494 381 495 382 Static Function Sinh(x As Double) As Double 496 If Math.Abs(x) > _System_EPS5 Then 497 Dim t As Double 498 t = Math.Exp(x) 499 Return (t - 1 / t) * 0.5 500 Else 501 Return x * (1 + x * x * 0.166666666666667) ' x * (1 + x * x / 6) 502 End If 383 Sinh = ActiveBasic.Math.Sinh(x) 503 384 End Function 504 385 505 386 Static Function Sqrt(x As Double) As Double 506 If x > 0 Then 507 If ActiveBasic.Math.IsInf(x) Then 508 Sqrt = x 509 Else 510 Sqrt = x 511 Dim i = (VarPtr(Sqrt) + 6) As *Word 512 Dim jj = GetWord(i) As Long 513 Dim j = jj >> 5 As Long 514 Dim k = (jj And &h0000001f) As Long 515 j = (j + 511) << 4 + k 516 SetWord(i, j) 517 Dim last As Double 518 Do 519 last = Sqrt 520 Sqrt = (x / Sqrt + Sqrt) * 0.5 521 Loop While Sqrt <> last 522 End If 523 ElseIf x < 0 Then 524 Sqrt = ActiveBasic.Math.Detail.GetNaN() 525 Else 526 'x = 0 Or NaN 527 Sqrt = x 528 End If 387 Sqrt = ActiveBasic.Math.Sqrt(x) 529 388 End Function 530 389 531 390 Static Function Tan(x As Double) As Double 532 If ActiveBasic.Math.IsNaN(x) Then 533 Tan = x 534 Exit Function 535 ElseIf ActiveBasic.Math.IsInf(x) Then 536 Tan = ActiveBasic.Math.Detail.GetNaN() 537 Exit Function 538 End If 539 540 Dim k As Long 541 Dim t As Double 542 t = urTan(x, k) 543 If (k And 1) = 0 Then 'k mod 2 = 0 Then 544 Return t 545 ElseIf t <> 0 Then 546 Return -1 / t 547 Else 548 Return ActiveBasic.Math.CopySign(ActiveBasic.Math.Detail.GetInf(False), -t) 549 End If 391 Tan = ActiveBasic.Math.Tan(x) 550 392 End Function 551 393 552 394 Static Function Tanh(x As Double) As Double 553 If x > _System_EPS5 Then 554 Return 2 / (1 + Math.Exp(-2 * x)) - 1 555 ElseIf x < -_System_EPS5 Then 556 Return 1 - 2 / (Math.Exp(2 * x) + 1) 557 Else 558 Return x * (1 - x * x * 0.333333333333333) 'x * (1 - x * x / 3) 559 End If 560 End Function 561 562 'ToString 395 Tanh = ActiveBasic.Math.Tanh(x) 396 End Function 563 397 564 398 Static Function Truncate(x As Double) As Double 565 399 Return Fix(x) 566 400 End Function 567 568 'Private569 Static Function urTan(x As Double, ByRef k As Long) As Double570 Dim i As Long571 Dim t As Double, x2 As Double572 573 If x >= 0 Then574 k = ( Fix(x * _System_InverseHalfPI) + 0.5 ) As Long575 Else576 k = ( Fix(x * _System_InverseHalfPI) - 0.5 ) As Long577 End If578 x = (x - (3217.0 / 2048.0) * k) + _System_D * k579 x2 = x * x580 t = 0581 For i = _System_UrTan_N To 3 Step -2582 t = x2 / (i - t)583 Next i584 urTan = x / (1 - t)585 End Function586 Private587 Static Const _System_Atan_N = 20 As Long588 Static Const _System_UrTan_N = 17 As Long589 Static Const _System_D = 4.4544551033807686783083602485579e-6 As Double590 Static Const _System_EPS5 = 0.001 As Double591 401 End Class 592 402 593 403 End Namespace 594 595 Const _System_HalfPI = (_System_PI * 0.5)596 Const _System_InverseHalfPI = (2 / _System_PI) '1 / (PI / 2)597 Const _System_InverseLn10 = 0.43429448190325182765112891891661 '1 / (ln 10)598 Const _System_InverseSqrt2 = 0.70710678118654752440084436210485 '1 / (√2) -
trunk/ab5.0/ablib/src/basic/function.sbp
r581 r589 2 2 3 3 Const _System_PI = 3.14159265358979323846264 4 Const _System_LOG2 = 0.69314718055994530941723212145817656807555 4 Const _System_SQRT2 = 1.41421356237309504880168872421 6 Const _System_Log_N = 7 As Long7 5 8 6 '------------- サポート関数の定義 ------------- 9 7 10 Function ldexp(x As Double, n As Long) As Double11 If x = 0 Then12 ldexp = 013 Exit Function14 End If15 Dim pSrc = VarPtr(x) As *QWord16 Dim pDest = VarPtr(ldexp) As *QWord17 n += (pSrc[0] >> 52) As DWord And &h7FF18 pDest[0] = n << 52 Or (pSrc[0] And &h800FFFFFFFFFFFFF)19 End Function20 21 Function frexp(x As Double, ByRef n As Long) As Double22 If x = 0 Then23 n = 024 frexp = 025 Exit Function26 End If27 28 Dim pSrc = VarPtr(x) As *QWord29 Dim pDest = VarPtr(frexp) As *QWord30 n = ((pSrc[0] >> 52) As DWord And &h7FF) - 102231 pDest[0] = (pSrc[0] And &h800FFFFFFFFFFFFF) Or &h3FE000000000000032 End Function33 34 Function frexp(x As Single, ByRef n As Long) As Single35 If x = 0 Then36 n = 037 frexp = 038 Exit Function39 End If40 41 Dim pSrc As *DWord, pDest As *DWord42 pSrc = VarPtr(x) As *DWord43 pDest = VarPtr(frexp) As *DWord44 n = ((pSrc[0] >> 23) And &hFF) - 12645 pDest[0] = (pSrc[0] And &h807FFFFF) Or &h7E00000046 End Function47 48 8 Function ipow(x As Double, n As Long) As Double 49 Dim abs_n As Long 50 Dim r = 1 As Double 51 52 abs_n=Abs(n) As Long 53 While abs_n<>0 54 If abs_n and 1 Then r *= x 55 x = x * x 56 abs_n >>= 1 ' abs_n \= 2 57 Wend 58 59 If n>=0 Then 60 ipow=r 61 Else 62 ipow=1/r 63 End If 9 ipow = ActiveBasic.Math.pow(x, n) 64 10 End Function 65 11 66 12 Function pow(x As Double, y As Double) As Double 67 ' If -LONG_MAX<=y and y<=LONG_MAX and y=CDbl(Int(y)) Then 68 If y = (y As Long) Then 69 pow = ipow(x, y As Long) 70 ElseIf x>0 Then 71 pow = Exp(y * Log(x)) 72 Exit Function 73 ElseIf x<>0 or y<=0 Then 74 pow = ActiveBasic.Math.Detail.GetNaN() 75 Else 76 pow = 0 77 End If 13 pow = ActiveBasic.Math.pow(x, y) 78 14 End Function 79 15 … … 89 25 _System_RndNext = dwSeek 90 26 End Sub 91 92 27 93 28 '------------- ここからBasic標準関数の定義 ------------- … … 154 89 '---------- 155 90 156 Function Abs(number As Double) As Double 157 'Abs = System.Math.Abs(number) 158 If number < 0 then159 Abs = -number 160 Else 161 Abs = number 162 End If163 End Function 164 165 Function Abs(n umberAs Int64) As Int64166 If number < 0 then167 Abs = -number 168 Else 169 Abs = number 170 End If171 End Function 172 173 Function Abs(n umber As Long) As Long174 If number < 0 then175 Abs = -number 176 Else 177 Abs = number 178 End If91 /* 92 Function Abs(n As Double) As Double 93 Abs = ActiveBasic.Math.Abs(n) 94 End Function 95 96 Function Abs(n As Single) As Single 97 Abs = ActiveBasic.Math.Abs(n) 98 End Function 99 100 Function Abs(n As Int64) As Int64 101 Abs = ActiveBasic.Math.Abs(n) 102 End Function 103 104 Function Abs(n As Long) As Long 105 Abs = ActiveBasic.Math.Abs(n) 106 End Function 107 108 Function Abs(n As Integer) As Integer 109 Abs = ActiveBasic.Math.Abs(n) 110 End Function 111 112 Function Abs(n As SByte) As SByte 113 Abs = ActiveBasic.Math.Abs(n) 179 114 End Function 180 115 181 116 Function Exp(x As Double) As Double 182 Exp = System.Math.Exp(x)117 Exp = ActiveBasic.Math.Exp(x) 183 118 End Function 184 119 185 120 Function Log(x As Double) As Double 186 Log = System.Math.Log(x)187 End Function 188 189 Function Sgn(n umberAs Double) As Long190 Sgn = System.Math.Sign(number)191 End Function 192 193 Function Sqr( numberAs Double) As Double194 Sqr = System.Math.Sqrt(number)195 End Function 196 197 Function Atn( numberAs Double) As Double198 Atn = System.Math.Atan(number)121 Log = ActiveBasic.Math.Log(x) 122 End Function 123 */ 124 Function Sgn(n As Double) As Long 125 ' Sgn = ActiveBasic..Math.Sign(n) 126 End Function 127 128 Function Sqr(x As Double) As Double 129 Sqr = ActiveBasic.Math.Sqrt(x) 130 End Function 131 132 Function Atn(x As Double) As Double 133 Atn = ActiveBasic.Math.Atan(x) 199 134 End Function 200 135 201 136 Function Atn2(y As Double, x As Double) As Double 202 Atn2 = System.Math.Atan2(y, x)203 End Function 204 205 Function Sin( numberAs Double) As Double206 Sin = System.Math.Sin(number)207 End Function 208 209 Function Cos( numberAs Double) As Double210 Cos = System.Math.Cos(number)211 End Function 212 213 Function Tan( numberAs Double) As Double214 Tan = System.Math.Tan(number)215 End Function 216 137 Atn2 = ActiveBasic.Math.Atan2(y, x) 138 End Function 139 /* 140 Function Sin(x As Double) As Double 141 Sin = ActiveBasic.Math.Sin(x) 142 End Function 143 144 Function Cos(x As Double) As Double 145 Cos = ActiveBasic.Math.Cos(x) 146 End Function 147 148 Function Tan(x As Double) As Double 149 Tan = ActiveBasic.Math.Tan(x) 150 End Function 151 */ 217 152 Const RAND_UNIT = (1.0 / (LONG_MAX + 1.0)) 218 153 Function Rnd() As Double
Note:
See TracChangeset
for help on using the changeset viewer.