Thread Tools Display Modes
09-30-10, 09:48 PM   #1
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
[Question] floor(mod(x,y))

Been searching around how to to covert copper to g/s/c readout. GetCoinText() is too bulky and I apparently still fail at using gsub correctly. I found this solution, but the silver calculation has me confused:

lua Code:
  1. copper = 123456
  2. gold = floor(copper/10000) -- makes sense
  3. copper = floor(mod(copper, 100) -- makes sense
  4.  
  5. silver = floor(mod(copper/100, 100)) -- um, wut?

My working (and apparently incorrect) understanding:
s = floor(mod(x,y))
1) x = 123456/100 = 1234.56
2) x/y = 1234.56/ 100 = 12.3456
3) mod(12.3456) = 3456
4) floor(3456) = 3456

I'm not seeing how the answer "34" is obtained by this method, yet somehow works. Can anyone shine some light on this?
  Reply With Quote
09-30-10, 10:25 PM   #2
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
Originally Posted by ChaosInc View Post

lua Code:
  1. copper = 123456
  2. gold = floor(copper/10000) -- makes sense
  3. copper = floor(mod(copper, 100) -- makes sense
  4.  
  5. silver = floor(mod(copper/100, 100)) -- um, wut?


I'm not seeing how the answer "34" is obtained by this method, yet somehow works. Can anyone shine some light on this?
copper is 123456 so we know is 12g 34s 56c

mod(123456, 100) is 56, because if you divide 123456, you get 1234.56 the remainder is 56

now silver is 123456/100/100 so its 12.3456, and since the mod is 100 the remainder is 34.56, but its been floored to 34.
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote
09-30-10, 11:33 PM   #3
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
Originally Posted by Xubera View Post
copper is 123456 so we know is 12g 34s 56c

mod(123456, 100) is 56, because if you divide 123456, you get 1234.56 the remainder is 56

now silver is 123456/100/100 so its 12.3456, and since the mod is 100 the remainder is 34.56, but its been floored to 34.
I'm even more confused than before. Obviously I know that 34 is supposed to be the silver amount. It's how it gets there that I'm confused by. The equation shows division by 100 twice (arg1 = 123456/100 and y = 100) yet your explanation throws in a third non-existent 100 (to my understanding at this point).

123456/100 => 1234.56
1234.56/100 => 12.3456
mod(12.3456) => 3456
??? => 34.56
floor(34.56) => 34

I'm not seeing how the decimal point seems to "magically" move over two spaces in order to floor it to 34.
  Reply With Quote
10-01-10, 12:38 AM   #4
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
I think your issue is trying to separate mod(x, y) into two steps when it doesn't quite work that way. The function mod(x, y) is basically solving the equation x = floor(x / y) + (r / y) and returning the value r.

-- silver = floor(mod(copper/100, 100))
silver = copper / 100 -- 1234.56
silver = mod(silver, 100) -- 34.56
silver = floor(silver) -- 34

Or maybe this is easier to look at:
silver = floor(copper / 100 % 100)

And here is some code I've used for something similar:
Code:
local cuInd = [[|TInterface\MoneyFrame\UI-CopperIcon:0:1:2:0|t]]
local agInd = [[|TInterface\MoneyFrame\UI-SilverIcon:0:1:2:0|t ]]
local auInd = [[|TInterface\MoneyFrame\UI-GoldIcon:0:1:2:0|t ]]

local function MoneyToString(ammount)
	local cu = ammount % 100
	ammount = floor(ammount / 100)
	local ag, au = ammount % 100, floor(ammount / 100)
	if au > 0 then
		return au .. auInd .. ag .. agInd .. cu .. cuInd
	elseif ag > 0 then
		return ag .. agInd .. cu .. cuInd
	end
	return cu .. cuInd
end
  Reply With Quote
10-01-10, 01:08 AM   #5
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
the function mod(x,y) will first divide x by y and then produce the remainder

so if

x = 123456
y = 100
print(mod(x,y)) --outputs 56 because x/y is 1234.56 and the remainder is 0.56.

or
x1 = 15
x2 = 16
y = 5
print(mod(x1,y)) --outputs 0 because x1/y is exactly 3, so the remainder is 0
print(mod(x2,y)) --outputs 1 because x2/y is 3 and 1/5th, so the remainder is 1

in the above floor(mod(copper/100, 100))

its really saying

lua Code:
  1. floor(mod(123456/100, 100))
  2. floor(mod(1234.56,100))
  3. floor(34.56) --because the remainder of 12.3456 is 34.56
  4. 34
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>

Last edited by Xubera : 10-01-10 at 01:12 AM.
  Reply With Quote
10-01-10, 04:22 AM   #6
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
lua Code:
  1. local money = 1234562
  2. local copper = money % 100 -- take the last two digits
  3. local silver = floor(money / 100) % 100 -- first shift two digits to the right and then take the last two digits
  4. local gold = floor(money / 10000) -- shift four digits to the right and take the remaining digits
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote
10-01-10, 10:22 AM   #7
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
mod(1234.56, 100)
This is where my confusion lies. Unless I'm suddenly failing at math, isn't 1234.56/100=12.3456? Then would be mod(12.3456)=3456?
  Reply With Quote
10-01-10, 10:54 AM   #8
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
Originally Posted by ChaosInc View Post
This is where my confusion lies. Unless I'm suddenly failing at math, isn't 1234.56/100=12.3456? Then would be mod(12.3456)=3456?
When using modulus you're asking what the remainder is after you've completed the division. Think of it as "how many times can I fit my second value into my first? What is left if I remove that value that many times?".

You can subtract twelve 100s from 1234.56, and thus 34.56 is the remainder.

Example:
4 mod 4 = 0, because there's nothing left when you've removed as many 4s as you can (1).
5 mod 4 = 1, now there's 1 left.

Ergo, 15 mod 3 = 0 (cause 5 times 3 is 15), 16 mod 3 = 1 (5 times 3 is 15, can't get another 3 out of the remaining 1), and so on.

Wikipedia entry: http://en.wikipedia.org/wiki/Modulo_operation (but it's pretty daunting..)
__________________
Oh, the simulated horror!
  Reply With Quote
10-01-10, 11:49 AM   #9
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
Originally Posted by Ailae View Post
When using modulus you're asking what the remainder is after you've completed the division. Think of it as "how many times can I fit my second value into my first? What is left if I remove that value that many times?".

You can subtract twelve 100s from 1234.56, and thus 34.56 is the remainder.

Example:
4 mod 4 = 0, because there's nothing left when you've removed as many 4s as you can (1).
5 mod 4 = 1, now there's 1 left.

Ergo, 15 mod 3 = 0 (cause 5 times 3 is 15), 16 mod 3 = 1 (5 times 3 is 15, can't get another 3 out of the remaining 1), and so on.

Wikipedia entry: http://en.wikipedia.org/wiki/Modulo_operation (but it's pretty daunting..)
Ah, so there is a "hidden" step in there (work with me here, this how my brain understands things lol). Actually, the Wikipedia page made much more sense to me than Lua's "finds the remainder of x/y" which presents that it's just straight division when it's technically not.

I get it now. Thanks to all for your patience in my noob questions. Haven't gotten to modulus yet in my courses so I haven't had anything prior to work with to help understand.

Edit: Actually, it is like straight division now that I reflect upon it. It's just been a long time since I've worked with non-decimal division. It's like learning 5th grade math all over lol.

Last edited by Sythalin : 10-01-10 at 11:55 AM.
  Reply With Quote
10-01-10, 12:06 PM   #10
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Well, you say you understand now but here is another way of looking at it:

mod(x, y) = x % y = x - floor(x / y) * y
  Reply With Quote
10-01-10, 07:47 PM   #11
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
Originally Posted by ChaosInc View Post
This is where my confusion lies. Unless I'm suddenly failing at math, isn't 1234.56/100=12.3456? Then would be mod(12.3456)=3456?
yeah, hopefully this wont aid in the confusion, but

mod(1234.56, 100) is 12.3456 (like you said above) but that is really
Code:
12     34.56
   and  -----  (this is a fraction :)  sorry for bad artwork)
        100
and yeah like you said, its like the division you learned in primary school, before they taught about decimals. the mod function goes ahead and rounds that down. so its 34.

and like the other examples mod(15,5) and mod(16,5) are 0 and 1 because its
Code:
3  0          3   1
   --   and      --
   5              5
respectively. the remainder is 0, and the remainder is 1 giving our answers of what mod(15,5) and (16,5) are

edit:above in code for formatting
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » [Question] floor(mod(x,y))


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off