Menu

#967 uinteger (64 bit) seems to be limited at (2^63) for uinteger variables only

closed
nobody
None
compiler
2022-08-07
2022-07-21
No

Confusing behaviour for uniteger, in uniteger variables only, on FBC Linux 64 bit platform

2 Attachments

Discussion

  • Vincent Chapman

    Vincent Chapman - 2022-07-21

    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 integers
    
    dim min as integer = ((2 ^ 64) - 1) 
    dim max as integer = (2 ^ 63) - 1)
    
    print min; max'this works
    

    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.

     
  • TeeEmCee

    TeeEmCee - 2022-07-22

    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!

    var x = 2 ^ 64
    var y = 2 ^ 64 - 1
    dim t as boolean = (x = y)
    print t  'true
    

    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
  • Jeff Marshall

    Jeff Marshall - 2022-08-07

    I have reviewed and reworded some notes about this in the documentation.
    https://www.freebasic.net/wiki/KeyPgOpExponentiate

     
  • Jeff Marshall

    Jeff Marshall - 2022-08-07
    • status: open --> closed
     

Log in to post a comment.