|
A simple expression evaluator
|
Total Hit (3421) |
While any Windows user could pop up the Calculator accessory to perform any type of math calculations, it would be great if you could offer him or her the capability to do simple math from within your application. This is a very simple expression evaluator function that does it:
This evaluator i
....Read More |
Rating
|
|
|
The "And" operator is faster than "Mod"
|
Total Hit (4149) |
Using the "And" operator instead of "Mod" may speed up your program under certain situations. Use the And operator instead of Mod when the divisor is a number in the form 2^N. For instance, there are two methods to extract the least significant byte in an Integer:
«Code LangId=1»
lowByte% = valu
....Read More |
Rating
|
|
|
Avoid Integer Overflow
|
Total Hit (2963) |
When working with integer expressions there is often the risk of raising the "Overflow" error. More specifically, there can be two occasions when this frequently occurs:
When you multiply or add Integer values and the result exceeds 32,767.
When you create generic string routines that iterate
....Read More |
Rating
|
|
|
Convert Hexadecimal numbers
|
Total Hit (4178) |
While Visual Basic offers the Hex$ function that converts a decimal value into its hexadecimal equivalent number, it seems that the inverse function is missing. Not true. Try out this one-liner:
«Code LangId=1»
Function HexToDec(HexValue As String) As Long
HexToDec = Val("&H" & HexValue)
....Read More |
Rating
|
|
|
Counting Bits
|
Total Hit (3307) |
It seems that the only way to count the number of 1's or 0's in a binary value is creating a loop that iterates on all the 16 or 32 bits of the number. There is, however, a faster algorithm:
«Code LangId=1»
Function BitCount (ByVal number As Long) As Integer
Dim bits As Integer, temp As Long
....Read More |
Rating
|
|
|
Determine whether the app is running on a flawed Pentium CPU
|
Total Hit (2746) |
Here's a simple test that you can use to determine whether the application is running on a system equipped with a Pentium CPU affected by the FDIV bug:
«Code LangId=1»
' return True if the CPU suffers from the FDIV bug
Function IsBuggedPentium() As Boolean
IsBuggedPentium = ((1 / 3221224
....Read More |
Rating
|
|
|
Evaluate the integer equal or higher than a given value
|
Total Hit (2845) |
The VBA language offers the Int() function, which returns the integer equal or lower than a given value, but lacks a similar function that returns the integer equal or higher than a given value. You can remedy with the following function:
«Code LangId=1»
' Returns the integer equal or higher tha
....Read More |
Rating
|
|
|
|
Understanding the "Allow Unrounded Floating Point Operations" option
|
Total Hit (2974) |
The Microsoft manuals preach that all the compiler options in the Advanced Optimization dialog box are to be considered unsafe, in that they might lead to incorrect results (or just program crashes!). This is true for most of them, but often one of such options - namely, the "Allow Unrounded Floatin
....Read More |
Rating
|
|
|
Undocumented behavior of the CInt() function
|
Total Hit (3105) |
The CInt() function rounds to the nearest integer value. In other words, CInt(2.4) returns 2, and CInt(2.6) returns 3.
This function exhibits an under-documented behavior when the fractional part is equal to 0.5. In this case, this function rounds down if the integer portion of the argument is e
....Read More |
Rating
|
|
|
Use integer division operator
|
Total Hit (2994) |
Use "\" instead of "/" when performing divisions between Integers. The "/" operator returns a Single value, therefore the seemingly efficient line
«Code LangId=1»
C% = A% / B%
«/Code»
actually requires three implicit conversions, two for converting the operands from Integer to Single (to pre
....Read More |
Rating
|
|
|
Any2Dec - Convert from any numeric base to decimal
|
Total Hit (3327) |
«Code LangId=1»' convert from any base to decimal
' BASE can be in the range 2-36
Function Any2Dec(ByVal otherBaseNumber As String, ByVal base As Integer) As Long
Dim index As Long
Dim digits As String
Dim digitValue As Long
' check base
If base < 2 Or base > 36 T
....Read More |
Rating
|
|
|
|
|
Atn2 - Arc tangent of Y/X
|
Total Hit (3470) |
«Code LangId=1»' arc tangent of Y/X - returns values in all four quadrants
Function Atn2(x As Double, y As Double) As Double
If x = 0 Then
Atn2 = Sgn(y) * 1.5707963267949
ElseIf x > 0 Then
Atn2 = Atn(y / x)
Else
Atn2 = Atn(y / x) + 3.14159265358979 * Sgn
....Read More |
Rating
|
|
|
|
Bin - Convert from decimal to binary
|
Total Hit (2795) |
«Code LangId=1»' convert from decimal to binary
' if you pass the Digits argument, the result is truncated
' to that number of digits
'
Function Bin(ByVal value As Long, Optional digits As Long = -1) As String
Dim result As String, exponent As Integer
' this is faster than creating t
....Read More |
Rating
|
|
|
BinToDec - Convert from binary to decimal
|
Total Hit (4116) |
«Code LangId=1»' convert from binary to decimal
'
Function BinToDec(value As String) As Long
Dim result As Long, i As Integer, exponent As Integer
For i = Len(value) To 1 Step -1
Select Case Asc(Mid$(value, i, 1))
Case 48 ' "0", do nothing
Case 4
....Read More |
Rating
|
|
|
BitClear - Clear a bit in a value
|
Total Hit (2735) |
«Code LangId=1»Function BitClear(ByVal value As Long, ByVal bit As Long) As Long
' simply AND with the negation of the bit mask
' Range checking is performed in Power2()
BitClear = (value And Not Power2(bit))
End Function
' Raise 2 to a power
' the exponent must be in the range [
....Read More |
Rating
|
|
|
BitCount - The number of "1" bits in a number
|
Total Hit (2706) |
«Code LangId=1»
' The number of 1's in a binary number
'
' This routine is based on the following property
' of binary numbers: n And (n-1) always clears the
' least significant "1" bit in the number
Function BitCount (ByVal number As Long) As Integer
Do While number
number =
....Read More |
Rating
|
|
|
BitSet - Set a bit in a number
|
Total Hit (2881) |
«Code LangId=1»' Set a bit in a value
'
' NOTE: requires Power2()
Function BitSet(ByVal value As Long, ByVal bit As Long) As Long
' simply OR with the bit mask
' Range checking is performed in Power2()
BitSet = (value Or Power2(bit))
End Function
' Raise 2 to a power
' the e
....Read More |
Rating
|
|
|
BitTest - Test the value of a bit
|
Total Hit (3582) |
«Code LangId=1»' Test the value of a bit
'
' NOTE: requires Power2()
Function BitTest(ByVal value As Long, ByVal bit As Long) As Boolean
' simply AND with the bit mask
' Range checking is performed in Power2()
BitTest = (value And Power2(bit))
End Function
' Raise 2 to a po
....Read More |
Rating
|
|
|
BitToggle - Invert a bit in a value
|
Total Hit (2963) |
«Code LangId=1»' Toggle a bit in a value
'
' NOTE: requires Power2()
Function BitToggle(ByVal value As Long, ByVal bit As Long) As Long
' simply XOR with the negation of the bit mask
' Range checking is performed in Power2()
BitToggle = (value Xor Power2(bit))
End Function
....Read More |
Rating
|
|
|
CComplexNumber - A class for dealing with complex numbers
|
Total Hit (2765) |
«Code LangId=1»Option Explicit
'------------------------------------------
' A class for dealing with complex numbers
'------------------------------------------
' The main properties
Public Real As Double
Public Imaginary As Double
' Initialize this complex number
' (returns Me)
Fu
....Read More |
Rating
|
|
|
|
|
|
Cot, Sec, Csc - Missing trig functions
|
Total Hit (3678) |
«Code LangId=1»' Cotangent of an angle
Function Cot(radians As Double) As Double
Cot = 1 / Tan(radians)
End Function
' Secant of an angle
Function Sec(radians As Double) As Double
Sec = 1 / Cos(radians)
End Function
' cosecant of an angle
Function Csc(radians As Double) As
....Read More |
Rating
|
|
|
Crc16 - Evaluate the 16-bit CRC of an array of bytes
|
Total Hit (4653) |
«Code LangId=1»Option Explicit
' Evalutate the 16-bit CRC (Cyclic Redundancy Checksum) of an array of bytes
'
' If you omit the second argument, the entire array is considered
Function Crc16(cp() As Byte, Optional ByVal Size As Long = -1) As Long
Dim i As Long
Dim fcs As Long
Static
....Read More |
Rating
|
|
|
Dec2Any - Convert a decimal number to any other base
|
Total Hit (3268) |
«Code LangId=1»' convert a number to any base
' BASE can be in the range 2-36
Function Dec2Any(ByVal number As Long, ByVal base As Integer) As String
Dim index As Long
Dim digits As String
Dim digitValue As Long
' check base
If base < 2 Or base > 36 Then Err.Raise
....Read More |
Rating
|
|