Post new topic    
Liquid Metal Slime
Send private message
percentage math question 
 PostSun May 13, 2012 2:15 am
Send private message Reply with quote
I'm trying to write a script that reduces the player's total money by 10%. I tried the script below but it doesn't work because I can't use floating point variables. How do I make this happen?

plotscript, paragon, begin
set money (party money * 0.90)
end
Slime Knight
Send private message
 
 PostSun May 13, 2012 2:22 am
Send private message Reply with quote
Could do something like:

plotscript, paragon, begin
setmoney (partymoney--(partymoney/10))
end

I'm not 100% that will perform the order of operations correctly. If not, do something like this:

plotscript, paragon, begin
variable(10pctmoney)
10pctmoney:=(partymoney/10)
setmoney(partymoney--10pctmoney)
end
SPELLSHARD: THE BLACK CROWN OF HORGOTH now COMPLETE! Grab it today!
Slime Knight
Send private message
 
 PostSun May 13, 2012 2:51 am
Send private message Reply with quote
By the way, a general method for dealing with percentages in Hamsterspeak that I've used over the years is to work with some variation of "divide by 100 (to get 1 percent), then multiply by the desired percentage". It isn't exact, so it's only useful for some applications where 100% accuracy isn't important, but it generally works:

For instance, you could get 73% money (or some unusual percentage) by that method:

32,742 gold * .73 = 23,901.66 = 23,902

32,742 gold / 100 = 327.42; 327 * 73 = 23,871

Difference of 31 gold, not a terribly significant amount of the total.

I am quite certain, tho, that someone on here will know of a better method than this.
SPELLSHARD: THE BLACK CROWN OF HORGOTH now COMPLETE! Grab it today!
Metal Slime
Send private message
 
 PostSun May 13, 2012 5:30 am
Send private message Reply with quote
If you want it to be accurate, just do all division as late as possible, since that will create the rounding. So here, instead of money/100*73, do money*73/100. I'm pretty certain Hamsterspeak can handle big enough numbers to do this now, can't it?
I am Srime
Super Slime
Send private message
 
 PostSun May 13, 2012 6:12 am
Send private message Reply with quote
Yes. Do what msw said and you'll be golden.
Mega Tact v1.1
Super Penguin Chef
Wizard Blocks
Slime Knight
Send private message
 
 PostSun May 13, 2012 12:25 pm
Send private message Reply with quote
What'd I tell ya? Grin

<-- had to stay up really really late in order to get good grades in math classes ;_;
SPELLSHARD: THE BLACK CROWN OF HORGOTH now COMPLETE! Grab it today!
Liquid Metal Slime
Send private message
 
 PostSun May 13, 2012 8:59 pm
Send private message Reply with quote
Quote:
variable(10pctmoney)


Just wanted to jump in and say that variables can't start with numbers. The first character has to be a letter. I discovered this the hard way a few months ago. It shouldn't affect your math issue, though.
Place Obligatory Signature Here
Liquid Metal Slime
Send private message
 
 PostSun May 13, 2012 9:12 pm
Send private message Reply with quote
Harlock Hero wrote:
plotscript, paragon, begin
setmoney (partymoney--(partymoney/10))
end


I ended up with something like this. Thank you for the techniques for using odd percentages as well.
Metal King Slime
Send private message
 
 PostMon May 14, 2012 7:33 am
Send private message Reply with quote
Of course, even "x * 73 / 100" doesn't round to nearest. "(x * 73 + 50) / 100" does, but that's a very minor improvement.
Display posts from previous: