如何从8个字节生成vba中的单个数据?

时间:2018-08-04 22:11:47

标签: vba excel-vba floating-point precision single-precision

下面的我的VBA代码,我想我无法获得vba的单部分和双部分,但是根据int,long和byte逻辑,结果还可以。在找出单值和双值的最大值时我会在哪里弄错?

Sub test()

    Debug.Print String(65535, vbCr) ' clear

    Const MaxByte As Long = (2 ^ 7) - 1
    Debug.Print "Byte,    [1 Byte (0...7 bit)]: "; MaxByte

    Const MaxInt As Integer = (2 ^ 15) - 1
    Debug.Print "Integer, [2 Byte (0..15 bit)]: "; MaxInt

    Const MaxLong As Long = (2 ^ 31) - 1
    Debug.Print "Long,    [4 Byte (0..31 bit)]: "; MaxLong

    ' But it works with 0-127 bit?
    Const MaxSingle As Single = (2 ^ 127) - 1
    Debug.Print "Single,  [4 Byte (0..31 bit)]: "; MaxSingle

    ' But it works with 0-1023 bit?
    Const MaxDouble As Double = (2 ^ 1023) - 1
    Debug.Print "Double,  [8 Byte (0..63 bit)]: "; MaxDouble

    Debug.Print "Decimal Sensetivity > Single: "; CSng(1 / 3)
    Debug.Print "Decimal Sensetivity > Double: "; CDbl(1 / 3)
End Sub

即时窗口中的结果

Byte,    [1 Byte (0...7 bit)]:  127 
Integer, [2 Byte (0..15 bit)]:  32767 
Long,    [4 Byte (0..31 bit)]:  2147483647 
Single,  [4 Byte (0..31 bit)]:  1,701412E+38 
Double,  [8 Byte (0..63 bit)]:  8,98846567431158E+307 
Decimal Sensetivity > Single:  0,3333333 
Decimal Sensetivity > Double:  0,333333333333333 

output

0 个答案:

没有答案