问题:是否可以在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
<小时/> 那么,有没有办法制作双非整数(十进制)精度浮点数?
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
我搞砸了什么?
我也不知道哪个标签真的适合这个...(除了小基本标签)
答案 0 :(得分:3)
这不起作用的原因是因为您有效地为编译器提供了Integer
格式的常量,并且编译器正在尝试将您的巨大整数转换为浮点值。这会失败,因为您的&gt; 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
失败。
进一步阅读
What Every Computer Scientist Should Know About Floating-Point Arithmetic
答案 1 :(得分:0)
如果用于自动化,请尝试以下操作:
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