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

0 Members and 1 Guest are viewing this topic.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
A good way to deal with negative values
« 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?
« Last Edit: June 20, 2010, 08:22:48 pm by DJ Omnimaga »

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: A good way to deal with negative values
« Reply #1 on: June 20, 2010, 09:03:49 pm »
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:
Code: [Select]
-7->A
If A<<-5
4->A
End
Disp A>Dec,i
And it displayed "4". What piece of code of yours is failing?
« Last Edit: June 20, 2010, 09:15:33 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: A good way to deal with negative values
« Reply #2 on: June 20, 2010, 09:18:48 pm »
I'm not convinced this comparison code works:
Code: [Select]
xor a
ld b,h
sbc hl,de
ld h,a
rra
xor b
xor d
rlca
and 01
ld l,a
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: A good way to deal with negative values
« Reply #3 on: June 20, 2010, 09:20:16 pm »
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:
Code: [Select]
-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

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: A good way to deal with negative values
« Reply #4 on: June 20, 2010, 09:34:49 pm »
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.
« Last Edit: June 20, 2010, 09:35:31 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: A good way to deal with negative values
« Reply #5 on: June 20, 2010, 09:41:41 pm »
Hmmm i tried both and it worked for me as well.  Interesting bug, what compiler version are you using?

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: A good way to deal with negative values
« Reply #6 on: June 20, 2010, 09:48:33 pm »
DJ, could you tell us the value of A when A<<-5 fails?
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: A good way to deal with negative values
« Reply #7 on: June 20, 2010, 09:54:54 pm »
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

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: A good way to deal with negative values
« Reply #8 on: June 20, 2010, 09:58:12 pm »
Ah, I think you need to use >> in the first If statement instead of >, maybe?
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: A good way to deal with negative values
« Reply #9 on: June 20, 2010, 09:59:05 pm »
really? Because it only checks for positive values in that case

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: A good way to deal with negative values
« Reply #10 on: June 20, 2010, 09:59:15 pm »
The greater than sign should also be signed, like ">>", since -5, 65531 rather, is greater than 5.
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: A good way to deal with negative values
« Reply #11 on: June 20, 2010, 10:00:11 pm »
That's why.  If A=-5, then in unsigned eight bit, that is 133. (I think).

Meaning that a becomes 4.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: A good way to deal with negative values
« Reply #12 on: June 20, 2010, 10:02:16 pm »
@calcdude84se Nope doesn't work either. Same results as if I used >

@graphmastur actually Axe uses 16 bit numbers for such operation.
« Last Edit: June 20, 2010, 10:02:44 pm by DJ Omnimaga »

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: A good way to deal with negative values
« Reply #13 on: June 20, 2010, 10:07:41 pm »
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?
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: A good way to deal with negative values
« Reply #14 on: June 20, 2010, 10:15:18 pm »
it seems to do. They are set to 4.

So what would be my solution, then? It seems rather confusing...