Round (was Re: [DGD] RE: DGD digest, Vol 1 #118 - 6 msgs)

Noah Lee Gibbs angelbob at monkeyspeak.com
Sun Feb 3 06:17:06 CET 2002


  DGD has kind of funny automatic rounding behavior.  Unlike C, a typecast
to int does what John Ruiz wrote -- rounds to the nearest integer.  So a
nearest-integer round would just be a typecast to int!
  To to round down (what C's cast does) you'd want to invert what he did
and say (int)(myfloat - 0.5).  To round up, I guess you'd want to do the
equivalent of C's (int)(myfloat + (1.0 - 1.0e9)), which I (further) guess
in DGD would be (int)(myfloat + (0.5 - 1.0e9)), or basically (int)(myfloat
+ 0.499999).  It looks kinda funny, but I think it'll do a "round up".
  All of these assume that you want to round numbers toward positive
infinity or negative infinity.  For instance, if you "round up" the number
-2.4, you'll get -2 instead of -3 since -2 is "up" toward positive
infinity.  You can change that to round toward larger absolute value, but
it'll involve a little more thinking and possibly some "if(myfloat <
0)" checks.

-- 
angelbob at monkeyspeak.com
See my page of DGD documentation at
"http://www.angelbob.com/projects/DGD_Page.html"
If you post to the DGD list, you may see yourself there!

On Sat, 2 Feb 2002, John Ruiz wrote:

> > I was wondering if anyone had a simple way to round a float up/down
> and 
> > return it as an int?
> 
> Although the Melville MUDlib doesn't support floats (and I see no reason
> why it should), this would probably work for you:
> 
> int round(float victim)
> {
> 	int i;
> 	i = victim + .5;
> 
> 	return i;
> }
> 
> John Ruiz
> ________________________
> Junior, IST @ Penn State
> http://www.johnruiz.com/
> (814)272-4324
> AIM: ipandra
> 
> 
> 
> _________________________________________________________________
> List config page:  http://list.imaginary.com/mailman/listinfo/dgd
> 

_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list