English [DEV] Better rounding (forget math.round() !) icon

7 comments
29.01.12 03:56:35 am
like 3 like it!
39 kb, 78 Downloads
VADemon
User
Offline Off
[DEV] Better rounding (forget math.round() !)
Developer Tool


Hey all!
I made this script for developers who don't like Lua's default inaccurate math.round(). Why? Because it fails!

∗ Feature:
All description you need is on the picture.

In fact my small function corrects the math.round() whenever it returns a wrong value.


For example, math.round(2.3) returns you a 3.
Quote:
math.round(2.1) => 3
math.round(2.7) => 3

My round() :
round(2.1) => 2
round(2.7) => 3


∗ How to use:
1. Add the whole part of the code to your script or add the file with dofile(better_rounding.lua)
2. Use round(2.3) instead math.round(2.3) !

° Permissions:
There're no restrictions. Use the code how you want!
But please don't reupload it anywhere and say it's yours! (the code is very simple and you're just an idiot if you do that ;))

About:
Only 8 lines of code and 115 characters. Smallest script (function) I've ever made.
image
edited 4×, last 29.01.12 04:00:21 am
ok This file has been reviewed and approved by Fraizeraust (13.03.16 07:12:38 pm)

Comments

7 comments
Goto Page
To the start Previous 1 Next To the start

Log in!

You need to log in to be able to write comments!Log in
29.01.12 12:31:36 pm
Up
VADemon
User
Offline Off
@user PyKemis:
There're people who are better than someone

Should I also add your script to the archive?

@user DannyDeth:
Nice idea. Will make examples of it.
29.01.12 09:05:41 am
like I like it!
Up
Infinite Rain
Reviewer
Offline Off
Nice
29.01.12 08:24:10 am
Up
DannyDeth
User
Offline Off
Aah, never thought of that! Complete geniuses, both of you.
29.01.12 08:19:06 am
like I like it!
Up
DarkLight66
User
Offline Off
@user DannyDeth: numbers lesser than n.5 + 0.5 on math.floor will return n, but any numbers over n.5 +0.5 on math.floor will give n+1, so i think it is right lol.
29.01.12 08:13:12 am
Up
DannyDeth
User
Offline Off
@user PyKemis:
No, it couldn't. That would just floor it to the lowest value, which is not what "rounding" a number does.

@VADemon:
Try modifying the math table:
Code:
1
math.round = round

Comes to mind.
29.01.12 04:44:04 am
Up
PyKemis
User
Offline Off
This could just be 3 lines:

Code:
1
2
3
function round(a)
     return math.floor(a+0.5)
end
29.01.12 04:09:11 am
like I like it!
Up
KenVo
User
Offline Off
Short but useful
To the start Previous 1 Next To the start