My environment:

FreeBASIC Compiler - Version 1.06.0 (11-29-2017), built for win64 (64bit)

Windows 10 64-bit

Division, "/", results are hit or miss - no consistency.

INT(x / ?), (the floor function), always returns a consistent answer for any divisor.

SHR always returns a consistent answer when using it to divide by multiples of 2.

Code: Select all

`'18446744073709551610 ' Given x as this ULONGINT...`

'--------------------

'9223372036854775808 ' FB x/2 = ( __WRONG__ ? answer )

'9223372036854775805 ' C++ x/2 = ( Correct ? answer )

'9223372036854775805 ' FB INT(x/2) = ( Correct ? answer )

'9223372036854775805 ' FB x SHR 1 = ( Correct ? answer )

dim x as ULongInt

dim r as ULongInt

dim i as ULongInt

i = 4575728998270 'Random medium-size ULONGINT value

for x = i to i+9

print "---------------------------"

print "x = "; x

print "---------------------------"

r = x / 2

print "x / 2 = "; r

r = int(x / 2)

print "int(x / 2) = "; r

r = x shr 1

print "x SHR 1 = "; r

print "--------"

r = x / 4

print "x / 4 = "; r

r = int(x / 4)

print "int(x / 4) = "; r

r = x shr 2

print "x SHR 2 = "; r

print "--------"

r = x / 3

print "x / 3 = "; r

r = int(x / 3)

print "int(x / 3) = "; r

print

next

I ran up against a wall when trying to port the PCG PRNG's advance/skip/jump functionality

to FB. I've looked at it on/off over the last few months and just now figured out that the

issue is that FB's "/" appears to be rounding differently and is not returning results consistent

with C++'s "/" when used with QWORDS (i.e. ULONGINT, uint64_t, ull, unsignedlonglong, whatever).

Does anyone know if this is a feature, or a bug, or just a difference that I will have to

always work around by using INT(ULONGINT / x) in the future?

Code: Select all

`/'`

Test output:

I've set a "?" next to the results that are in question.

D:\>Test_ULONGINT_DIV_error.exe

---------------------------

x = 4575728998270

---------------------------

x / 2 = 2287864499135

int(x / 2) = 2287864499135

x SHR 1 = 2287864499135

--------

x / 4 = 1143932249568 ?

int(x / 4) = 1143932249567

x SHR 2 = 1143932249567

--------

x / 3 = 1525242999423

int(x / 3) = 1525242999423

---------------------------

x = 4575728998271

---------------------------

x / 2 = 2287864499136 ?

int(x / 2) = 2287864499135

x SHR 1 = 2287864499135

--------

x / 4 = 1143932249568 ?

int(x / 4) = 1143932249567

x SHR 2 = 1143932249567

--------

x / 3 = 1525242999424 ?

int(x / 3) = 1525242999423

---------------------------

x = 4575728998272

---------------------------

x / 2 = 2287864499136

int(x / 2) = 2287864499136

x SHR 1 = 2287864499136

--------

x / 4 = 1143932249568

int(x / 4) = 1143932249568

x SHR 2 = 1143932249568

--------

x / 3 = 1525242999424

int(x / 3) = 1525242999424

---------------------------

x = 4575728998273

---------------------------

x / 2 = 2287864499136

int(x / 2) = 2287864499136

x SHR 1 = 2287864499136

--------

x / 4 = 1143932249568

int(x / 4) = 1143932249568

x SHR 2 = 1143932249568

--------

x / 3 = 1525242999424

int(x / 3) = 1525242999424

---------------------------

x = 4575728998274

---------------------------

x / 2 = 2287864499137

int(x / 2) = 2287864499137

x SHR 1 = 2287864499137

--------

x / 4 = 1143932249568

int(x / 4) = 1143932249568

x SHR 2 = 1143932249568

--------

x / 3 = 1525242999425 ?

int(x / 3) = 1525242999424

---------------------------

x = 4575728998275

---------------------------

x / 2 = 2287864499138 ?

int(x / 2) = 2287864499137

x SHR 1 = 2287864499137

--------

x / 4 = 1143932249569 ?

int(x / 4) = 1143932249568

x SHR 2 = 1143932249568

--------

x / 3 = 1525242999425

int(x / 3) = 1525242999425

---------------------------

x = 4575728998276

---------------------------

x / 2 = 2287864499138

int(x / 2) = 2287864499138

x SHR 1 = 2287864499138

--------

x / 4 = 1143932249569

int(x / 4) = 1143932249569

x SHR 2 = 1143932249569

--------

x / 3 = 1525242999425

int(x / 3) = 1525242999425

---------------------------

x = 4575728998277

---------------------------

x / 2 = 2287864499138

int(x / 2) = 2287864499138

x SHR 1 = 2287864499138

--------

x / 4 = 1143932249569

int(x / 4) = 1143932249569

x SHR 2 = 1143932249569

--------

x / 3 = 1525242999426 ?

int(x / 3) = 1525242999425

---------------------------

x = 4575728998278

---------------------------

x / 2 = 2287864499139

int(x / 2) = 2287864499139

x SHR 1 = 2287864499139

--------

x / 4 = 1143932249570 ?

int(x / 4) = 1143932249569

x SHR 2 = 1143932249569

--------

x / 3 = 1525242999426

int(x / 3) = 1525242999426

---------------------------

x = 4575728998279

---------------------------

x / 2 = 2287864499140 ?

int(x / 2) = 2287864499139

x SHR 1 = 2287864499139

--------

x / 4 = 1143932249570 ?

int(x / 4) = 1143932249569

x SHR 2 = 1143932249569

--------

x / 3 = 1525242999426

int(x / 3) = 1525242999426

'/

Thanks,

CBruce