Problem adding Ulongint numbers

New to FreeBASIC? Post your questions here.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Problem adding Ulongint numbers

Post by Luis Babboni »

Look at this!
I need it for the bitboard version of my chess engine.
Image

I think I saw something similar to this before and I think it was a FreeBasic bug. Could be?
I hope is just a fool thing I´m doing.

Code: Select all

Dim a As ULongInt
a=18049651735527936
Print 2^(0+0)
Print a+1
a=a+2^(0+0)
Print a
Sleep
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Problem adding Ulongint numbers

Post by Luis Babboni »

Here waht I said:

viewtopic.php?f=2&t=25019&p=224080#p224080

It seems a similar bug was in FB versions previous from 1.04.
Now i´m using the FreeBASIC-1.07.1-win64 version
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Problem adding Ulongint numbers

Post by badidea »

I cannot test at the moment, but is ^ a floating point operation that introduces a rounding error?
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Problem adding Ulongint numbers

Post by Luis Babboni »

badidea wrote:I cannot test at the moment, but is ^ a floating point operation that introduces a rounding error?
Mmmm, I cant say. Im working with integers.... or not?
What i could use instead of "^"?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Problem adding Ulongint numbers

Post by MrSwiss »

Luis Babboni wrote:Look at this! I hope is just a fool thing I´m doing.
Well, the 2^(0+0) part of code looks definitly weird (makes no sense).
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Problem adding Ulongint numbers

Post by Luis Babboni »

MrSwiss wrote:
Luis Babboni wrote:Look at this! I hope is just a fool thing I´m doing.
Well, the 2^(0+0) part of code looks definitly weird (makes no sense).
I show it like this to be simplier to understand.

This is the real code:

Code: Select all

For f=1 To 8
	For c=1 To 8
		fa=f
		ca=c
		While (fa>1 And ca>1)
			fa=fa-1
			ca=ca-1
			AtaquesAlfilesSO(8*(f-1)+(c-1))=AtaquesAlfilesSO(8*(f-1)+(c-1))+2^(8*(fa-1)+(ca-1))
		Wend
		Print 8*(f-1)+(c-1),AtaquesAlfilesSO(8*(f-1)+(c-1))
		Sleep 
	Next
Next
When fa=1 and ca=1 appears the problem.
Last edited by Luis Babboni on Apr 04, 2021 1:25, edited 1 time in total.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Problem adding Ulongint numbers

Post by Luis Babboni »

Introducing another Ulongint variable, pivote, it works right... but Im a little scared :-D

Code: Select all

For f=1 To 8
	For c=1 To 8
		fa=f
		ca=c
		While (fa>1 And ca>1)
			fa=fa-1
			ca=ca-1
			pivote=2^(8*(fa-1)+(ca-1))
			AtaquesAlfilesSO(8*(f-1)+(c-1))=AtaquesAlfilesSO(8*(f-1)+(c-1))+pivote
		Wend
		Print 8*(f-1)+(c-1),AtaquesAlfilesSO(8*(f-1)+(c-1))
		Sleep 
	Next
Next
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Problem adding Ulongint numbers

Post by Luis Babboni »

Is for the diagonal where bishops attacks when is in H8 position in a chessboard:

Image
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Problem adding Ulongint numbers

Post by MrSwiss »

Maybe the additional variable (the assignment to it) solves the possible rounding issue ...

Read Operators/Arithmetic/^ in the FB-documentation.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Problem adding Ulongint numbers

Post by Luis Babboni »

MrSwiss wrote:Maybe the additional variable (the assignment to it) solves the possible rounding issue ...

Read Operators/Arithmetic/^ in the FB-documentation.
"... Note: this operation is not guaranteed to be fully accurate, and there may be some inaccuracy in the least significant bits of the number. This is particularly noticeable when the result is expected to be an exact number: in these cases, you may find the result is out by a very small amount. For this reason, you should never assume that an exponentiation expression will be exactly equal to the value you expect.
This also means that you should be wary of using rounding methods such as Int and Fix on the result: if you expect the result to be an integer value, then there's a chance that it might be slightly lower, and will round down to a value that is one less than you would expect... "

Ooops.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Problem adding Ulongint numbers

Post by Luis Babboni »

Any suggestion to how avoid this problem?
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Problem adding Ulongint numbers

Post by Luis Babboni »

Is this safer for 2^63?
I just need to do it once at the begining of the game, no matter if consumes more time.

Image
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Problem adding Ulongint numbers

Post by caseih »

Should be safe. 2^63 won't overflow a 64-bit value.
Last edited by caseih on Apr 04, 2021 2:09, edited 2 times in total.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Problem adding Ulongint numbers

Post by Luis Babboni »

caseih wrote:Why would it not be safe? Are you concerned about an overflow?
I need the exactly result. A differencie of just 1 means a piece could moves wrongly.
Or do not see a possible move.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Problem adding Ulongint numbers

Post by caseih »

I don't see how you can get more accurate than multiplying integers! Beats the heck out of floating point for accuracy. Short of an overflow it's not possible to be off by one. That said are you always trying to raise 2 to a power? Shouldn't you just shift left rather than multiply? 1 shl 63 is a heck of lot faster than 2^63 (by any method) and does the same thing.
Post Reply