Files

> > CS2D > Lua Scripts > [DEV] Better rounding (forget math.round() !)
Files overviewCS2D overviewLua Scripts overview

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

7 comments39 kb, 480 Downloads

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

VADemon
User Off Offline

[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.
edited 4×, last 29.01.12 04:00:21 am
Approved by GeoB99

Download Download

39 kb, 480 Downloads

Comments

7 comments
To the start Previous 1 Next To the start

Log in!

You need to log in to be able to write comments!Log in

old

VADemon
User Off Offline

@user Apache uwu:
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.

old

DannyDeth
User Off Offline

Aah, never thought of that! Complete geniuses, both of you.

old

DarkLight66
User Off Offline

@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.
I like it!

old

DannyDeth
User Off Offline

@user Apache uwu:
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:
1
math.round = round
Comes to mind.

old

Apache uwu
User Off Offline

This could just be 3 lines:

1
2
3
function round(a)
	return math.floor(a+0.5)
end

old

KenVo
User Off Offline

Short but useful
I like it!
To the start Previous 1 Next To the start