generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: How do I use some of the math functions?

Previous Thread :: Next Thread 
killjoy37 is not online. 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 is not online. Seranok
Joined: 12 Dec 2009
Total Posts: 11083
09 Jul 2011 07:45 PM
x = 4.3

print(math.floor(x))

-> 4
Report Abuse
nickmaster2424 is not online. nickmaster2424
Joined: 27 May 2008
Total Posts: 152
09 Jul 2011 07:46 PM
math.random(1, 10) > A number between 1 and 10

math.floor(234.235) > 234
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. 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 is not online. 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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. 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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. 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 is not online. 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 is not online. 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 is not online. BigBBQ12
Joined: 02 Sep 2009
Total Posts: 193
09 Jul 2011 08:27 PM
MyForums
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
09 Jul 2011 08:28 PM
?
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. 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 is not online. 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 is not online. 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 is not online. 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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
09 Jul 2011 08:40 PM
[ Content Deleted ]
Report Abuse
killjoy37 is not online. 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 is not online. 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 is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
09 Jul 2011 08:43 PM
[ Content Deleted ]
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image