Thursday, October 20, 2022

Data type - Single

Data type Single is short for single precision floating point are stored as IEEE 32-bit (4-byte) ranging for negative values -3.402823E38 to -1.401298E-45 and for positive values 1.401298E-45 to 3.402823E38. Different from Double, Single can support 7 significant figures with 6 decimal places only. Single, Double and Decimal most probably at the same group may be different by range and accuracy of decimal points. Instead of Dim SglNum as Single we can use Dim SglNum!.

For more example refer below table and code. 

Below we try to get area in square meter base on width and length.

Base on result there is huge different in figure especially on decimal points. Which one is more accurate in this case? Double is the same as Direct.

VBA Vode:

Option Explicit
Sub Examples_Single()
    
    Dim i As Long
    
    'Single Declaration
    Dim SglOpnStk As Single, SglInOut As Single
    Dim SglClsStk!
    
    'Double Declaration
    Dim DblOpnStk As Double, DblInOut As Double
    Dim DblClsStk#
    
    i = 2
    With ActiveSheet
        Do
            'Sgleger
            SglOpnStk = .Range("B" & i): SglInOut = .Range("C" & i)
            SglClsStk = SglOpnStk * SglInOut
            
            'Long
            DblOpnStk = .Range("B" & i): DblInOut = .Range("C" & i)
            DblClsStk = DblOpnStk * DblInOut
            
            'Direct input
            .Range("D" & i) = .Range("B" & i) * .Range("C" & i)
            .Range("E" & i) = SglClsStk 'Single variables
            .Range("F" & i) = DblClsStk 'Double variables
            
            SglOpnStk = 0: SglInOut = 0: SglClsStk = 0
            DblOpnStk = 0: DblInOut = 0: DblClsStk = 0
            
            i = i + 1
        Loop Until .Range("A" & i) = ""
    End With

End Sub

Note: 

Single.Double and Decimal at the same group and choose wisely. Same as Integer and Long.

Microsoft Reference-Single-data-type
Other Reference-Single-data-type

Practice makes perfect. Thank You.

excelmacros
macro excel
excel programming
excel vba

No comments:

Post a Comment