Rounding Numbers?


you use float for x.something numbers (eg, 3.02) and int for round numbers (3), so I think, if you just have

global
int y;
float x;

begin
y=3.02;
x=y;
end;

you should have the rounded value in x. Don`t know if it works this, just thought it up.


EDIT: Hmm, only thing is, I think it always rounds down, so 3.99 gets 3.
 
Oh man that is weak, such a simple solution, why didn`t I think of it???
As I had some sparetime (don`t know what minigame I could code next),
I took the time to code a little round program.

I`ll post it anyways, though it is very unnecessary:

Code:
program rounding;
 global
  int rounded_number;
  float number_to_round;  
 
 begin 
   set_mode(160,28,8);
   
   number_to_round=0;      
   write(0,5,5,0, "Number To Round:");
   write(0,5,15,0, "Rounded Number:");   
   write_float(0,120,5,0, &number_to_round);
   write_int(0,120,15,0, &rounded_number);      
   loop     
     if(number_to_round>=0)
       rounded_number=number_to_round+0.51;                 
      end;
     if(number_to_round<0)
       rounded_number=number_to_round-0.51;
      end;      
     if(key(_control))
       number_to_round+=0.01;
      end;     
     if(key(_alt))     
       number_to_round-=0.01;
      end;
     frame;
    end;
  end;

EDIT: Made it simpler :D, also I have to add (sub) 0.51, so it rounds right.
 
Whoops, good point, damn me for not noticing :)

Code:
process Round(float toRound);begin;return (x=toRound+(0.49 * (toRound/abs(toRound));end

Still is pretty short too :), just don't use it 100 times every frame.
 
Example: -3.5+(0.49 * (-3.5/3.5) = -3.5-0.49 = -3.99, but should be <=-4.

You term isn`t working :D besides that, you missing 2 brackets:

Code:
process Round(float toRound);begin;return (x=toRound+(0.5 * (toRound/abs(toRound))));end

This should work now, though I didn`t try it.


EDIT:Just a moment, correct me if I`m wrong, are you rounding -3.5 to -4 or to -3???
I must have remembered that wrong, 3.5 rounds to 4, so I thought you round negative the same way, but that would be your way, so you seem be right, Moogle.
I`m confused now.
 
Yeah I'm confused too. Good point there with the example, but you don't want 3.49 rounded up to 4 either, as would happen with 0.51. Delicate business.

Edit: what was wrong with 0.5 again? What? I'm going for that then. Stupid conversation over something that should be obvious :)
 
Yeah, stupid brains of ours :D

In my example, I need to use 0.51 to round correct. Don`t really know why, though... (It rounds correc this way: 3.49=3 and 3.5=4)


Damn you devlkore, for asking such confusing questions :D


EDIT:
btw, someone please try running my program and tell my, why, when the number to round is increased to 0.83, a value of 0.0099999 gets added.
I`d really like to know that, as it is really strange.

Also, can you limit float to a certain number of numbers after the point? (so 3.29999 for example gets 3.2, no rounding just limiting?)
 
I can't remember whether there is any agreement whether -0.5 should be rounded to 0 or -1, my gut says -1.

so ...
x=sgn(y)*int(abs(y)+0.5)

To round to a significant digit you need to multiply, round, then divide. By 1 'LSB'

ie. to round to hundredths

x=sgn(y)*int(abs(y)*100+0.5)/100




I don't know Fenix, so I hope that int(), abs() and sgn() are 'integer part', 'absolute (ie numerical .. not signed) part' and 'signed part, ie +1, 0 or -1'



Edit: oh! I just noticed that some time *may* be saved by multiplying by 0.01 instead of dividing by 100
so ... x=sgn(y)*int(abs(y)*100+0.5)*0.01

Unless Fenix notices that /100 is a constant and optimizes itself :D
 
You guys really went all out. I was only asking if Fenix had an easy way to convert floats to integers by rounding, and you went writing crazy programs :p

LA!

ThanX, I appreciate it.

Everything seems to be working without any of it though, but I don't remember why I needed it in the first place. Something's bound to break next time I do some coding, probably.
 
Back
Top