E
E
Evgeny Churakov2018-05-30 17:26:34
C++ / C#
Evgeny Churakov, 2018-05-30 17:26:34

What is the best way to round double with a given error/precision?

In the code I met such a rounding:

static class TraceConvertor
    {
        public const double ValueTolerance = 1e-2;
        public const int ValueDigitTolerance = 2;

        [ MethodImpl( MethodImplOptions.AggressiveInlining ) ]
        public static double RoundByTolerance( double value )
        {
            return Math.Round( value / ValueTolerance ) * ValueTolerance;
        }

        [ MethodImpl( MethodImplOptions.AggressiveInlining ) ]
        public static double RoundByDigitTolerance( double value )
        {
            return Math.Round( value, ValueDigitTolerance );
        }
    }

The first method I inherited, the second is mine.
I don't see any difference. But why is it done this way, maybe I don’t understand something and the first option will be faster?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2018-05-30
@d-stream

Hm... if "round" appears, then it would probably be more honest to use the decimal types
Well, as if Round is still faster than Round + division and multiplications (unless, of course, the optimizer leads all this to the same thing)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question