Aside from dumb mistakes in my example, clarifying the problem as:
print (2 ^ 64) - 1 'this works
dim a as uinteger = ((2 ^ 64 ) - 1)
print a ' returns a zero
now
print(2^63)-1:print(2^64)-1'both work for signed integersdim min as integer = ((2 ^ 64) - 1) dim max as integer = (2 ^ 63) - 1)print min; max'thisworks
Does not appear to be handling uinteger declared variables correctly pas 63 bits
root@abaddon:/usr/local/src/basic# fbc --version
FreeBASIC Compiler - Version 1.09.0 (2022-01-01), built for linux-x86_64 (64bit)
Copyright (C) 2004-2021 The FreeBASIC development team.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Not a bug. The ^ operator returns a double, as your own examples show. Double precision floats have only 53 bits of precision. They can't actually store the number 2 ^ 64 - 1!
If you look more closely at your examples, the lines you say "work" actually do not. print max prints -9223372036854775808 instead of the correct value 9223372036854775807.
The solution is to write 1 shl 63 instead of 2 ^ 63. However! You can't write 1 shl 64 because on x86 CPUs (but IIRC not ARM) that's equivalent to 1 shl 0 which is equal to 1 instead of the expected result, 0. Thankfully fbc will warn you about the overflow.
Last edit: TeeEmCee 2022-07-22
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Aside from dumb mistakes in my example, clarifying the problem as:
now
Does not appear to be handling uinteger declared variables correctly pas 63 bits
root@abaddon:/usr/local/src/basic# fbc --version
FreeBASIC Compiler - Version 1.09.0 (2022-01-01), built for linux-x86_64 (64bit)
Copyright (C) 2004-2021 The FreeBASIC development team.
Not a bug. The ^ operator returns a double, as your own examples show. Double precision floats have only 53 bits of precision. They can't actually store the number
2 ^ 64 - 1!If you look more closely at your examples, the lines you say "work" actually do not.
print maxprints -9223372036854775808 instead of the correct value 9223372036854775807.The solution is to write
1 shl 63instead of2 ^ 63. However! You can't write1 shl 64because on x86 CPUs (but IIRC not ARM) that's equivalent to1 shl 0which is equal to 1 instead of the expected result, 0. Thankfully fbc will warn you about the overflow.Last edit: TeeEmCee 2022-07-22
I have reviewed and reworded some notes about this in the documentation.
https://www.freebasic.net/wiki/KeyPgOpExponentiate