Bennu Game Development

English Forums => General => Topic started by: SolarLune on December 04, 2010, 02:47:53 AM

Title: Rounding variables up or down?
Post by: SolarLune on December 04, 2010, 02:47:53 AM
Hey. So I'm looking for a way to round variables up or down - does anyone know of a way to do this? I thought I saw something like 'RoundUp', but apparently not...?
Title: Re: Rounding variables up or down?
Post by: Outlaw on December 04, 2010, 03:06:09 AM
Hi! I was having the same issue just yesterday, I don´t know if there is a function like that, the way I used to solve this matter was as follows:



num_lines=acum / size_line;
If ((acum [u]mod[/u] long_renglon) <> 0)
   num_lines=num_lines + 1;
End



So, I use mod to see if the result of making a division equals "0" , if not, then the result is´nt exact, the system, when having decimals in an integer, rounds down, so if the result is 5,55, it will save only 5, meaning the integer part of the whole decimal number. When I find that is´nt an integer I add 1 to the variable... then it will save 5,55 +1= 6,55 ----> 6 rounding up without any trouble.

Title: Re: Rounding variables up or down?
Post by: SolarLune on December 04, 2010, 03:53:43 AM
Yeah, I found that I didn't need to round my variable at all. I was trying to move an object by the x and y values, but by fractions - I ended up just moving a float variable I have (I have to make sure that I move the x and y values as well if I want collision, I suppose).
Title: Re: Rounding variables up or down?
Post by: SolarLune on December 04, 2010, 07:44:58 PM
OK, I found a way to round down (and up, as well):


function int Round(float value)
local int bottom;
begin

bottom = value; // Because bottom is an integer, if value is a float, it gets truncated downwards

if bottom + 0.5 >= value
    return bottom;
else
    return bottom + 1;
end

end



I think that'll work.
Title: Re: Rounding variables up or down?
Post by: Windgate on December 05, 2010, 06:25:06 PM
I used this, but it has more code, your solution looks good:

FUNCTION int round ( float f )
PRIVATE
int n;
float g;
BEGIN
g = abs ( f );
n = g;
g = g - n;
IF ( g >= 0.5 )
IF ( f >= 0 )
RETURN f + 1;
ELSE
RETURN f - 1;
END
ELSE
RETURN f;
END
END


And this one can get the decimal part of a float:

FUNCTION float frac ( float f )
PRIVATE
int n;
BEGIN
n = f;
RETURN (f - n);
END
Title: Re: Rounding variables up or down?
Post by: Drumpi on December 06, 2010, 12:11:50 AM
Maybe you must read about predefined local variable RESOLUTION ;)
Title: Re: Rounding variables up or down?
Post by: SolarLune on December 06, 2010, 05:41:08 PM
To my knowledge the RESOLUTION variable dealt with displaying an object when in between positions... I suppose I could have / should have used that for moving my objects using x and y positions. Thanks for the reminder. Still, rounding numbers off would be a nice addition to Bennu.