This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
grand_mind1's profile picture

Rounding

Started by grand_mind1, 04 February 2013 - 11:31 AM
grand_mind1 #1
Posted 04 February 2013 - 12:31 PM
I am working on a cobble factory that uses turtles and a main computer. You type in the amount of cobble you want to have mined and the computer will divide that number by the amount of active turtles in the factory. I am having some trouble with dividing the amount of cobble between the turtles. If I want 1024 cobble and there are 5 active turtles then each one will be assigned to mine 204.8 cobble. This won't work with my code because the amount of cobble they have will never be equal to what they were assigned. I would like to know if there is a way I can round 204.8 to 205 to fix this problem.
Help is appreciated!
Thanks! :D/>
coolblockj #2
Posted 04 February 2013 - 12:44 PM
math.ceil(x) should work, it rounds whatever X is to the nearest integer greater or equal to it
Hope that helped :)/>
Dlcruz129 #3
Posted 04 February 2013 - 04:43 PM
math.ceil(x) should work, it rounds whatever X is to the nearest integer greater or equal to it
Hope that helped :)/>/>

There is also math.floor() and math.round().
Kingdaro #4
Posted 04 February 2013 - 05:01 PM
Just checked, math.round doesn't exist. True rounding can be done easily though, if you're rounding to the ones place.


math.floor(somenumber + .5)
Dlcruz129 #5
Posted 04 February 2013 - 05:06 PM
Just checked, math.round doesn't exist. True rounding can be done easily though, if you're rounding to the ones place.


math.floor(somenumber + .5)

:o/> really? I must be thinking of Java.
Kingdaro #6
Posted 04 February 2013 - 05:54 PM
Yep, it's everywhere but lua. :lol:/>
remiX #7
Posted 04 February 2013 - 06:11 PM
Well, it's easy to make one…


function math.round(n)
	for i = 1, #tostring(n) do
		if string.sub(tostring(n), i, i) == "." then
			return tonumber(string.sub(tostring(n), i+1, i+1)) >= 5 and math.ceil(n) or math.floor(n)
		end
	end
	return n
end

Why does string.find(tonumber(n), ".") always return 1? :|
Kingdaro #8
Posted 04 February 2013 - 06:39 PM
It's usually not best to use string functions to perform math.

There are a couple of rounding implementations on lua-users. Near the top are functions that round to decimal places, and below that are functions that round to specific counts (e.g. round(5, 7) = 7).

http://lua-users.org/wiki/SimpleRound (the one on the very bottom is mine ^^)
remiX #9
Posted 04 February 2013 - 11:09 PM
Well… It works, so a problem is one I don't see :P/>
ChunLing #10
Posted 04 February 2013 - 11:22 PM
It's just ludicrously expensive to convert a number to a string and then use string functions to do math on it before converting it back into a number.

Particularly when Kingdaro already posted a perfectly good solution…as in a solution simple and reliable enough to explain why Lua doesn't bother having a math.round() function.
Orwell #11
Posted 04 February 2013 - 11:47 PM
Yep, it's everywhere but lua. :lol:/>
You know, C and C++ don't have a round function in the standard library either. I've always found that a bit odd. :P/> I usually use your solution as well in C/C++. Except for when I need to round on the n-th digit.
GopherAtl #12
Posted 05 February 2013 - 09:36 AM
rounding in c:

float f=1.5f
int i=(int)(f+.5f)
Lyqyd #13
Posted 05 February 2013 - 11:15 AM
Why does string.find(tonumber(n), ".") always return 1? :|

Because you do not fully grok Lua string matching patterns. :)/>
Orwell #14
Posted 05 February 2013 - 11:44 AM
rounding in c:

float f=1.5f
int i=(int)(f+.5f)
That wouldn't work well for negative numbers. I always use:

#include <math.h>

double a = 3.14159f;
double rounded = floor( a + 0.5f );
or the same in C++:

#include <cmath>

double a = 3.14159f;
double rounded = std::floor( a + 0.5f );
GopherAtl #15
Posted 05 February 2013 - 11:49 AM
Naturally floor should be called if you want the rounded value to still be used as a float; most commonly if I'm rounding a value in c, I'm converting to an int at the same time. I'm fairly sure this would behave as I would expect for negtive numbers - rounding -.5 to 0, -1.1 to -1, but I'm not 100% sure this is correct, as I don't remember dealing with this case before.
Orwell #16
Posted 05 February 2013 - 11:57 AM
Naturally floor should be called if you want the rounded value to still be used as a float; most commonly if I'm rounding a value in c, I'm converting to an int at the same time. I'm fairly sure this would behave as I would expect for negtive numbers - rounding -.5 to 0, -1.1 to -1, but I'm not 100% sure this is correct, as I don't remember dealing with this case before.
Well, let's take -1.1 . Thus, according to your example:

float f=-1.1f
int i=(int)(f+.5f)
// = (int)(-1.1f + .5f)
// = (int)(-0.6f)
// = 0
Casting to an integer will always round towards zero, so you get this problem. floor() will actually round towards negative infinity which makes it work.
GopherAtl #17
Posted 05 February 2013 - 11:59 AM
ah, right. Floating-point values aren't signed the same way as integers, the mantissa is always stored unsigned.
Orwell #18
Posted 05 February 2013 - 12:52 PM
ah, right. Floating-point values aren't signed the same way as integers, the mantissa is always stored unsigned.
I'm not sure how that is relevant. Even if the significand would be signed under one's complement (not two's complement, floats have negative zero), it would be a matter of dropping the most significant bit like it wasn't there. The point is that you don't really round towards zero but just drop all decimals. It hardly even matters anyway.
GopherAtl #19
Posted 05 February 2013 - 01:06 PM
jesus. Truncation would be the same as floor if the values were signed. Since the sign is separate from the mantissa, truncation is towards 0.

Half of my responses in this ridiculous back-and-forth have been clarifying things I said and the other half apologizing for omissions or mistakes I made. 100% of yours have been attacking me for what you apparently see as grevious errors. Carry on as long as you like, I'm done with this stupid tangent.
Orwell #20
Posted 05 February 2013 - 01:45 PM
jesus. Truncation would be the same as floor if the values were signed. Since the sign is separate from the mantissa, truncation is towards 0.

Half of my responses in this ridiculous back-and-forth have been clarifying things I said and the other half apologizing for omissions or mistakes I made. 100% of yours have been attacking me for what you apparently see as grevious errors. Carry on as long as you like, I'm done with this stupid tangent.
I've thought it through and you are right about the importance of signedness. Although you can expect correction when posting C code out of the blue, I admit that I was totally aggressive in my last post and I apologize for it.