1

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


So, is there any way to make a double non-integral (decimal) precision float?
The code (I've tried) are:

Small Basic:
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)

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Timothy
  • 364
  • 1
  • 12
  • 24

2 Answers2

3

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

J...
  • 30,968
  • 6
  • 66
  • 143
  • Thanks. But is there any way to convert from a Double Integer to a float? ie. When I reach the maximum value for a Double Integer, can I then turn it into a float? – Timothy Jun 12 '14 at 23:05
  • @aytimothy If you absolutely need to work with such large numbers then consider having a look at the `BigInteger` structure : http://msdn.microsoft.com/en-us/library/system.numerics.biginteger%28v=vs.100%29.aspx Consider, though, that if you are intending to push these values into a `double` then it's essentially a waste - just re-write your integers in exponential format (as above) rather than expressly writing out all the digits. I highly recommend you read that link at the end, also. Otherwise, there are several other "Big Integer" libraries out there - search and you'll find them. – J... Jun 12 '14 at 23:12
  • Strange... I can't seem to apply the exponential... Like 1.8446744073709551615E+19 is treated as the number 19 added to 1.8446744073709551615E. – Timothy Jun 14 '14 at 07:53
  • @aytimothy I didn't realize you are 15 years old. The link above about floating point arithmetic is probably much too advanced. For now, I suggest you read this : http://en.wikipedia.org/wiki/Scientific_notation -- Pay particular attention to the section on `E Notation`. It seems that you are not familiar with scientific notation. Usually this is taught around 8th grade or so. See also https://www.khanacademy.org/math/cc-eighth-grade-math/cc-8th-numbers-operations/cc-8th-scientific-notation/v/scientific-notation--old – J... Jun 14 '14 at 10:08
0

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