rounded to the nth decimal

G

Gary Wessle

Hi
how can I return
0.0002 from 0.00015
and
0.0002 from 0.00014

Example:
double myRound(double myNum, int precision);
myRound(0.00015, 4); //should return 0.0002
myRound(0.00014, 4); //should return 0.0001
myRound(0.00015, 3); //should return 0
myRound(0.00015, 5); //should return 0.00015


thanks
 
J

Jim Langston

Gary Wessle said:
Hi
how can I return
0.0002 from 0.00015
and
0.0002 from 0.00014

Example:
double myRound(double myNum, int precision);
myRound(0.00015, 4); //should return 0.0002
myRound(0.00014, 4); //should return 0.0001
myRound(0.00015, 3); //should return 0
myRound(0.00015, 5); //should return 0.00015


thanks

Okay, what you are trying to do is round to the nearerst. One problem you
may run into is that doubles are not exactly precise. That is, 0.00015 may
not be exactly stored as that number. It may be 0.000149999 or something.

Anyway, the way I would do it multiply by 10 for each decimal position, add
..5, take the int, divide by down by 10 for each. Understand? Let me show
you...

Value 0.00015. Mulitply by 10 for each position. Becomes 1.5. Add .5.
Becomes 2.0. Take int. 2. Divide back down Becomes 0.0002.
Value 0.00014. Multiply by 10 for each position. Becomes 1.4 Add .5.
Becomes 1.9. Take int. 1. Divide back down. Becomes 0.0001.

Mulitply b 10 for each postion can be done with the pow fucntion (power).
pow( 10, digits ); It's fairly straight forward but may not always give you
exactly what you want. In which case you may want to add .51 or something
to fudge it.

If you need code to do it let me know, but you should have enough to go on.
There may be an easier way, but this is how I would do it.
 
G

Greg Herlihy

Hi
how can I return
0.0002 from 0.00015
and
0.0002 from 0.00014

Example:
double myRound(double myNum, int precision);
myRound(0.00015, 4); //should return 0.0002
myRound(0.00014, 4); //should return 0.0001
myRound(0.00015, 3); //should return 0
myRound(0.00015, 5); //should return 0.00015

Try:

#include <cmath>

using std::pow;

inline
double myRound( double x, int prec)
{
return x * pow(10, prec) * 1.0/pow(10, prec);
}

The idea here is to use exponential expression to compensate for the
imprecise representation of decimal value. Ideally the difference
between the represented value and the true value should "ping-pong"
around 0 - and not steadily move away from 0 in a constant direction.

Greg
 
K

Kai-Uwe Bux

Greg said:
Try:

#include <cmath>

using std::pow;

inline
double myRound( double x, int prec)
{
return x * pow(10, prec) * 1.0/pow(10, prec);
}

The idea here is to use exponential expression to compensate for the
imprecise representation of decimal value. Ideally the difference
between the represented value and the true value should "ping-pong"
around 0 - and not steadily move away from 0 in a constant direction.

Did you try?

#include <cmath>

double myRound( double x, int prec ) {
return x * pow(10, prec) * 1.0/pow(10, prec);
}


#include <iostream>
#include <ostream>
#include <iomanip>

int main ( void ) {
double const high = 0.00015;
double const low = 0.00014;

for ( unsigned prec = 1; prec < 10; ++prec ) {
std::cout << prec << " digits. --> ";
std::cout << std::setprecision(20)
<< myRound( high, prec )
<< ' ';
std::cout << std::setprecision(20)
<< myRound( low, prec )
<< '\n';
}

}


Output on my machine:

1 digits. --> 0.00014999999999999998686 0.00013999999999999998774
2 digits. --> 0.00014999999999999998686 0.00013999999999999998774
3 digits. --> 0.00014999999999999998686 0.00013999999999999998774
4 digits. --> 0.00014999999999999998686 0.00013999999999999998774
5 digits. --> 0.00014999999999999998686 0.00013999999999999998774
6 digits. --> 0.00014999999999999998686 0.00013999999999999998774
7 digits. --> 0.00014999999999999998686 0.00013999999999999998774
8 digits. --> 0.00014999999999999998686 0.00013999999999999998774
9 digits. --> 0.00014999999999999998686 0.00013999999999999998774



To the OP:

Consider rounding at the time of writing the number to some stream.
std::setprecision is your friend.

What is the reason that you want to round intermediate results?


Best

Kai-Uwe Bux
 
M

Mark P

Jim said:
Okay, what you are trying to do is round to the nearerst. One problem you
may run into is that doubles are not exactly precise. That is, 0.00015 may
not be exactly stored as that number. It may be 0.000149999 or something.

Anyway, the way I would do it multiply by 10 for each decimal position, add
.5, take the int, divide by down by 10 for each. Understand? Let me show
you...

This won't work for negative values since floats are truncated when
converted to integers. E.g., -0.7 should round to -1 but will, by your
approach, round to 0. For negative values you can either subtract 0.5
instead of adding it, or flip the sign before and after the rounding
operation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top