Jump to content
IGNORED

IntyBASIC Negative Number Display/Handling?


Recommended Posts

Hey all.

 

From manual.txt:

Using PRINT for numbers always show numbers as unsigned from 0 to 65535

I plan to store values in an array, each value one can be from -99 to 99. For debugging, since I can't PRINT the values on-screen, what is a smart way to watch the values?

 

 

Thanks.

Link to comment
Share on other sites

And if nanochess' approach is too much code, why not just add a linear offset? If it really is in the range -99 to 99, just add 100 to it. You'll have to mentally subtract off the extra 100 when you read off the value, but for debug code, that's not too bad.

 

nanochess' produces more human friendly output, though, and is also worth using for actual signed outputs that aren't just debug outputs.

  • Like 2
Link to comment
Share on other sites

I was wondering about the linear offset, it is "better" for the sake of speed to work in 1 to 199 or -99 to 99? It's premature optimization and all that, but if there is a performance penalty I can dodge I'd like to understand the tradeoffs.

 

Thanks.

 

 

 

And if nanochess' approach is too much code, why not just add a linear offset? If it really is in the range -99 to 99, just add 100 to it. You'll have to mentally subtract off the extra 100 when you read off the value, but for debug code, that's not too bad.

 

nanochess' produces more human friendly output, though, and is also worth using for actual signed outputs that aren't just debug outputs.

Link to comment
Share on other sites

My approach is marginally faster. Mostly, it's a lot smaller (both in source and in executable size), and less intrusive looking. It's perhaps also harder to get wrong. ;)

 

For example, nanochess's THEN and ELSE clauses need to be swapped:

.

Before:


' Note: for use with 8-bit signed values (declare with SIGNED), for 16 bits values use #a
IF a < 0 THEN PRINT <3>,a ELSE PRINT "-",<2>-a


Corrected:


' Note: for use with 8-bit signed values (declare with SIGNED), for 16 bits values use #a
IF a < 0 THEN PRINT "-"<2>,-a ELSE PRINT <3>-a

  • Like 1
Link to comment
Share on other sites

If you want the absolute value, how would you come up with a formula for a variable in the range of 1-199 where 100 equals the mid point? I tried for a while and came up with a%100 as part of the solution but then one needs to reverse the numbers for one half of the range so the absolute value would be 99, 98 ... 2, 1, 0, 1, 2, ... 98, 99.

 

And yes, one of my projects uses signed numbers where I use the absolute value at more than one occasion so it isn't entirely academic.

Link to comment
Share on other sites

 

My approach is marginally faster. Mostly, it's a lot smaller (both in source and in executable size), and less intrusive looking. It's perhaps also harder to get wrong. ;)

 

For example, nanochess's THEN and ELSE clauses need to be swapped:

.

Before:


' Note: for use with 8-bit signed values (declare with SIGNED), for 16 bits values use #a
IF a < 0 THEN PRINT <3>,a ELSE PRINT "-",<2>-a


Corrected:


' Note: for use with 8-bit signed values (declare with SIGNED), for 16 bits values use #a
IF a < 0 THEN PRINT "-"<2>,-a ELSE PRINT <3>-a

 

Learn kids, don't write code when you don't have 100% your attention on it :P

 

Edit: I've edited the post, thanks Joe! :)

  • Like 1
Link to comment
Share on other sites

 

Learn kids, don't write code when you don't have 100% your attention on it :P

 

Edit: I've edited the post, thanks Joe! :)

 

Indeed. I was guilty of that the other day in the random-position-in-a-square thread, where I wrote 14 (the correct value), edited it to 19 (incorrect) and then had to wipe the egg off my face while changing it back to 14.

 

*d'oh*

:dunce:

  • Like 2
Link to comment
Share on other sites

I thought Nanochess did that mistake on purpose, as an exercise to the reader (mainly First Spear). I was about to comment about it, but I wasn't sure if I was missing out on some smart detail so I didn't.

 

Yes, it has reached that point that when Nanochess posts code snippets, I'm more willing to think he has come up with something spectacularly clever than having made a typo... The same goes for Intvnut and some others.

  • Like 3
Link to comment
Share on other sites

I thought Nanochess did that mistake on purpose, as an exercise to the reader (mainly First Spear). I was about to comment about it, but I wasn't sure if I was missing out on some smart detail so I didn't.

 

Yes, it has reached that point that when Nanochess posts code snippets, I'm more willing to think he has come up with something spectacularly clever than having made a typo... The same goes for Intvnut and some others.

 

Well, there are days when I'm not so smart :dunce:

  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...