forked from lee-soft/ViPad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modMath.bas
100 lines (70 loc) · 2.39 KB
/
modMath.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
Attribute VB_Name = "MathSupport"
Option Explicit
Public Function IsEqualWithinReason(ByVal sourceNumber As Long, ByVal secondNumber As Long, Optional ByVal offsetAmount As Long = 3) As Boolean
'15, 15
If sourceNumber > (secondNumber + offsetAmount) Then
IsEqualWithinReason = False
Exit Function
End If
If sourceNumber < (secondNumber - offsetAmount) Then
IsEqualWithinReason = False
Exit Function
End If
IsEqualWithinReason = True
End Function
Public Function HowManyInto(ByVal lngSrcNumber As Long, ByVal lngByNumber As Long)
If lngByNumber = 0 Then Exit Function
Dim intoCount As Long
While lngSrcNumber >= 0
lngSrcNumber = lngSrcNumber - lngByNumber
intoCount = intoCount + 1
Wend
If lngSrcNumber < 0 Then
HowManyInto = intoCount - 1
End If
End Function
Public Function RoundIt(ByVal lngSrcNumber As Integer, ByVal lngByNumber As Integer)
'Round(12, 5)
Dim lngModResult As Long
lngModResult = (lngSrcNumber Mod lngByNumber)
If lngModResult >= lngByNumber Then
RoundIt = CLng(SymArith(lngSrcNumber / lngByNumber, 0) * lngByNumber + 1)
Else
RoundIt = CLng(SymArith(lngSrcNumber / lngByNumber, 0) * lngByNumber)
End If
End Function
Private Function SymArith(ByVal X As Double, _
Optional ByVal DecimalPlaces As Double = 1) As Double
SymArith = Fix(X * (10 ^ DecimalPlaces) + 0.5 * Sgn(X)) _
/ (10 ^ DecimalPlaces)
End Function
Public Function Floor(dblValue As Double) As Double
'Returns the largest integer less than or equal to the specified number.
On Error GoTo PROC_ERR
Dim myDec As Long
myDec = InStr(1, CStr(dblValue), ".", vbTextCompare)
If myDec > 0 Then
Floor = CDbl(Strings.Left(CStr(dblValue), myDec))
Else
Floor = dblValue
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox Err.Description, vbInformation, "Round Down"
End Function
Public Function Ceiling(dblValue As Double) As Double
'Returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.
On Error GoTo PROC_ERR
Dim myDec As Long
myDec = InStr(1, CStr(dblValue), ".", vbTextCompare)
If myDec > 0 Then
Ceiling = CDbl(Strings.Left(CStr(dblValue), myDec)) + 1
Else
Ceiling = dblValue
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox Err.Description, vbInformation, "Round Up"
End Function