We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
round() ?? (Read 2894 times)
round() ??
May 2nd, 2005, 8:45pm
 
what happend to round() ?

it's not in the docs and not colored but it works ...

F
Re: round() ??
Reply #1 - May 4th, 2005, 2:05am
 
I noticed this. Round is really important.

Can you document this?

If you've not done a direct implementation can I suggest a feature where you can specify
round(value, decimalplaces);

Because its often useful to round to 1 dp., or 2dp, or to round to the nearest thousand (-3 d.p.).

I've been using this function:
Code:

float round(float number, float decimal) {
return (float)(round((number*pow(10, decimal))))/pow(10, decimal);
}


(I added the internal round() when I read this post, previously I was using the flawed (int)value method.)
Re: round() ??
Reply #2 - May 4th, 2005, 3:05am
 
here's the code straight from PApplet:

Code:

static public final float round(float what) {
return Math.round(what);
}

static public final float mag(float a, float b) {
return (float)Math.sqrt(a*a + b*b);
}

static public final float mag(float a, float b, float c) {
return (float)Math.sqrt(a*a + b*b + c*c);
}


mag() is also not documented. I'll get them the docs soon...
Re: round() ??
Reply #3 - May 4th, 2005, 3:46am
 
hm, mag() was just an internal function.. it returns the magnitude of a vector, but wasn't named for public use. if you have a better name in mind let's figure something out.

re: rounding to a decimal place.. i think this is better left to people who want to write their own function (how do you use it in your own work? i can't say i've ever had to do this.. except for formatting it to print, which is done via nf). the problem is that float values won't give an exact rounding on things, so it's likely to be confusing for folks. but if you want to discuss it further please start a new thread over in the suggestions forum so we can keep track.
Re: round() ??
Reply #4 - May 4th, 2005, 11:03pm
 
Fry - you're right I just use rounding to decimal place for formatting string outputs - useful when I'm debugging to have values printed up on the screen. I'll just copy & paste my version wherever I need it.
Re: round() ??
Reply #5 - May 6th, 2005, 7:45pm
 
fixed in the master files. will be uploaded shortly.
Page Index Toggle Pages: 1