Author Topic: A good way to deal with negative values  (Read 6545 times)

0 Members and 1 Guest are viewing this topic.

Offline SourceCoder2

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 13
  • Rating: +0/-0
    • View Profile
    • SourceCoder 2.5
Re: A good way to deal with negative values
« Reply #15 on: June 20, 2010, 11:45:57 pm »
HH
KermMartian has just edited this program.  The source code now reads:
Quote from: BASIC Code
:.SSS
:‾5→A
:If A>5
:4→A
:End
:If A<<‾5
:‾4→A
:End
:A→Ans
Generated by SourceCoder, © 2005-2010 Cemetech
This is an automatic post from SourceCoder 2.  Report abuse to [email protected] . You can disable these posts by unchecking the "Post on Update" box in the file's permissions.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: A good way to deal with negative values
« Reply #16 on: June 21, 2010, 01:01:06 am »
-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.
Code: [Select]
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)
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55939
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: A good way to deal with negative values
« Reply #17 on: June 21, 2010, 01:09:23 am »
It outputs 1 for the first and 0 for the second
Dream of Omnimaga

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: A good way to deal with negative values
« Reply #18 on: June 21, 2010, 01:27:46 am »
Which is exactly what it should... am I missing something?
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55939
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: A good way to deal with negative values
« Reply #19 on: June 21, 2010, 01:29:46 am »
Yeah. So this means there is something wrong in my code, but I am not too sure what checks to do x.x
Dream of Omnimaga