 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Shea Martin Guest
|
Posted: Tue Oct 28, 2003 10:14 pm Post subject: rounding a float/double to nearest 1/10th |
|
|
Any one have a better/simpler method for rounding a float to the nearest
1/10th? This is currently what I am using, and there must be a better
way, or perhaps a canned method that I am not aware of.
double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;
NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.
Any suggestions would be great thank.
~S
|
|
| Back to top |
|
 |
Shea Martin Guest
|
Posted: Tue Oct 28, 2003 10:32 pm Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
Shea Martin wrote:
| Quote: | Any one have a better/simpler method for rounding a float to the nearest
1/10th? This is currently what I am using, and there must be a better
way, or perhaps a canned method that I am not aware of.
double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;
NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.
Any suggestions would be great thank.
~S
|
float z2 = atof(arg[1]);
z2 = (float)floor(z2*10+0.5)/10;
I thinks this is a lot better.
~S
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Tue Oct 28, 2003 10:37 pm Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
Shea Martin wrote:
| Quote: | Any one have a better/simpler method for rounding a float to the nearest
1/10th? This is currently what I am using, and there must be a better
way, or perhaps a canned method that I am not aware of.
double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;
NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.
|
z = 0.1 * round( z * 10.0 );
|
|
| Back to top |
|
 |
Shea Martin Guest
|
Posted: Tue Oct 28, 2003 11:13 pm Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
Gianni Mariani wrote:
| Quote: | Shea Martin wrote:
Any one have a better/simpler method for rounding a float to the
nearest 1/10th? This is currently what I am using, and there must be
a better way, or perhaps a canned method that I am not aware of.
double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;
NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.
z = 0.1 * round( z * 10.0 );
Which header do I need to get round? It does not seem to be in math.h |
on Solaris 9.
Thanks,
~S
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Wed Oct 29, 2003 9:58 am Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote
[SNIP]
| Quote: | z = 0.1 * round( z * 10.0 );
|
Somehow I missed that round() was a standard function. I'd be happy if you
could point out where I can find it, so that I can get rid of my own
solution.
I usually use the following approach:
double Round( double Value, int Digits )
{
if( Value > 0.0 )
return ( (long)( Value * Faktor + 0.5 ) ) / pow( 10.0, Digits);
return ( (long)( Value * Faktor - 0.5 ) ) / pow( 10.0, Digits);
}
In case of common values for Digits one could use a table instead of
calculating the factor with pow() as it is a rather slow function.
Regards
Chris
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Wed Oct 29, 2003 10:04 am Post subject: Correction Re: rounding a float/double to nearest 1/10th |
|
|
"Chris Theis" <Christian.Theis (AT) nospam (DOT) cern.ch> wrote
| Quote: |
"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote in message
news:bnmr3c$j3u (AT) dispatch (DOT) concentric.net...
[SNIP]
double Round( double Value, int Digits )
{
if( Value > 0.0 )
return ( (long)( Value * pow( 10.0, Digits) + 0.5 ) ) / pow( 10.0,
Digits); |
Sorry, this should of course be
if( Value > 0.0 )
return ( (long)( Value * pow( 10.0, Digits) + 0.5 ) ) / pow( 10.0,
Digits);
return ( (long)( Value * pow( 10.0, Digits) - 0.5 ) ) / pow( 10.0,
Digits);
It's obviously too early for me :-)
Chris
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Oct 29, 2003 4:24 pm Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
"Shea Martin" <smartin (AT) arcis (DOT) com> wrote
| Quote: | z = 0.1 * round( z * 10.0 );
Which header do I need to get round? It does not seem to be in math.h
on Solaris 9.
Ain't no such function. Try nearbyint or rint. |
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Oct 29, 2003 4:26 pm Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
"Chris Theis" <Christian.Theis (AT) nospam (DOT) cern.ch> wrote
| Quote: |
"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote in message
news:bnmr3c$j3u (AT) dispatch (DOT) concentric.net...
[SNIP]
z = 0.1 * round( z * 10.0 );
Somehow I missed that round() was a standard function. I'd be happy if you
could point out where I can find it, so that I can get rid of my own
solution.
Should be in math.h. It's a C function, but part of C99. |
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Wed Oct 29, 2003 4:41 pm Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
"Ron Natalie" <ron (AT) sensor (DOT) com> wrote
| Quote: |
"Chris Theis" <Christian.Theis (AT) nospam (DOT) cern.ch> wrote
"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote in message
news:bnmr3c$j3u (AT) dispatch (DOT) concentric.net...
[SNIP]
z = 0.1 * round( z * 10.0 );
Somehow I missed that round() was a standard function. I'd be happy if
you
could point out where I can find it, so that I can get rid of my own
solution.
Should be in math.h. It's a C function, but part of C99.
|
Aah, thanks Ron. Didn't know that.
Cheers
Chris
|
|
| Back to top |
|
 |
Shea Martin Guest
|
Posted: Wed Oct 29, 2003 8:20 pm Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
Ron Natalie wrote:
| Quote: | "Chris Theis" <Christian.Theis (AT) nospam (DOT) cern.ch> wrote
"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote in message
news:bnmr3c$j3u (AT) dispatch (DOT) concentric.net...
[SNIP]
z = 0.1 * round( z * 10.0 );
Somehow I missed that round() was a standard function. I'd be happy if you
could point out where I can find it, so that I can get rid of my own
solution.
Should be in math.h. It's a C function, but part of C99.
One more reason to upgrade our aging compilers (Sun WorkShop 5.0).  |
~S
|
|
| Back to top |
|
 |
Anonymous Guest
|
Posted: Thu Oct 30, 2003 2:42 am Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
Shea Martin wrote:
| Quote: | Any one have a better/simpler method for rounding a float to the nearest
1/10th?
|
Don't know what you want to do with the result, but how about
char s[30];
sprintf(s,"%.1lf",z); // s is z rounded to nearest 0.1
z=atof(s);
[email]sherNOwoodSPAM (AT) computer (DOT) org[/email] (remove caps for e-mail)
|
|
| Back to top |
|
 |
Steven C. Guest
|
Posted: Thu Oct 30, 2003 6:09 am Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
"Shea Martin" <smartin (AT) arcis (DOT) com> wrote
Gianni Mariani wrote:
| Quote: | Shea Martin wrote:
Any one have a better/simpler method for rounding a float to the
nearest 1/10th? This is currently what I am using, and there must be
a better way, or perhaps a canned method that I am not aware of.
double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;
NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.
z = 0.1 * round( z * 10.0 );
Which header do I need to get round? It does not seem to be in math.h |
on Solaris 9.
Thanks,
~S
----------------------------------------------
Personally I prefer a car.
|
|
| Back to top |
|
 |
P.J. Plauger Guest
|
Posted: Thu Oct 30, 2003 4:51 pm Post subject: Re: rounding a float/double to nearest 1/10th |
|
|
"Steven C." <nospam (AT) xxx (DOT) com> wrote
| Quote: | "Shea Martin" <smartin (AT) arcis (DOT) com> wrote in message
news:fNCnb.9864$f7.536027 (AT) localhost (DOT) ..
Gianni Mariani wrote:
Shea Martin wrote:
Any one have a better/simpler method for rounding a float to the
nearest 1/10th? This is currently what I am using, and there must be
a better way, or perhaps a canned method that I am not aware of.
double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;
NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.
z = 0.1 * round( z * 10.0 );
Which header do I need to get round? It does not seem to be in math.h
on Solaris 9.
|
You need a version of math.h that better conforms to C99. We offer
such a library, but it's an extra-cost item.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|