是否有可能使一个双浮点值



问题:是否可以在Small和Visual Basic中制作双Float值?


我一直在尝试在Small/Visual Basic中创建一个双浮点值(就像两者一样)…
我总是运气不好…我总是以如下错误结束:

    at System.Decimal..ctor(Double value)
    at System.Decimal.op_Explicit(Double value)
    at Microsoft.SmallBasic.Library.Primitive.op_Implicit(Double value)
    at _SmallBasicProgram._Main()

或者,在Visual Basic中运行:

overflow


那么,有没有办法使双非整(十进制)精度浮点数?
代码(我试过)是:

小基本:

var1 = 18446744073709551615
var2 = 1797693134862315800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

(是的,对不起,0的数量…是303个)

Visual Basic中:

Module experiment_doesDoubleFloat_workModule
    Dim var1, var2 As Double
    Sub Main()
        var1 = 18446744073709551615
        var2 = 1797693134862315800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    End Sub
End Module

我是不是搞砸了什么?
我也不知道哪些标签适合这个…(除了smallbasic标签)

这不起作用的原因是因为您有效地为编译器提供了Integer格式的常量,并且编译器正在尝试将您的巨大整数转换为浮点值。这将失败,因为>300位的值大于任何标准数据类型所能容纳的最大整数。

如果你想在你的代码中分配一个常量值,它必须是编译器可以解析的格式,即:

 var1 = 1.8446744073709552E+19
 var2 = 1.7976931348623157E+308

事实上,你会注意到当你输入:

 var1 = 1.8446744073709551615E+19

值自动转换为:

 var1 = 1.8446744073709552E+19

,因为原始值包含比double格式所能容纳的更高的精度。此外,我没有计算代码示例中的零的数量,但如果它是303个,那么这使得var2 = 1.797... E+319的值,对于double来说也太大了。加上292个零后,该值变为...E+308,如上所述,这是可表示的最大双精度浮点值。

请注意,将整型常量赋给浮点变量是没有问题的,只要代码中的值足够小,可以放入Integer。看:

var1 = 9223372036854775807  ' << largest Int64

编译得很好,但是还有一个

var1 = 9223372036854775808

失败。


进一步阅读

每个计算机科学家都应该知道的浮点运算

如果是为自动化设置,请试试:

 Public Function REAL_to_DOUBLE(ByVal i3264 As Long) '
        Dim sBit As Integer = 1
        Dim expBits As Integer = 8 'for 32 bits, 11 for 64 bits
        Dim expAux As Integer = 127 'for 32 bts, 1023 for 64 bits
        Dim nBits As Integer = 32 ' for 23 bits, 64 for 64 bits
        Dim dec As Double = 1
        Dim hexstring As String = Hex(i3264)
        If hexstring.Length > 8 Then
            expBits = 11
            nBits = 64
            expAux = 1023
        End If
        Dim bin_ As String
        If nBits = 32 Then
            bin_ = Convert.ToString(Convert.ToInt32(hexstring, 16), 2).PadLeft(nBits, "0"c)
        Else
            bin_ = Convert.ToString(Convert.ToInt64(hexstring, 16), 2).PadLeft(nBits, "0"c)
        End If
        Dim _sinal As Integer = -1
        If (bin_.Substring(0, 1)) = "0" Then _sinal = 1
        Dim _e As String = bin_.Substring(1, expBits).PadLeft(expBits, "0"c)
        Dim a As Integer = Convert.ToInt32(_e, 2)
        Dim exp_ As Integer = a - expAux
        Dim matissa As String = bin_.Substring(expBits + 1, bin_.Length - (expBits + 1))
        Dim length As Integer = Len(matissa)
        Dim ps As Long = 2
        For x As Integer = 0 To length
            Dim temp As Integer = Val(Mid(matissa, x + 1, 1))
            If temp = 1 Then
                dec += temp / ps
            End If
            ps *= 2
        Next
        dec = _sinal * 2 ^ (a - expAux) * dec
        Return dec
    End Function

相关内容

  • 没有找到相关文章

最新更新