'DIG' equivalent in LUAplayer?

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

'DIG' equivalent in LUAplayer?

Post by Art »

Forgive me if this is obvious, I checked out the tutorial on the LUA
maths library because that's where I expected to find it...

One of my basic compilers provide a maths operation for selecting any
digit of a variable, say digit 2 of 255, and setting another variable to that value.

The syntax would be:
variable1 = variable2 DIG 2

Is there any feature like that in LUAplayer that doesn't use exsessive time?
Cheers, Art.
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

In order to understand what you meant:

Say, variable1 = 123. What is the result of variable2?
Geo Massar
Retired Engineer
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

That depends on whether you start counting digits from zero or from 1.
But if you started from zero, and started counting from the least signifigant
digit, then digit 2 would be 1, so:

variable1 = 123
variable2 = 0
variable2 = variable1 DIG 2

The result stored in variable2 would now be 1.
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

I can decompile the output of the compiler in question,
that will show me how it is done in RISC assembler
using small operations. That might put me in better stead
to convert it to LUA, or express it better in English :)
Art.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: 'DIG' equivalent in LUAplayer?

Post by Shine »

Art wrote:Forgive me if this is obvious, I checked out the tutorial on the LUA
maths library because that's where I expected to find it...
Math is a good start, but there are many ways to implement it, e.g.:

Code: Select all

function dig(number, digit)
	local digitValue = math.pow(10, digit)
	return math.floor(math.mod(number, digitValue) / (digitValue / 10))
end

function dig(number, digit)
	local numberString = tostring(number)
	local index = string.len(numberString) - digit + 1
	return tonumber(string.sub(numberString, index, index))
end

assert(dig(7654, 1) == 4)
assert(dig(7654, 2) == 5)
assert(dig(7654, 3) == 6)
assert(dig(7654, 4) == 7)
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

That's great thanx :)
I'll take the top one because it looks the most like on paper..and it looks shorter too.

102/10 = 10.2 <-- DIG 0 = 2
10/10 = 1.0 <-- DIG 1 = 0
1/10 = 0.1 <-- DIG 2 = 1

I have figured out the syntax to reverse:
var1 = dig(var4, 3)
var2 = dig(var4, 2)
var3 = dig(var4, 1)

Cheers, Art.
Zenurb
Posts: 106
Joined: Fri Sep 30, 2005 8:33 am
Location: United Kingdom
Contact:

Post by Zenurb »

I think i see a use for this in displaying numbers from a picture font without converting the number to a string. Right?
Proud Dvorak User
US 1.5 PSP (Original)
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

Yes, and it might also come in handy to edit individual digits in an existing number
the way you would need to with a PSP keypad.
Art.
Post Reply