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 13 25897
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.
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
> float z2 = atof(arg[1]); z2 = (float)floor(z2*10+0.5)/10;
better
z2 = (float)(floor( z2 * 10 + 0.5 ) / 10)
So the value gets chopped to a float (if at all) after the division.
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 );
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.
Thanks,
~S
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bn********@dispatch.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.
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
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bn**********@sunnews.cern.ch... "Gianni Mariani" <gi*******@mariani.ws> wrote in message news:bn********@dispatch.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
"Shea Martin" <sm*****@arcis.com> wrote in message news:fNCnb.9864$f7.536027@localhost... 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.
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:bn**********@sunnews.cern.ch... "Gianni Mariani" <gi*******@mariani.ws> wrote in message news:bn********@dispatch.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.
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m... "Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bn**********@sunnews.cern.ch... "Gianni Mariani" <gi*******@mariani.ws> wrote in message news:bn********@dispatch.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
Ron Natalie wrote: "Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:bn**********@sunnews.cern.ch...
"Gianni Mariani" <gi*******@mariani.ws> wrote in message news:bn********@dispatch.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
Shea Martin wrote: 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); sh************@computer.org (remove caps for e-mail)
"Shea Martin" <sm*****@arcis.com> wrote in message
news:fNCnb.9864$f7.536027@localhost...
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.
Thanks,
~S
----------------------------------------------
Personally I prefer a car.
"Steven C." <no****@xxx.com> wrote in message news:oW*****************@twister.socal.rr.com... "Shea Martin" <sm*****@arcis.com> wrote in message news:fNCnb.9864$f7.536027@localhost... 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marc Schellens |
last post by:
My compiler warns about assignment to int form
float/double.
Will it nevertheless do what I expect?
Are there caveeats/pits?
(Apart from the range and the precision of course)
What would be the...
|
by: Alexander Block |
last post by:
Hello newsgroup,
let's say I have a function like
template<class Type>
inline bool areEqual(const Type &a, const Type &b)
{
return ( a == b );
}
|
by: JKop |
last post by:
Does the Standard specify any minimum range or minimum
precision for the float, double and long double. I haven't
found anything in the Standard about it.
-JKop
|
by: Madan |
last post by:
Hi all,
I had problem regarding float/double arithmetic only with + and -
operations, which gives inaccurate precisions. I would like to know how the
arithmetic operations are internally handled...
|
by: kennethlou |
last post by:
Hi,
If in C a variable appears like X=10.000000, I can round it to zero
decimal places. ie X=10?
I then have to save the number into a variable.
The method appears below:...
|
by: illegal.prime |
last post by:
Hi all, I have a class that I would like to use to hide the data type
I'm using to store decimal point numbers. Failing all else, I will
just use object and do conversion/casts as necessary. But...
|
by: lindabaldwin |
last post by:
Hi everyone,
I have a code that I can use in Excel to round to the nearest 0.5, but I cannot get it to work in Access. When I try to execute it I get the following error message: "Compile error:...
|
by: veremue |
last post by:
I want to store a float/double as String. I have an input file with a column with float values i.e. have the form 1.4E15. I want to store these values as Strings. Here is my code which is resulting...
|
by: Wander |
last post by:
currently there are those functions is_float, is_double and is_real,
which do exactly the same as far as i can see
what surprises me is that $a = (float/double/real) 1; all work, while
with...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |