views:

2694

answers:

10

How can you round any number (not just integers > 0) to N significant digits?

For example, if I want to round to 3 significant digits, I'm looking for a formula that could take:

1,239,451 and return 1,240,000

12.1257 and return 12.1

.0681 and return .0681

5 and return 5

Naturally the algorithm should not be hard-coded to only handle N of 3, although that would be a start.

+9  A: 

SUMMARY:

double roundit(double num, double N)
{
    double d = log10(num);
    double power;
    if (num > 0)
    {
        d = ceil(d);
        power = -(d-N);
    }
    else
    {
        d = floor(d); 
        power = -(d-N);
    }

    return (int)(num * pow(10.0, power) + 0.5) * pow(10.0, -power);
}


So you need to find the decimal place of the first non-zero digit, then save the next N-1 digits, then round the Nth digit based on the rest.

We can use log to do the first.

log 1239451 = 6.09
log 12.1257 = 1.08
log 0.0681  = -1.16

So for numbers > 0, take the ceil of the log. For numbers < 0, take the floor of the log.

Now we have the digit d: 7 in the first case, 2 in the 2nd, -2 in the 3rd.

We have to round the (d-N)th digit. Something like:

double roundedrest = num * pow(10, -(d-N));

pow(1239451, -4) = 123.9451
pow(12.1257, 1)  = 121.257
pow(0.0681, 4)   = 681

Then do the standard rounding thing:

roundedrest = (int)(roundedrest + 0.5);

And undo the pow.

roundednum = pow(roundedrest, -(power))

Where power is the power calculated above.


About accuracy: Pyrolistical's answer is indeed closer to the real result. But note that you can't represent 12.1 exactly in any case. If you print the answers as follows:

System.out.println(new BigDecimal(n));

The answers are:

Pyro's: 12.0999999999999996447286321199499070644378662109375
Mine: 12.10000000000000142108547152020037174224853515625
Printing 12.1 directly: 12.0999999999999996447286321199499070644378662109375

So, use Pyro's answer!

Claudiu
Had some minor bugs right when it was accepted, but it's fixed now.
Claudiu
This algorithm seems prone to floating point errors. When implemented with JavaScript, I get: 0.06805 -> 0.06810000000000001 and 12.1 -> 12.100000000000001
Ates Goral
12.1 by itself can't be represented accurately using floating point - it's not a result of this algorithm.
Claudiu
Nice job - thanks.
Jonathan Leffler
This code in Java produces 12.100000000000001 and this is using 64-bit doubles which can present 12.1 exactly.
Pyrolistical
It doesn't matter if it's 64 bit or 128 bit. You can't represent the fraction 1/10 using a finite sum of powers of 2, and that's how floating point numbers are represented
Claudiu
I fixed your code with my own answer
Pyrolistical
A: 

Have you tried just coding it up the way you'd do it by hand?

  1. Convert the number to a string
  2. Starting at the beginning of the string, count digits - leading zeroes aren't significant, everything else is.
  3. When you get to the "nth" digit, peek ahead at the next digit and if it's 5 or higher, round up.
  4. Replace all of the trailing digits with zeroes.
Mark Bessey
A: 

Here's a short and sweet JavaScript implementation:

function sigFigs(n, sig) {
    var mult = Math.pow(10, sig - Math.floor(Math.log(n) / Math.LN10) - 1);
    return Math.round(n * mult) / mult;
}

alert(sigFigs(1234567, 3)); // Gives 1230000
alert(sigFigs(0.06805, 3)); // Gives 0.0681
alert(sigFigs(5, 3)); // Gives 5
Ates Goral
but 12.1257 gives 12.126
Pyrolistical
Yes, that's valid rounding.
Ates Goral
+2  A: 

Isn't the "short and sweet" JavaScript implementation

Number(n).toPrecision(sig)

e.g.

alert(Number(12345).toPrecision(3)

?

Sorry, I'm not being facetious here, it's just that using the "roundit" function from Claudiu and the .toPrecision in JavaScript gives me different results but only in the rounding of the last digit.

JavaScript:

Number(8.14301).toPrecision(4) == 8.143

.NET

roundit(8.14301,4) == 8.144
Justin Wignall
+10  A: 

Here's the same code in Java without the 12.100000000000001 bug that the accepted answer has.

I also removed repeated code, changed power to a type integer to prevent floating issues when n - d is done, and made the long intermediate more clear

The bug was caused by multiplying a large number with a small number. Instead I divide two numbers of similar size.

EDIT
Fixed more bugs. Added check for 0 as it would result in NaN. Made the function actually work with negative numbers (The original code doesn't handle negative numbers because a log of a negative number is a complex number)

public static double roundToSignificantFigures(double num, int n) {
    if(num == 0) {
        return 0;
    }

    final double d = Math.ceil(Math.log10(num < 0 ? -num: num));
    final int power = n - (int) d;

    final double magnitude = Math.pow(10, power);
    final long shifted = Math.round(num*magnitude);
    return shifted/magnitude;
}
Pyrolistical
Your code is indeed much nicer.
Claudiu
Thanks for accepting my answer. I just realized my answer is more than a year after the question. This is one of the reasons why stackoverflow is so cool. You can find useful information!
Pyrolistical
A: 
Loadmaster
Nope. Read the question again. 1239451 with 3 sig figs using your algorithm would incorrectly yield 123951
Pyrolistical
Yep, I corrected it to distinguish between rounding to a *fractional* number of digits (to the right of the decimal point) versus an *integral* number of digits (to the left).
Loadmaster
A: 

See my answer on this other thread

Ben
A: 

Here is a modified version of Ates' JavaScript that handles negative numbers.

function sigFigs(n, sig) {
    if ( n === 0 )
        return 0
    var mult = Math.pow(10,
        sig - Math.floor(Math.log(n < 0 ? -n: n) / Math.LN10) - 1);
    return Math.round(n * mult) / mult;
 }
Jason Swank
A: 
/**
 * Set Significant Digits.
 * @param value value
 * @param digits digits
 * @return
 */
public static BigDecimal setSignificantDigits(BigDecimal value, int digits) {
    //# Start with the leftmost non-zero digit (e.g. the "1" in 1200, or the "2" in 0.0256).
    //# Keep n digits. Replace the rest with zeros.
    //# Round up by one if appropriate.
    int p = value.precision();
    int s = value.scale();
    if (p < digits) {
        value = value.setScale(s + digits - p); //, RoundingMode.HALF_UP
    }
    value = value.movePointRight(s).movePointLeft(p - digits).setScale(0, RoundingMode.HALF_UP)
        .movePointRight(p - digits).movePointLeft(s);
    s = (s > (p - digits)) ? (s - (p - digits)) : 0;
    return value.setScale(s);
}
Valeri Shibaev
A: 

How about this java solution :

double roundToSignificantFigure(double num, int precision){
 return new BigDecimal(num)
            .round(new MathContext(precision, RoundingMode.HALF_EVEN))
            .doubleValue(); 
}
wolfgang grinfeld