Darwinbots Forum

Code center => Darwinbots Program Source Code => Topic started by: Endy on March 07, 2005, 05:01:29 PM

Title: Questions on Ceil and Floor
Post by: Endy on March 07, 2005, 05:01:29 PM
Thought of this code while converting Din, but can't seem to make it work correctly. :unsure:

A B sub sgn 0 floor  = 1 if A>B

A B sub sgn 0 ceil -1 mult = 1 if A<B

May be the dna itself, I've tried to check it but the sheer size of it is overwhelming right now. I suspect the ceil and floor commands are responsible. Possibly the new free store ability is causing some stack effects, and messing up the genes as a result but I would think Nums would have the values still be cleared from the stack.

Any help would be great,

Endy B)
Title: Questions on Ceil and Floor
Post by: Numsgil on March 07, 2005, 05:15:40 PM
How exactly is it not working?

Note that A B sub sgn will return 1 if A = B since 0 sgn = 1

The free store still takes two values from the stack.
Title: Questions on Ceil and Floor
Post by: Endy on March 07, 2005, 10:31:06 PM
Oops. :rolleyes:

Didn't realize that about sgn, I assumed that it would yield a zero since 0 is neutral. Darn DB math for not reflecting real math. :lol:

Endy B)
Title: Questions on Ceil and Floor
Post by: Numsgil on March 08, 2005, 12:21:32 AM
I can change it no prob.  That's just how the VB sgn function works.
Title: Questions on Ceil and Floor
Post by: Endy on March 08, 2005, 09:24:27 PM
I think it would be better if it was changed. I mainly use it for comparision between the two numbers, for stuff like greaterthan/lessthan and =/!=. With DB's 0/0 = 0 it is easier than normal to use the resulting zeros.

Endy B)
Title: Questions on Ceil and Floor
Post by: SyndLig on March 09, 2005, 11:19:26 AM
So wait, in DB currently, 0 / 0 = 1 ?
Title: Questions on Ceil and Floor
Post by: PurpleYouko on March 09, 2005, 11:25:54 AM
No Actually in DB 0/0 = "Division by zero error"

It is just that 0 is considered a positive number in Visual Basic.

SGN is Boolean. It can only have two states.

 :D  PY  :D
Title: Questions on Ceil and Floor
Post by: Botsareus on March 09, 2005, 02:22:58 PM
(hint) Make it exactly like in vb:

+n -----> +1
0   -----> 0
-n ------> -1
Title: Questions on Ceil and Floor
Post by: shvarz on March 09, 2005, 03:40:29 PM
So, a gene like

cond
start
0 0 div
end

would crash the program?  Or there are safeguards?
Title: Questions on Ceil and Floor
Post by: Numsgil on March 09, 2005, 04:12:21 PM
0 0 div is undefine behavior.  Currently DB returns 0 for this.

And the VB sgn function works like this:

0 and + sgn = 1
- sng = -1

proof?

here's the code:

Code: [Select]
'This one could be very useful
Private Sub DNASgn(robotnumber As Integer) 'returns sign of number on stack
    Dim a As Integer
    a = readst(robotnumber)
    
    writest robotnumber, Sgn(a)
End Sub
Title: Questions on Ceil and Floor
Post by: PurpleYouko on March 09, 2005, 09:36:25 PM
As I said, in Visual Basic, SGN returns a Boolean state result with two possible outcomes. +1 or -1.

It cannot return zero unless you write a whole bunch of code to take the input of zero as a special case..

And yes there are safeguards against dividing by zero in DarwinBots.

 :D  PY  :D
Title: Questions on Ceil and Floor
Post by: Botsareus on March 10, 2005, 01:11:03 PM
'Microsoft Visual Basic 6.0
'Form1, no tricks
Private Sub Form_Load()
MsgBox Sgn(700) 'My vb is messed up it returns 1
MsgBox Sgn(0) 'My vb is messed up it returns 0
MsgBox Sgn(-700) 'My vb is messed up it returns -1
End Sub
'Enterpise Edition
Title: Questions on Ceil and Floor
Post by: Botsareus on March 10, 2005, 01:14:46 PM
'This one could be very useful , once you figure out the rest of the code (hint)
Private Sub DNASgn(robotnumber As Integer) 'returns sign of number on stack
   Dim a As Integer
   a = readst(robotnumber)
   
   writest robotnumber, Sgn(a)
End Sub

Private Sub Form_Load()
MsgBox DNASgn(4) 'Good Luck with that, I dont have enough code to make it work
End Sub
Title: Questions on Ceil and Floor
Post by: Botsareus on March 11, 2005, 03:26:39 PM
Endy, they are lieing about that "Sgn()" function , I have just porved it. :D , no really: how come no one replyed?
Title: Questions on Ceil and Floor
Post by: Numsgil on March 11, 2005, 06:15:04 PM
Whatever the case, I can add a sepcial case to return 0 for 0.
Title: Questions on Ceil and Floor
Post by: Botsareus on March 11, 2005, 08:38:07 PM
Num, why fix a bug by adding code when you can fix it by changing code? I believe there is a bug in the vb code you posted. I also believe there is no bug in the code I posted.

