Reputation: 383
Question: Is it possible to make a Double Float Value in Small and Visual Basic?
I've been trying to create a double float value in Small/Visual Basic (as in BOTH of them)...
And I've always been not having any luck.. I always terminate with an error like so:
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()
Or, running in Visual Basic:
overflow
var1 = 18446744073709551615
var2 = 1797693134862315800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
(Yeah, sorry about the number of zeros.. It's 303 of them.)
and in Visual Basic:
Module experiment_doesDoubleFloat_workModule
Dim var1, var2 As Double
Sub Main()
var1 = 18446744073709551615
var2 = 1797693134862315800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
End Sub
End Module
Am I messing something up?
I also have no idea which tags actually fit this... (Apart from the smallbasic tag)
Upvotes: 1
Views: 2022
Reputation: 11
if it is for automation try this:
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
Upvotes: 0
Reputation: 31433
The reason this does not work is because you are effectively giving the compiler a constant in Integer
format and the compiler is trying to convert your enormous integer into a floating point value. This fails because your >300 digit value is larger than the largest integer that fits into any standard data type.
If you want to assign a constant value in your code it must be in a format that the compiler can parse, ie :
var1 = 1.8446744073709552E+19
var2 = 1.7976931348623157E+308
In fact, you'll notice that when you type :
var1 = 1.8446744073709551615E+19
that the value is automatically converted to:
var1 = 1.8446744073709552E+19
since the original value contains more precision than the double
format can accomodate. Also, I didn't count the number of zeroes in your code sample, but if it is 303 of them, then this makes the value of var2 = 1.797... E+319
, which is also too large for a double
. With 292 zeroes the value becomes ...E+308
, as above, which is the largest representable double-precision floating point value.
Note that there is no problem assigning integer constants to a floating point variable so long as the value in your code is small enough to fit into an Integer
. See that :
var1 = 9223372036854775807 ' << largest Int64
compiles fine, but one more
var1 = 9223372036854775808
fails.
For further reading
What Every Computer Scientist Should Know About Floating-Point Arithmetic
Upvotes: 3