0

I have a formula of a sequence of double numbers k = a + d * n, where a and d are constant double values, n is an integer number, k >= 0, a >= 0. For example:

..., 300, 301.6, 303.2, 304.8, 306.4, ...

I want to round a given number c to a nearest value from this sequence which is lower than c.

Currently I use something like this:

double someFunc(double c) {

    static double a = 1;
    static double d = 2;
    int n = 0;
    double a1 = a;
    if (c >= a) {

        while (a1 < c) {

            a1 += d;
        }
        a1 -= d;
    } else {

        while (a1 > c) {

            a1 -= d;
        }
    }
    return a1;
}

Is it possible to do the same without these awful cycles? I ask because the following situation may appear:

abs(a - c) >> abs(d) (the first number is much more then the second one and so a lot of iterations possible)

My question is similar to the following one. But in my case I also have a a variable which has influence on the final result. It means that a sequence may haven't number 0.

Community
  • 1
  • 1
Gargo
  • 1,135
  • 1
  • 10
  • 21
  • 2
    Use some maths and compute `floor((c-a)/d)` which will give you an approximation to `n`. Then calculate `a+d*n`. I'll leave the fiddly tolerance and f-p issues to you. – High Performance Mark Nov 01 '14 at 16:05
  • You could put the sequence into an array and use a binary search algorithm like [this one](http://www.java2s.com/Code/Java/Collections-Data-Structure/Findtheindexofthearraynearesttothevalue.htm) to quickly find the element (index) you're looking for. I think the min complexity will remain `O(n)`. – Jean-Paul Nov 01 '14 at 16:05

1 Answers1

3

Suppose c is a number in your sequence. Then you have n = (c - a) / d. Since you want an integer <= c, then take n = floor((c - a) / d). Then you can round c to: a + d * floor((c - a) / d)

Suppose k = 3 + 5 * n and you round c=21.

And 3 + 5 * floor((21 - 3) / 5) = 3 + 5 * 3 = 18

M. Page
  • 2,694
  • 2
  • 20
  • 35
  • Might want to account for rounding errors by checking `n-1` and `n+1` in addition to `n` (as hinted in [a comment](http://stackoverflow.com/questions/26691053/rounding-of-double-to-nearest-member-of-an-arithmetical-progression#comment41978647_26691053)) – anatolyg Nov 04 '14 at 16:38