Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: FinaleTI on July 09, 2010, 11:53:51 am

Title: Integer Extraction Problem
Post by: FinaleTI on July 09, 2010, 11:53:51 am
I'm back! Yay!

I was wondering how to extract a single digit from a number. I need to be able to do this for Nostalgia, as it uses a custom font set, I need a way of testing numbers to display in the custom font. Is there a way to extract a digit from a number, or would I have to send the 1 or 2-byte number to a string.
If so, how would I do that?
Title: Re: Integer Extraction Problem
Post by: jnesselr on July 09, 2010, 11:58:17 am
It kinda depends on what digit you want.  The last digit in a number is always (num mod base).

The second to last digit is ((num mod base^2)-(num mod base))/base.
The nth digit is ((num mod base^n)-(nm mod base(n-1))/base^(n-1).

Example: 341
341 mod 10 = 1;
((341 mod 100)-(341 mod 10))/10=(41-1)/10=40/10=4
((341 mod 100)-(341 mod 100))/100=(341-41)/100=300/100=3

That should work.
Title: Re: Integer Extraction Problem
Post by: nemo on July 09, 2010, 01:04:28 pm
a simpler way to get the first digit in axe would be the following:
number /  (base ^ (digits in number - 1))

example: 341
341 / (10 ^ (3-1))
341 / 10^2
341 / 100
3.41
axe doesn't support floating point so....
3

if you're dealing with 3 digit numbers and above, graphmastur's algorithm is probably the best idea. if you're dealing with just two digits, the easier way would be to mod and divide the number by the base.

example: 34
34 ^ 10 = 4
34 / 10 = 3.4 = 3
Title: Re: Integer Extraction Problem
Post by: _player1537 on July 09, 2010, 01:14:33 pm
or, (I don't think this was said) to get the 3rd number of a 5 digit number would be
number mod 1000 / 100
so for 12345 that would be
12345 mod 1000 = 345
345 / 100 = 3
Title: Re: Integer Extraction Problem
Post by: jnesselr on July 09, 2010, 02:16:52 pm
Oh, yes.  Sorry, didn't see it was axe, so you can get rid of the "-(num mod base^(n-1)" part, and it will still work.
Title: Re: Integer Extraction Problem
Post by: Builderboy on July 09, 2010, 02:29:16 pm
Yeah the general formula to access a digit in base 10 is this.  To extract the Nth digit (starting from 0 on the right)

digit = #/(10^N) mod 10
Title: Re: Integer Extraction Problem
Post by: Quigibo on July 09, 2010, 02:31:15 pm
The algorithm is like this to display a number:
Code: [Select]
.Z is the number you're trying to display
!If Z
sub(D,0)
End
While Z
sub(D,Z^10)
Z/10->Z
End

Where the subroutine D does something with the digit 0-9 and moves a cursor.  This displays the number from right to left and does not add extra zeros or spaces to the front of the number.
Title: Re: Integer Extraction Problem
Post by: DJ Omnimaga on July 09, 2010, 07:50:25 pm
(That stuff can be pretty useful when using custom fonts :D. I use something like this in Supersonic Ball for the timer at the bottom of the screen and will also use it when displaying the score)

The funny thing is that it seems faster to do all of this than just displaying the entire number at once using Text(0,0,Z>Dec ;D (even if Text is set to not automatically update the LCD)