Omnimaga
Calculator Community => TI Calculators => Axe => Topic started by: DJ Omnimaga on June 20, 2010, 08:14:44 pm
-
Ok I wonder something about signed integers...
How would you do the following TI-BASIC command into Axe?
If A<-5
4→A
Of course the following will fail, because it will check if it's smaller than 65530:
If A<-5
4->A
End
But the following doesn't appear to work either:
If A<<-5
4->A
End
Any hint?
-
It doesn't work? Hmm... I mean, it'd fail if A wrapped around to positive, but that's in the 30 thousands.
I'll try some code and edit/post.
Edit: I just tried this code:
-7->A
If A<<-5
4->A
End
Disp A>Dec,i
And it displayed "4". What piece of code of yours is failing?
-
I'm not convinced this comparison code works:
xor a
ld b,h
sbc hl,de
ld h,a
rra
xor b
xor d
rlca
and 01
ld l,a
-
It doesn't work? Hmm... I mean, it'd fail if A wrapped around to positive, but that's in the 30 thousands.
I'll try some code and edit/post.
Edit: I just tried this code:
-7->A
If A<<-5
4->A
End
Disp A>Dec,i
And it displayed "4". What piece of code of yours is failing?
both
-
As in, my code and yours? What compiler version are you using?
calc84: I never liked routines optimized so much they became obfuscated, and this is one of them. I'll look at it in more detail, though.
Edit: Just for clarification, I know that routine is part of Axe, not calc84's.
-
Hmmm i tried both and it worked for me as well. Interesting bug, what compiler version are you using?
-
DJ, could you tell us the value of A when A<<-5 fails?
-
woops, I forgot to mention that before this code, I also check If A>5. In that case I store 4 to A.
Example of code if I use <<:
http://sc.cemetech.net/?xpi=54e3a005bb4cb1ffcf69f02825479247
If I store anything that is below zero to A before the two if statements, it's set to 4, as if the variable was greater than 5.
If I use <, then the variable is set to 65532 regardless of its initial value
Sorry for the confusion
-
Ah, I think you need to use >> in the first If statement instead of >, maybe?
-
really? Because it only checks for positive values in that case
-
The greater than sign should also be signed, like ">>", since -5, 65531 rather, is greater than 5.
-
That's why. If A=-5, then in unsigned eight bit, that is 133. (I think).
Meaning that a becomes 4.
-
@calcdude84se Nope doesn't work either. Same results as if I used >
@graphmastur actually Axe uses 16 bit numbers for such operation.
-
Well, 65532 is -4, so that means that A<<-5 is always true... which means A>>5 is always false (because 4<<-5 is 0), which means you're always trying values between -32768 and 5. Do other numbers outside of this range work?
-
it seems to do. They are set to 4.
So what would be my solution, then? It seems rather confusing...
-
HH
KermMartian has just edited this program. The source code now reads:
:.SSS
:‾5→A
:If A>5
:4→A
:End
:If A<<‾5
:‾4→A
:End
:A→Ans
Generated by SourceCoder (http://sc.cemetech.net), © 2005-2010 Cemetech (http://www.cemetech.net)
This is an automatic post from SourceCoder 2 (http://sc.cemetech.net). Report abuse to [email protected] . You can disable these posts by unchecking the "Post on Update" box in the file's permissions.
-
-5>5 is true since this is unsigned so A should be 4 now
4<<-5 is false since this is signed (like basic) so A is still 4
Is it outputting something else?
Unless my understanding of how signed comparisons work, the routine should always work.
xor a ;Make A zero
ld b,h ;H is saved for later
sbc hl,de ;The values are compared unsigned (carry flag)
ld h,a ;Command returns 1 or 0 so high byte is zeroed
rra ;Carry is put into bit 7 of a
xor b ;Bit 7 is xored with sign bit of original left argument
xor d ;Bit 7 is xored with sign bit of original right argument
rlca ;Bit 7 put into bit 0
and %00000001 ;Mask to make this a 1 or 0
ld l,a ;HL holds (sign1 xor sign2 xor comparison)
-
It outputs 1 for the first and 0 for the second
-
Which is exactly what it should... am I missing something?
-
Yeah. So this means there is something wrong in my code, but I am not too sure what checks to do x.x