Tuesday, 15 January 2013

algorithm - Rounding of double to nearest member of an arithmetical progression? -



algorithm - Rounding of double to nearest member of an arithmetical progression? -

i have formula of sequence of double numbers k = + d * n, , d constant double values, n integer number, k >= 0, >= 0. example:

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

i want round given number c nearest value sequence lower c.

currently utilize this:

double somefunc(double c) { static double = 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; } } homecoming a1; }

is possible same without these awful cycles? inquire because next situation may appear:

abs(a - c) >> abs(d) (the first number much more sec 1 , lot of iterations possible)

my question similar next one. in case have a variable has influence on final result. means sequence may haven't number 0.

suppose c number in sequence. have n = (c - a) / d. since want integer <= c, take n = floor((c - a) / d). can round c to: + d * floor((c - a) / d)

suppose k = 3 + 5 * n , round c=21.

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

algorithm math language-agnostic sequence intervals

No comments:

Post a Comment