Darwinbots Forum
Code center => Darwinbots Program Source Code => Topic started 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)
-
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.
-
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)
-
I can change it no prob. That's just how the VB sgn function works.
-
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)
-
So wait, in DB currently, 0 / 0 = 1 ?
-
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
-
(hint) Make it exactly like in vb:
+n -----> +1
0 -----> 0
-n ------> -1
-
So, a gene like
cond
start
0 0 div
end
would crash the program? Or there are safeguards?
-
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:
'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
-
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
-
'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
-
'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
-
Endy, they are lieing about that "Sgn()" function , I have just porved it. :D , no really: how come no one replyed?
-
Whatever the case, I can add a sepcial case to return 0 for 0.
-
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.
-
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
-
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!
-
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.
-
'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?
-
Hey, you're the one finding 'bugs', not me. If according to your tests no bugs exist, then problem solved!
-
yea but my output is "1; 0; -1" <---------
its not: "1; 1; -1" right?
-
Again, I don't see the problem.
-
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...
-
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.
-
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?)
-
I think Nums is saying he fixed it. Not real sure would have to see new 3.16Beta vers to check.
Endy B)