The problem is I dont have the rest of your code , thats why I cant pinpoint were the bug is.
Title: Questions on Ceil and Floor
Post by: Numsgil on March 11, 2005, 08:47:23 PM
How can there be a bug?

'This one could be very useful
Private Sub DNASgn(robotnumber As Integer) 'returns sign of number on stack
    Dim a As Integer
    a = readst(robotnumber)
   
    writest robotnumber, Sgn(a)
End Sub

"readst" reads the top number of the stack from robotnumber.

"writest" writes a number to the stack of robotnumber.

So where's the bug?  How can there be a bug?  And last, why try to figure it out when you can just add:

if a = 0 then
writest robotnumber, 0
else
writest robotnumber, sgn(a)
end if
Title: Questions on Ceil and Floor
Post by: Botsareus on March 11, 2005, 09:08:41 PM
There is a bug in
"readst" reads the top number of the stack from robotnumber.
or
"writest" writes a number to the stack of robotnumber.

Paste the code for thouse custom  functions, paste the code for the stack itself, Paste the code for all the custom functions used by thouse custom functions, And I will show you were the bug is trust me!
Title: Questions on Ceil and Floor
Post by: Numsgil on March 11, 2005, 09:31:31 PM
A magical bug that only manifests itself in the DNAsgn function, huh?  A bug that has gone magically unnoticed since DB 1.0 was released?

Okay, sarcasm aside, it's not a bug.  I'll post the DNA parsing module so you can look it over if you like, but you're not going to find anything.
Title: Questions on Ceil and Floor
Post by: Botsareus on March 11, 2005, 09:46:51 PM
'boolstack structure used for conditionals
Private Type boolstack
  val(20) As Boolean
  pos As Integer
End Type
Private Const stacklim As Integer = 20
Dim currgene As Long 'for *.thisgene
Dim Condst As boolstack 'for the conditions stack
'/*********I had to gess this
Private Type stackishere
pos As Double
val(255) As Double
End Type
Private Type therobotsarehere
st As stackishere
End Type
Dim rob(20) As therobotsarehere
'\**************

Private Sub DNASgn(robotnumber As Integer) 'returns sign of number on stack
Dim a As Integer
a = readst(robotnumber)

writest robotnumber, Sgn(a)
End Sub

Private Function readst(n As Integer) As Integer
  readst = 0
  If rob(n).st.pos > 0 Then
    readst = rob(n).st.val(rob(n).st.pos)
    rob(n).st.pos = rob(n).st.pos - 1
  End If
End Function

' stack push
Private Sub writest(n As Integer, val)
  If Abs(val) > 32000 Then val = Sgn(val) * 32000
  If rob(n).st.pos < stacklim Then
    rob(n).st.pos = rob(n).st.pos + 1
    rob(n).st.val(rob(n).st.pos) = val
  End If
End Sub

Private Sub Form_Load()
writest 0, 700
DNASgn 0
MsgBox rob(0).st.val(rob(n).st.pos) ' returns 1
'
writest 0, 0
DNASgn 0
MsgBox rob(0).st.val(rob(n).st.pos) ' returns 0
'
writest 0, -700
DNASgn 0
MsgBox rob(0).st.val(rob(n).st.pos) ' returns -1
End Sub
'Even In your own code It returns 0 for 0, rofl , now what?
Title: Questions on Ceil and Floor
Post by: Numsgil on March 11, 2005, 09:56:12 PM
Hey, you're the one finding 'bugs', not me.  If according to your tests no bugs exist, then problem solved!
Title: Questions on Ceil and Floor
Post by: Botsareus on March 11, 2005, 10:03:31 PM
yea but my output is "1; 0; -1" <---------

its not: "1; 1; -1" right?
Title: Questions on Ceil and Floor
Post by: Numsgil on March 11, 2005, 10:12:20 PM
Again, I don't see the problem.
Title: Questions on Ceil and Floor
Post by: Botsareus on March 11, 2005, 10:19:12 PM
if a = 0 then
writest robotnumber, 0
else
writest robotnumber, sgn(a)
end if

The above code is not required because sgn does it automaticaly.
I dont know were Endy, Py, and YOU got the "1 1 -1" from...
Title: Questions on Ceil and Floor
Post by: Numsgil on March 11, 2005, 10:36:18 PM
The sign of zero (http://mathforum.org/library/drmath/view/57215.html).

Basically anything we choose, 1, -1, or 0, are all ligitimate choices for the sign of zero.

Don't know where I got the sign on 0 as 1 from.  Just figured that sgn only returns two values.
Title: Questions on Ceil and Floor
Post by: Botsareus on March 12, 2005, 11:16:30 AM
So whats going on with DB and Endy , what is he complaining about?

(I bet there is a bug somewere that returns the beepin "1 1 -1" , its just not in the code you gave me Num)

(Endy help me out here , the bug still happens?)
Title: Questions on Ceil and Floor
Post by: Endy on March 13, 2005, 12:12:28 AM
I think Nums is saying he fixed it. Not real sure would have to see new 3.16Beta vers to check.

Endy B)