Rounding variables up or down?

Started by SolarLune, December 04, 2010, 02:47:53 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

SolarLune

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...?

Outlaw

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.

"Life is cheap when the bounty is high"

SolarLune

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).

SolarLune

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.

Windgate

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
Iván García Subero. Programador, profesor de informática, monitor de actividades culturales y presidente de TRINIT Asociación de Informáticos de Zaragoza. http://trinit.es

Drumpi

Maybe you must read about predefined local variable RESOLUTION ;)
Hala, como con 1001 procesos sólo va a 9 FPS, vamos a meterle 32 veces más, a ver si revienta.
(Drumpi epic moment)

SolarLune

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.