Search code examples
vb.netsmallbasic

Is it possible to make a Double Float Value?


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)


Solution

  • 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