This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCFMathUtility.bas
More file actions
269 lines (170 loc) · 5.53 KB
/
CFMathUtility.bas
File metadata and controls
269 lines (170 loc) · 5.53 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
B4J=true
Group=Cuppy Framework\Cuppy\Utilities
ModulesStructureVersion=1
Type=StaticCode
Version=5.51
@EndOfDesignText@
'This contain math related functions
Private Sub Process_Globals
'Real Exponential equavalent : 2.7182818284590452354
Public EXPONENTIAL_EQUIVALENT As Double = 2.718281828459045
Public NEGATIVE_INFINITY As String = "-Infinity"
Public POSITIVE_INFINITY As String = "Infinity"
Public MAX_SAFE_INTEGER As String = "9007199254740991"
Public MIN_SAFE_INTEGER As String = "-9007199254740991"
Public MIN_VALUE As String = "5e-324"
Public MAX_VALUE As String = "1.7976931348623157e+308"
End Sub
'Checks if a value is NaN
Public Sub IsNan(d As Double) As Boolean
Return d <> d
End Sub
'Inverse hyperbolic cosine
Public Sub Acosh (arg As Double) As Double
Return Logarithm(arg + Sqrt(arg * arg - 1) , EXPONENTIAL_EQUIVALENT)
End Sub
'Inverse hyperbolic sine
Public Sub Asinh (arg As Double) As Double
Return Logarithm(arg + Sqrt(arg * arg + 1) , EXPONENTIAL_EQUIVALENT)
End Sub
'Inverse hyperbolic tangent
Public Sub Atanh (arg As Double) As Double
Return 0.5 * Logarithm((1 + arg) / (1 - arg) , EXPONENTIAL_EQUIVALENT )
End Sub
'Hyperbolic cosine of a number
Public Sub Cosh (arg As Double) As Double
Return (Exp(arg) + Exp(-arg)) / 2
End Sub
'Convert integer in any base ( 2 to 10 ) to decimal integer.
'FROM: https://www.b4x.com/android/forum/threads/number-base-conversions.39360/
Public Sub ConvertToDecimal( n As Int, base As Int) As Int
Dim result As Int = 0
Dim multiplier As Int = 1
Do While n > 0
result = result + (n Mod 10) * multiplier
multiplier = multiplier * base
n = n / 10
Loop
Return result
End Sub
'Convert decimal integer to any base (2 to 10)
'FROM: https://www.b4x.com/android/forum/threads/number-base-conversions.39360/
Public Sub ConvertFromDecimal(n As Int, base As Int) As Int
Dim result As Int = 0
Dim multiplier As Int = 1
Do While n > 0
result = result + (n Mod base) * multiplier
multiplier = multiplier * 10
n = n / base
Loop
Return result
End Sub
'Convert decimal integer to any base up to 20, returns string
'FROM: https://www.b4x.com/android/forum/threads/number-base-conversions.39360/
Public Sub ConvertFromDecimal2(n As Int, base As Int) As String
Dim chars As String ="0123456789ABCDEFGHIJ"
Dim result As String = ""
Do While n > 0
result = chars.charAt(n Mod base) & result
n = n / base
Loop
Return result
End Sub
'Convert any base up to 20 to decimal integer
'FROM: https://www.b4x.com/android/forum/threads/number-base-conversions.39360/
Public Sub ConvertToDecimal2( n As String, base As Int) As Int
n = n.ToUpperCase
Dim result As Int = 0
Dim st As String
Dim chars As String ="0123456789ABCDEFGHIJ"
Dim k As Int = n.length - 1
Dim multiplier As Int = 1
For i = k To 0 Step -1
st = n.CharAt(i)
result = chars.IndexOf(st) * multiplier + result
multiplier = multiplier * base
Next
Return result
End Sub
'Convert from one base to another, both must be less or equal to 10
'FROM: https://www.b4x.com/android/forum/threads/number-base-conversions.39360/
Public Sub ConvertFromTo(n As Int, frombase As Int, tobase As Int) As Int
Dim t As Int = ConvertToDecimal(n,frombase)
Return ConvertFromDecimal(t,tobase)
End Sub
'Calculates the exponent of e
'Returns e raised to the power of arg
Public Sub Exp(arg As Double) As Double
Return Power(EXPONENTIAL_EQUIVALENT, arg)
End Sub
'Hyperbolic tangent of a number,
'which is equal to sinh(x)/cosh(x).
Public Sub Tanh(number As Double) As Double
Return 1 - 2 / (Exp(2 * number) + 1)
End Sub
'Hyperbolic sine of a number,
'which is equal to (exp(number) - exp(-number))/2).
Public Sub Sinh(number As Double) As Double
Return (Exp(number) - Exp(-number)) / 2
End Sub
'Check whether a value is infinite or not
Public Sub Is_infinite(val As String) As Boolean
Return val = POSITIVE_INFINITY Or val = NEGATIVE_INFINITY
End Sub
'Check whether a value is finite or not
Public Sub Is_finite(val As String) As Boolean
Return Not(Is_infinite(val))
End Sub
'Converts the number in degrees to the radian equivalent
Public Sub Deg2rad(angle As Double) As Double
Return angle * 0.017453292519943295
End Sub
'Converts a radian value to a degree value.
Public Sub Rad2deg(number As Double) As Double
Return number * 57.29577951308232
End Sub
'Calculate the length of the hypotenuse
'of a right-angle triangle
Public Sub Hypot(x As Double, y As Double) As Double
x = Abs(x)
y = Abs(y)
'
Dim t As Double = Min(x, y)
x = Max(x, y)
t = t / x
Return x * Sqrt(1 + t * t)
End Sub
'Calculate the average of a list numbers
'data = is the List of numbers to calculate their average
'roundUp = should value be rounded up or not
Public Sub NumbersAverage(data As List ,roundUp As Boolean) As Double
Dim total As Int = 0
'add up values
For Each value As Int In data
total= total + value
Next
If roundUp Then
' Round up values
Return Ceil(total / data.Size)
Else
'Do Not Round up values... Return raw
Return total / data.Size
End If
End Sub
'Calculate the average of a list numbers
'data = is the List of numbers to calculate their average
'NOTE: result is automatically rounded up
Public Sub NumbersAverage2(data As List) As Double
Return NumbersAverage(data, True)
End Sub
'
''TODO:log1p
'
''The log1p() function returns log(1+number),
''computed in a way that is accurate even
''when the value of number is close to zero.
'Public Sub log1p(number As Double) As Double
'
'End Sub
'
''