Output not the same, bug?

General FreeBASIC programming questions.
Post Reply
Provoni
Posts: 513
Joined: Jan 05, 2014 12:33
Location: Belgium

Output not the same, bug?

Post by Provoni »

Hey,

In the following code, output from example 1 and 2 should (I think) be the same but it is not. Is this a bug or am I missing something?

Tried with GAS and GCC.

Thanks

Code: Select all

screenres 640,480,32

declare function logbx(byval n as double,byval bx as double)as double

dim as integer j,l=100,steps=2
dim as double entropymax1

'output from example 1 and 2 should be the same but is not?

'---------------------------------------------
'example 1
for j=1 to l-steps
	entropymax1+=logbx(1/(l-steps),2)*(1/(l-steps))
next j
entropymax1=abs(entropymax1)
print entropymax1
'---------------------------------------------
'example 2
entropymax1=(l-steps)*abs(logbx(1/(l-steps),2)*(1/(l-steps)))
print entropymax1
'---------------------------------------------

sleep

function logbx(byval n as double,byval bx as double)as double
	
   return log(n)/log(bx)
   
end function
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Output not the same, bug?

Post by SARG »

I guess just due to rounding.
If you try with a smallest value of L (25) no difference.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Output not the same, bug?

Post by caseih »

They are the same, though---the same to the 14th decimal place in fact! That's incredibly good for repeated summations of IEEE floating point numbers! I suspect if you did the same calculation by hand you'd get the same results.

Always remember that floating point numbers are binary fractions and thus subject to certain limitations and rounding errors, and also remember that in any kind of calculation like that, significant digits is important. How many significant digits do you need?
Provoni
Posts: 513
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Output not the same, bug?

Post by Provoni »

I don't think this is a rounding error problem, consider the following code, now example 1 and 2 are the same, but unremark "print t1" from example 1 and notice that the values are different again. How is the "print t1" here affecting the calculation?

I'm not sure how many digits my algorithm needs but something like 12 digits should probably be good.

Code: Select all

screenres 640,480,32

declare function logbx(byval n as double,byval bx as double)as double

dim as integer j,l=100,steps=2
dim as double entropymax1
dim as double t1=abs(logbx(1/(l-steps),2)*(1/(l-steps)))

'output from example 1 and 2 should be the same but is not?

'---------------------------------------------
'example 1
for j=1 to l-steps
	'print t1 '<----------------------------------------
	entropymax1+=t1
next j
entropymax1=abs(entropymax1)
print entropymax1
'---------------------------------------------
'example 2
entropymax1=(l-steps)*abs(logbx(1/(l-steps),2)*(1/(l-steps)))
print entropymax1
'print abs(logbx(1/(l-steps),2)*(1/(l-steps)))
'---------------------------------------------

sleep

function logbx(byval n as double,byval bx as double)as double
	
   return log(n)/log(bx)
   
end function
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Output not the same, bug?

Post by dodicat »

Here is the gmp answer as a check:
6.6147098441152082148839386344636616172820532519322815673545834481406416977243859729957219983404215702147210037786511460
i.e.
(l-steps)*abs(logbx(1/(l-steps),2)*(1/(l-steps)))
and the loop:
dim as string t2=mult(d,logbx(d,"2"))
for j=1 to l-steps
entropymax1=plus(entropymax1,t2)
next j
entropymax1=absolute(entropymax1)
print entropymax1
6.6147098441152082148839386344636616172820532519322815673545834481406416977243859729957219983404215702147210037786511460
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Output not the same, bug?

Post by SARG »

@provoni,
With ou without the print I get the same different values

Code: Select all

gas32 without print

D:\fb32bit>test3.exe
 6.614709844115218
 6.614709844115208

Code: Select all

with print

 0.0674970392256654
 0.0674970392256654
 6.614709844115218
 6.614709844115208
Using 64bit the last print is very slighty different (last digit)

Code: Select all

 0.0674970392256654
 0.0674970392256654
 6.614709844115218
 6.614709844115209
Provoni
Posts: 513
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Output not the same, bug?

Post by Provoni »

I have 2 very different machines, a Ryzen 4800H laptop and a dual Xeon 2680v2 workstation, both give the same results.

When "print t1" is commented, the numbers are the same, ending both in 209. When "print t1" is uncommented the numbers are different, 218 and 209.

64-bit GAS and GCC.
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Output not the same, bug?

Post by SARG »

Looking at the asm code I didn't see any reason that explains the different results.

On what OS (Windows/Linux) ?

Could you upload somewhere the 2 versions (w/wo print) compiled with gas64. I'll test on my machine, maybe CPUs working differently....
Provoni
Posts: 513
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Output not the same, bug?

Post by Provoni »

There was a issue with my GAS setup. The output is the same now for GAS. GCC still wrong but I guess it's a GCC thing.

Thanks SARG
Post Reply