| View previous topic :: View next topic |
| Author |
Message |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Sun Nov 27, 2005 7:41 am Post subject: 'DIG' equivalent in LUAplayer? |
|
|
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. |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Sun Nov 27, 2005 12:03 pm Post subject: |
|
|
In order to understand what you meant:
Say, variable1 = 123. What is the result of variable2? _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Sun Nov 27, 2005 12:24 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Sun Nov 27, 2005 2:50 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sun Nov 27, 2005 6:13 pm Post subject: Re: 'DIG' equivalent in LUAplayer? |
|
|
| 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: |
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)
|
|
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Mon Nov 28, 2005 9:48 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Zenurb
Joined: 30 Sep 2005 Posts: 106 Location: United Kingdom
|
Posted: Mon Nov 28, 2005 2:07 pm Post subject: |
|
|
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) |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Mon Nov 28, 2005 3:03 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
|