64-bit system or 62-bit system?

Just starting out? Need help? Post your questions and find answers here.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

64-bit system or 62-bit system?

Post by charvista »

Hello friends,
I have tested some extremes, and found out that the Windows 7's 64-bit system is in fact a true 62-bit system....
The following program shows why.

Code: Select all

For i=1 To 64
    Debug "2 ^ "+Str(i)+"  =  "+Str(Pow(2,i))
Next
Apparently, it cannot make calculation safely higher than 2^62 as 63 and 64 are returning negative values when it had still to be positive. I am not sure why. I know that there is an extra bit for the sign.
Even the PureBasic manual states clearly that 64-bit-Integers and Quads have a range from -9223372036854775808 to +9223372036854775807.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: 64-bit system or 62-bit system?

Post by netmaestro »

Pow() returns a double, with smaller limits.
BERESHEIT
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: 64-bit system or 62-bit system?

Post by Arctic Fox »

Code: Select all

a.q = 1

For i = 1 To 64
  a * 2
  Debug "2^" + Str(i) + ": " + Str(a)
Next i
Quad has a range from (-2^63) to (2^63 - 1). Note the -1.
For each multiplication by 2, the bits of the number are shifted one place to the left.

Thus 2^63 returns a negative number (and 2^64 returns zero).


Edit
@Charvista
In addition to netmaestro's reply, if you use StrD in your example, it returns positive numbers for 2^63 and 2^64, but it loses some significant digits in the end of the numbers (one of the 'side effects' of floating point numbers).

Code: Select all

For i=1 To 64
  Debug "2 ^ "+Str(i)+"  =  "+StrD(Pow(2,i))
Next
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: 64-bit system or 62-bit system?

Post by wilbert »

Arctic Fox wrote:if you use StrD in your example, it returns positive numbers for 2^63 and 2^64, but it loses some significant digits in the end of the numbers (one of the 'side effects' of floating point numbers).
It's true doubles have a limited accuracy. In this example however the results should be accurate.
A double consists of a sign bit, 11 bits for the exponent and 52 bits for the fraction.
In case of powers of 2, the fraction part isn't used, only the exponent. So even 2^1000 should be accurate.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: 64-bit system or 62-bit system?

Post by charvista »

I would like to thank you all for the explanations. I thought it was a kind of bug but now I understand. I have seen that accuracy is guaranteed up to 15 significant digits... So I'll keep that. It is very rare that we need such huge numbers, anyway... But when we need them, then better have them accurate... :wink:
Cheers
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Post Reply