5

I was asked the following question and I didn't know how to answer it. I was wondering if anybody could help:

Using C#, and not using any Math functions (Round() and Truncate() aren't allowed) take the following double 3.009784654 and round it to 4 decimal places.

This was for an interview, not for a class project or homework. The interviewer seemed to be trying to get me to use mod, but I still couldn't think of how to do it.

Thanks!

4
  • No mod, but you could format it as a string with 4 decimal places shown and parse it back to a double (not efficient at all!).
    – assylias
    Jul 2, 2012 at 18:24
  • Yeah - I mentioned that to them but that wasn't allowed either!
    – MattW
    Jul 2, 2012 at 18:25
  • 2
    How to drive a car without using a car....
    – H H
    Jul 2, 2012 at 18:26
  • 2
    @HenkHolterman More like understanding how the car's engine works rather than just drive it and take it for granted.
    – roken
    Jul 2, 2012 at 18:44

6 Answers 6

7

To truncate (since rounding rules were not specified): multiply by 1e4, cast to int, divide by 1e4.

5
  • Oh well you were the fastest ^^ Jul 2, 2012 at 18:26
  • Seems like I should have known this (simple Math) - but for whatever reason I didn't.
    – MattW
    Jul 2, 2012 at 18:29
  • @AustinSalonen For the record Math.Round(value, digits) limits abs(value) to 1e16d.
    – roken
    Jul 2, 2012 at 18:40
  • That won't round the number, it will just remove the digits after the 4. In this case the last 8 removed would turn the last remaining 7 into an 8.
    – MrFox
    Jul 2, 2012 at 18:46
  • @MrFox The rounding rules were undefined in the question, I explicitly stated that in my answer.
    – roken
    Jul 2, 2012 at 18:48
2

This is a bad interview question, because the task is generally impossible, given the expected tools, because most of the correct results cannot be represented. That is, the common floating-point type cannot represent the correct mathematical result, 3.0098, exactly. So it is impossible to calculate and return the correct answer. You would have to use some alternate mechanism, such as returning the value in a different type (scaled to an integer, in a string, in a decimal floating-point format, et cetera).

I suspect an answer the interviewer may have been expecting is to evaluate (int)(x*10000+.5)*.0001. This scales the value so that the desired results are integers (the rounding quantum, .0001, is scaled to 1), adds .5, truncates to an integer, and reverses the scaling. The combination of adding .5 and truncating is nearly equivalent to rounding, except it requires x to be positive, rounds ties away from zero, and has range/domain issues. Adjustments for those can be made depending on the situation and desired behavior. And, of course, the answers are generally slightly incorrect, due to the inability to represent exact results.

A more interesting answer is to evaluate (x*10000+0x1p52-0x1p52)*.0001. This scales the value, rounds it to an integer, using the current floating-point rounding mode, and undoes the scaling. The rounding is effected because the common double type has 53 bits in its significand, so, when the high bit has value 0x1p52 (252), the low bit’s value is 0x1p0 (20, also known as 1). In order to produce the result of adding 0x1p52, the significand must be rounded, which is done automatically in the floating-point hardware. Then subtracting 0x1p52 removes the added number, leaving the rounded value. As before, there are caveats with range/domain and ensuring the compiler does double-precision arithmetic and not something more, and adjustments can be made. However, it is faster on some hardware than converting to integer and back.

Using fmod or string conversions has worse performance because they require division, which is slow on most common processors.

1

multiply by 10^numberOfDigitsAfterComma, convert to int and then back to float and divide by 10^...

double x = 3.009784654;
x =  ((double)((int)(x * 10000))) / 10000;
1

If you really need mod, you can do:

double number = 3.009784654;
double truncatedNumber = number - number % 0.0001;
0

An answer using mod:

double d = 1.23456789;
int digitsToKeep = 2;
double divisor = Math.Pow(10, digitsToKeep * -1);
double rounded = d - (d % divisor);
0

This also rounds the number up if that is needed.

public static double Round(double number, int digits)
{
    for (int i = 0; i < digits; i++)
        number *= 10;

    int whole = (int)number;
    double fraction = number - whole;

    if (fraction >= 0.5)
        whole++;

    number = whole;

    for (int i = 0; i < digits; i++)
        number /= 10.0;

    return number;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.