Topic: Rounding off a double precision variable
Author: "Gillmer J. Derge" <gderge1@nycap.rr.com>
Date: 2000/03/24 Raw View
Thomas Krog Christensen wrote:
> > Is this compiler dependent? Don't C++ programs truncate when converting
> > from double to int?
>
> Yes they do. But if you want to truncate from double to double I think it's
> faster to use floor.(if not there won't be any reason to use floor)
Sure there would. What happens when you try to round a number such as 1E50 by
casting to an int? A typical signed 32 bit integer can only hold numbers on
the order of 10^10. So an advantage of floor is that it works for all possible
values of double rather than just the small ones.
Note: I'm not saying floor isn't also faster than casting to int. It may be.
I'm saying it has a purpose regardless of its speed.
Finally, let me point out that this also presents a problem for the RoundOff
function proposed earlier:
> double void RoundOff(double x,int noDecimal){
> int f=x*pow(10,noDecimal);
> return floor(x*f+0.5)/f;
> }
I believe a better solution would be to use the modf function (in <math.h>, or
since this is comp.std.c++, <cmath>). Quoting from K&R2, "modf(double x,
double *ip) splits x into integral and fractional parts, each with the same
sign as x. It stores the integral part in *ip, and returns the fractional
part."
-----
Gillmer J. Derge
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Kaspar Daugaard <kaspard@SPAMFREEcabocomm.dk>
Date: 2000/03/24 Raw View
Paul Meyerholtz wrote:
> A question about the function floor() from a newbie. If I'm trying to
> round off doubles the following seems to work:
>
> double x;
> .... x assigned some number ....
> int rounded = int(x +.5);
>
> Is this compiler dependent? Don't C++ programs truncate when converting
> from double to int?
You should note that using floor() is different from truncating when the
value is negative, e.g. floor(-2.4) is -3 while int(-2.4) is -2.
- Kaspar
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Paul Meyerholtz <pholtz@ix.netcom.com>
Date: 2000/03/23 Raw View
Thomas Krog Christensen wrote:
>
> "Tomas Alvier" <tal@nfis.no> wrote in message
> news:C5Ns4.5456$kz6.102045@news1.online.no...
> > Is there a function for rounding of a double precision variable to a
> chosen
> > number of decimals?
>
> double void RoundOff(double x,int noDecimal){
> int f=x*pow(10,noDecimal);
> return floor(x*f+0.5)/f;
> }
A question about the function floor() from a newbie. If I'm trying to
round off doubles the following seems to work:
double x;
.... x assigned some number ....
int rounded = int(x +.5);
Is this compiler dependent? Don't C++ programs truncate when converting
from double to int?
Paul
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Thomas Krog Christensen" <rick@kampsax.dtu.dk>
Date: 2000/03/23 Raw View
> Is this compiler dependent? Don't C++ programs truncate when converting
> from double to int?
Yes they do. But if you want to truncate from double to double I think it's
faster to use floor.(if not there won't be any reason to use floor)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/02/25 Raw View
Thomas Krog Christensen wrote:
>
> "Tomas Alvier" <tal@nfis.no> wrote in message
> news:C5Ns4.5456$kz6.102045@news1.online.no...
> > Is there a function for rounding of a double precision variable to a
> chosen
> > number of decimals?
>
> double void RoundOff(double x,int noDecimal){
> int f=x*pow(10,noDecimal);
> return floor(x*f+0.5)/f;
> }
>
Of course computationally that will do it, but the real question is
what the original poster really wanted. If he wanted the value only
"DISPLAYED" to a certain number of decimal places, this won't accomplish
it.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Tomas Alvier" <tal@nfis.no>
Date: 2000/02/24 Raw View
Is there a function for rounding of a double precision variable to a chosen
number of decimals?
Thanks in advance,
Tomas Alvier
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Thomas Krog Christensen" <rick@kampsax.dtu.dk>
Date: 2000/02/24 Raw View
"Tomas Alvier" <tal@nfis.no> wrote in message
news:C5Ns4.5456$kz6.102045@news1.online.no...
> Is there a function for rounding of a double precision variable to a
chosen
> number of decimals?
double void RoundOff(double x,int noDecimal){
int f=x*pow(10,noDecimal);
return floor(x*f+0.5)/f;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Thomas Krog Christensen" <rick@kampsax.dtu.dk>
Date: 2000/02/25 Raw View
Ron Natalie wrote:
>Of course computationally that will do it, but the real question is
>what the original poster really wanted. If he wanted the value only
>"DISPLAYED" to a certain number of decimal places, this won't accomplish
>it.
then you can use:
char buffer[100];
double v=5.457;
sprintf(buffer,"%.2f",v);
(but I don't think this is the answer to the original qustion)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]