PDA

View Full Version : how to store (and do arithmetic) and recall/display unsigned 64-bit (8-byte) integer?



DirectuX
22-11-2018, 12:32
Hi,
how to store (and do arithmetic) and recall/display unsigned 64-bit (8-byte) integer?
according to msdn it's 0 to 18,446,744,073,709,551,615
alias uint64 windows data type
how do you do this in thinBasic ?



Internally, a VARIANT is always 16-bytes in size
%VT_R8 Double
%VT_UI8 Quad (unsigned)
Is this info contradictory ? I didn't understand it.


I wrote this code 9910 to experiment data types , variant in particular,
there's even one case where the variant return value is different from input:



uses "console"

' how to store (and do arithmetic) and recall/display unsigned 64-bit (8-byte) integer?
' 0 to 18,446,744,073,709,551,615
' alias uint64 windows data type

'From thinbasic help :

'Internally, a VARIANT is always 16-bytes in size
' %VT_R8 Double
' %VT_UI8 Quad (unsigned)

Dim var_01 as Number value = 876543210987654321
Dim var_02 as Ext value = 876543210987654321
dim var_03 as Quad value = 876543210987654321
dim var_04 as Variant value = 876543210987654321

const Digits value = 18' Specifies the maximum number of significant digits (1 to 18) desired in the result.

Printl "For the value 876543210987654321 which is 18 significant digits," & $crlf & "it seems to be the limit with str$"
printl ""

display(str$(var_01,Digits),"Dim var_01 as Number")
display(str$(var_02,Digits),"Dim var_02 as Ext")
display(str$(var_03,Digits),"Dim var_03 as Quad")

printl ""
printl string$(20,"*") & "press a key to continue" & string$(20,"*")
printl ""
waitkey

printl ""
printl "Internally, a VARIANT is always 16-bytes in size"
printl "%VT_R8 means Double"
printl "%VT_UI8 means Quad (unsigned)"
printl ""

display(str$(var_04,Digits),"dim var_04 as Variant" & " displayed via str$," & $crlf & "internally stored as " & VARIANTVT$(var_04) & " !!! Notice value change !!!")
display(VARIANT$(var_04),"dim var_04 as Variant" & " displayed via VARIANT$," & $crlf & "internally stored as " & VARIANTVT$(var_04))
display(var_04,"dim var_04 as Variant" & " displayed directly," & $crlf & "internally stored as " & VARIANTVT$(var_04))

printl ""
printl ""
printl ""

printl ""
printl string$(20,"*") & "press a key to continue" & string$(20,"*")
printl ""
waitkey

Printl "For the value 18446744073709551615 which is 20 significant digits," & $crlf & "the maximum number for uint64 according to msdn"
printl ""

Dim var_05 as Number value = 18446744073709551615
Dim var_06 as Ext value = 18446744073709551615
dim var_07 as Quad value = 18446744073709551615
dim var_08 as Variant value = 18446744073709551615

display(str$(var_05,Digits),"Dim var_05 as Number")
display(str$(var_06,Digits),"Dim var_06 as Ext")
display(str$(var_07,Digits),"Dim var_07 as Quad")

printl ""
printl string$(20,"*") & "press a key to continue" & string$(20,"*")
printl ""
waitkey

display(str$(var_08,Digits),"dim var_08 as Variant" & " displayed via str$," & $crlf & "internally stored as " & VARIANTVT$(var_04))
display(VARIANT$(var_08),"dim var_08 as Variant" & " displayed via VARIANT$," & $crlf & "internally stored as " & VARIANTVT$(var_04))
display(var_08,"dim var_08 as Variant" & " displayed directly," & $crlf & "internally stored as " & VARIANTVT$(var_04))

function display(what as string, varname as string)
printl (string$(30,"-"))
printl (" " & varname)
Printl "string = " & what
Printl "length = " & len(what)
end function

printl ""
printl string$(20,"*") & "press a key to exit" & string$(20,"*")
printl ""
waitkey


I can't achieve to make a variant store data as %VT_UI8 .

Help ?

DirectuX
22-11-2018, 19:28
Meanwhile on thinBasic,
I've found this https://www.thinbasic.com/community/showthread.php?11062-BigInt-latest-release, but this is overkill.
How an 8-byte integer can't be hold by a 16-byte variant ?

any info about %VT_UI8 Quad (unsigned) ?

Petr Schreiber
06-12-2018, 20:39
Hi Directux,

ThinBASIC currently supports just signed 64bit integers, their range should be -9.22x10^18 - 9.22x10^18.

May I ask for your use case for such a huge numbers? Just curious.


Petr

DirectuX
07-12-2018, 15:53
ThinBASIC currently supports just signed 64bit integers



but 16-byte variant ? It doesn't fit in my logic. :confused:




their range should be -9.22x10^18 - 9.22x10^18.



I'm not comfortable enough to think out a workaround by myself right now, do you know one ?




May I ask for your use case for such a huge numbers? Just curious.



Sure, and the useful answer is here:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa394515(v=vs.85).aspx ; Originally Eros Olmi provided this link. (https://www.thinbasic.com/community/showthread.php?12887-Retrieve-Volume(s)-GUID(s)-in-windows&p=94531&viewfull=1#post94531)

More personal answer : I'm writing a utility program for which i should compute informations about disks; Several properties are provided by Windows as uint64.