' Classes/System/Math.ab

#require <Classes/ActiveBasic/Math/Math.ab>

Namespace System

Class Math
Public
	Static Function E() As Double
		return 2.7182818284590452354
	End Function
/*
	Static Function PI() As Double
		return _System_PI
	End Function
*/
	Static Function Abs(value As Double) As Double
		SetQWord(VarPtr(Abs), GetQWord(VarPtr(value)) And &h7fffffffffffffff)
	End Function

	Static Function Abs(value As Single) As Single
		SetDWord(VarPtr(Abs), GetDWord(VarPtr(value)) And &h7fffffff)
	End Function

	Static Function Abs(value As SByte) As SByte
		If value<0 Then
			return -value
		Else
			return value
		End If
	End Function

	Static Function Abs(value As Integer) As Integer
		If value<0 Then
			return -value
		Else
			return value
		End If
	End Function

	Static Function Abs(value As Long) As Long
		If value<0 Then
			return -value
		Else
			return value
		End If
	End Function

	Static Function Abs(value As Int64) As Int64
		If value<0 Then
			return -value
		Else
			return value
		End If
	End Function

	Static Function Acos(x As Double) As Double
		Acos = ActiveBasic.Math.Acos(x)
	End Function

	Static Function Asin(x As Double) As Double
		Asin = ActiveBasic.Math.Asin(x)
	End Function

	Static Function Atan(x As Double) As Double
		Atan = ActiveBasic.Math.Atan(x)
	End Function

	Static Function Atan2(y As Double, x As Double) As Double
		Atan2 = ActiveBasic.Math.Atan2(y, x)
	End Function

	Static Function BigMul(x As Long, y As Long) As Int64
		Return (x As Int64) * y
	End Function

	Static Function Ceiling(x As Double) As Long
		Ceiling = Floor(x)
		If Ceiling <> x Then
			Ceiling++
		End If
	End Function

	Static Function Cos(x As Double) As Double
		Cos = ActiveBasic.Math.Cos(x)
	End Function

	Static Function Cosh(value As Double) As Double
		Dim t = Math.Exp(value)
		Return (t + 1 / t) * 0.5
	End Function

	Static Function DivRem(x As Long, y As Long, ByRef ret As Long) As Long
		ret = x Mod y
		Return x \ y
	End Function

	Static Function DivRem(x As Int64, y As Int64, ByRef ret As Int64) As Int64
		DivRem = x \ y
		ret = x - (DivRem) * y
	End Function

	Static Function Exp(x As Double) As Double
		Exp = ActiveBasic.Math.Exp(x)
	End Function

	Static Function Floor(value As Double) As Long
		Return Int(value)
	End Function

	Static Function IEEERemainder(x As Double, y As Double) As Double
		If y = 0 Then Return ActiveBasic.Math.Detail.GetNaN()
		Dim q = x / y
		If q <> Int(q) Then
			If q + 0.5 <> Int(q + 0.5) Then
				q = Int(q + 0.5)
			ElseIf Int(q + 0.5) = Int(q * 2 + 1) / 2 Then
				q = Int(q + 0.5)
			Else
				q = Int(q - 0.5)
			End If
		End If
		If x - y * q = 0 Then
			If x > 0 Then
				Return +0
			Else
				Return -0
			End If
		Else
			Return x-y*q
		End If
	End Function

	Static Function Log(x As Double) As Double
		Log = ActiveBasic.Math.Log(x)
	End Function

	Static Function Log10(x As Double) As Double
		Log = ActiveBasic.Math.Log10(x)
	End Function

	Static Function Max(value1 As Byte, value2 As Byte) As Byte
		If value1>value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Max(value1 As SByte, value2 As SByte) As SByte
		If value1>value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Max(value1 As Word, value2 As Word) As Word
		If value1>value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Max(value1 As Integer, value2 As Integer) As Integer
		If value1>value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Max(value1 As DWord, value2 As DWord) As DWord
		If value1>value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Max(value1 As Long, value2 As Long) As Long
		If value1>value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Max(value1 As QWord, value2 As QWord) As QWord
		If value1>value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Max(value1 As Int64, value2 As Int64) As Int64
		If value1>value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Max(value1 As Single, value2 As Single) As Single
		If value1>value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Max(value1 As Double, value2 As Double) As Double
		If value1>value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Min(value1 As Byte, value2 As Byte) As Byte
		If value1<value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Min(value1 As SByte, value2 As SByte) As SByte
		If value1<value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Min(value1 As Word, value2 As Word) As Word
		If value1<value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Min(value1 As Integer, value2 As Integer) As Integer
		If value1<value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Min(value1 As DWord, value2 As DWord) As DWord
		If value1<value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Min(value1 As Long, value2 As Long) As Long
		If value1<value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Min(value1 As QWord, value2 As QWord) As QWord
		If value1<value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Min(value1 As Int64, value2 As Int64) As Int64
		If value1<value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Min(value1 As Single, value2 As Single) As Single
		If value1<value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Min(value1 As Double, value2 As Double) As Double
		If value1<value2 Then
			return value1
		Else
			return value2
		End If
	End Function

	Static Function Pow(x As Double, y As Double) As Double
		return pow(x, y)
	End Function

	'ReferenceEquals

	Static Function Round(value As Double) As Double'他のバージョン、誰か頼む。
		If value+0.5<>Int(value+0.5) Then
			value=Int(value+0.5)
		ElseIf Int(value+0.5)=Int(value*2+1)/2 Then
			value=Int(value+0.5)
		Else
			value=Int(value-0.5)
		End If
	End Function

	Static Function Sign(value As Double) As Long
		If value = 0 Then
			return 0
		ElseIf value > 0 Then
			return 1
		Else
			return -1
		End If
	End Function
/*
	Static Function Sign(value As SByte) As Long
		If value = 0 Then
			return 0
		ElseIf value > 0 Then
			return 1
		Else
			return -1
		End If
	End Function

	Static Function Sign(value As Integer) As Long
		If value = 0 Then
			return 0
		ElseIf value > 0 Then
			return 1
		Else
			return -1
		End If
	End Function

	Static Function Sign(value As Long) As Long
		If value = 0 Then
			return 0
		ElseIf value > 0 Then
			return 1
		Else
			return -1
		End If
	End Function

	Static Function Sign(value As Int64) As Long
		If value = 0 Then
			return 0
		ElseIf value > 0 Then
			return 1
		Else
			return -1
		End If
	End Function

	Static Function Sign(value As Single) As Long
		If value = 0 Then
			return 0
		ElseIf value > 0 Then
			return 1
		Else
			return -1
		End If
	End Function
*/
	Static Function Sin(x As Double) As Double
		Sin = ActiveBasic.Math.Sin(x)
	End Function

	Static Function Sinh(x As Double) As Double
		Sinh = ActiveBasic.Math.Sinh(x)
	End Function

	Static Function Sqrt(x As Double) As Double
		Sqrt = ActiveBasic.Math.Sqrt(x)
	End Function

	Static Function Tan(x As Double) As Double
		Tan = ActiveBasic.Math.Tan(x)
	End Function

	Static Function Tanh(x As Double) As Double
		Tanh = ActiveBasic.Math.Tanh(x)
	End Function

	Static Function Truncate(x As Double) As Double
		Return Fix(x)
	End Function
End Class

End Namespace
