killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 09 Jul 2011 07:44 PM |
| Like, math.random math.floor, things like that. |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 09 Jul 2011 07:45 PM |
x = 4.3
print(math.floor(x))
-> 4 |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2011 07:46 PM |
math.random(1, 10) > A number between 1 and 10
math.floor(234.235) > 234
|
|
|
| Report Abuse |
|
|
|
| 09 Jul 2011 07:47 PM |
math.random([ Minimum [, Maximum]])
Generates a random integer between Minimum and Maximum. If no Maximum is declared, returns a random integer between 0 and Minimum. If no Minimum and no Maximum is declared, returns a random floating point decimal between 0 and 1.
math.floor(number)
Returns the nearest integer (going to negative infinity).
math.ceil(number)
Returns the nearest integer (going to positive infinity). |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 09 Jul 2011 08:06 PM |
| Thanks guys, so math.ceil will always round up, and math.floor will always round down? How would I get it to round to the nearest whole number? |
|
|
| Report Abuse |
|
|
zeke505
|
  |
| Joined: 26 May 2008 |
| Total Posts: 15765 |
|
|
| 09 Jul 2011 08:07 PM |
Custom round fuction
function Round(Numb) local Number = Numb < 0 and math.ceil(Numb - 0.5) or math.floor(Numb + 0.5) return Number end |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2011 08:08 PM |
Use a custom rounding script. :3
function round(num, base) base = tonumber(base) and base or 1 return math.floor(0.5 + math.abs(num)/base)*base*math.abs(num)/num end
print( round(-3.1415927410123, 0.01) ) --> -3.14
The base is optional. If no base is specified, it is default 1 and will round to the nearest integer. |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 09 Jul 2011 08:09 PM |
| I don't really undertsand that, could you explain it a little differently please? |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2011 08:13 PM |
Though I don't believe you're saying that to me, I'll explain the script anyway. I will be using the number -7.8899 for the explanation. math.abs returns the absolute value of a number.
math.floor(0.5 + math.abs(num)/base)*base*math.abs(num)/num
math.floor( 0.5 + math.abs(-7.8899) / (1) ) * (1) * math.abs(-7.8899) / (-7.8899) = math.floor( 0.5 + math.abs(-7.8899) ) * math.abs(-7.8899) / (-7.8899) = math.floor( 0.5 + 7.8899 ) * 7.8899 / -7.8899 = math.floor( 8.3899 ) * -1 = 8 * -1 = -8
:3 |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 09 Jul 2011 08:16 PM |
| You are right, I was talking to zeke, I didn't really understand yours either though. What is "math.abs(num)/base)" mean? What is base? |
|
|
| Report Abuse |
|
|
zeke505
|
  |
| Joined: 26 May 2008 |
| Total Posts: 15765 |
|
|
| 09 Jul 2011 08:17 PM |
| Hm? Oh. You simply do Round(YOURNUMBERHERE) and it will round it to the nearest whole number. |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 09 Jul 2011 08:25 PM |
| I mean, I understand what to do, I just don't understand how you went about it, neither of you, if I knew what base was in agentfirefox's maybe, but "local Numb = Numb < 0" already threw me off in yours sorry. |
|
|
| Report Abuse |
|
|
BigBBQ12
|
  |
| Joined: 02 Sep 2009 |
| Total Posts: 193 |
|
| |
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
| |
|
|
| 09 Jul 2011 08:29 PM |
AGHGHGHG I forgot to explain base. :c The base is the number with which you're rounding to the nearest multiple of.
function round(num, base) base = tonumber(base) and base or 1 return math.floor(0.5 + math.abs(num)/base)*base*math.abs(num)/num end
print( round(-3.1415927410123, 2) ) --> -4 |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 09 Jul 2011 08:35 PM |
| I think you're explaining it well enough, but I still don't understand base. I feel silly for not understanding but I'd like to know so could you word it a little differently please? |
|
|
| Report Abuse |
|
|
McBlocker
|
  |
| Joined: 16 Nov 2008 |
| Total Posts: 1721 |
|
|
| 09 Jul 2011 08:36 PM |
old = 3.6 new = math.floor(old+0.5) >4
:3 |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 09 Jul 2011 08:38 PM |
| Oh, that makes sense, because, if it never went to the next number, it would mean that the number had to be less than halfway to the next number. Thanks mcblocker. |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 09 Jul 2011 08:39 PM |
| @AgentFirefox Thanks for your help, but isn't mcblocker's way easier? |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2011 08:39 PM |
Hmm... I'll try to explain through example.
7.555 rounds to 9 when rounding to the nearest multiple of 3 8.999 rounds to 8 when rounding to the nearest multiple of 2 10.22 rounds to 12 when rounding to the nearest multiple of 4
The base is the number which you're trying to round to the nearest multiple of. In the case of 1, the new number is always the nearest integer. |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2011 08:40 PM |
"@AgentFirefox Thanks for your help, but isn't mcblocker's way easier?"
math.floor(-2.5 + 0.5) --> -2
BAAAAD!!! |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 09 Jul 2011 08:41 PM |
| What if you did math.abs before adding the .5 and then taking math.floor? |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 09 Jul 2011 08:42 PM |
@Oysi Why did you take 10 to the 0 power, it is just 1? |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|