是否可以制作双浮动值?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (67)

问题:是否可以在Small和Visual Basic中创建Double 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

那么,有没有办法制作双非整数(十进制)精度浮点数?

代码(我试过)是:

Small Basic:

var1 = 18446744073709551615
var2 = 1797693134862315800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

(是的,抱歉零的数量......这是其中的303个。) 和在Visual Basic中:

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

我搞砸了什么? 我也不知道哪个标签真的适合这个...(除了小基本标签)

提问于
用户回答回答于

这不起作用的原因是因为您有效地为编译器提供了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,这对于a来说也太大了double...E+308如上所述,对于292个零,该值变为最大可表示的双精度浮点值。

请注意,只要代码中的值足够小以适应浮点变量,就可以将整数常量分配给浮点变量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

扫码关注云+社区

领取腾讯云代金券