0

I'm working on a small algorithm, in fact a mathematical function in which I have to find a number, while starting by doubling another number.

For example, I have to find a range for 206, while starting from 8 and doubling it i.e.

8 + 8 -> 16 + 8 -> 24 + 8 -> 32 ...

At some point have to find the nearest number before 206 and then adding further 8 to that value would be required.

Is there any statistical or mathematical formula for that which I can use directly?

Farrukh Waheed
  • 2,163
  • 2
  • 29
  • 59

2 Answers2

3

Yes, it's called integer division.

result_lo = floor(206 / 8) * 8
result_hi = (floor(206 / 8) + 1) * 8
  • That's great.. Thanks @H2CO3... That seems to be giving results. – Farrukh Waheed Jun 30 '13 at 20:38
  • @FarrukhWaheed: Do you know why it's giving the results? – Aravind Jun 30 '13 at 20:39
  • Well, I'm very interested to know that as well. But just read about its inner side and its a horrible maths there. i.e. http://www.encyclopediaofmath.org/index.php/Floor_function – Farrukh Waheed Jun 30 '13 at 20:44
  • @FarrukhWaheed What's so horrible in truncating the fractional part of the result of a simple division? I don't understand exactly what you are having difficulties with. –  Jun 30 '13 at 20:45
  • @FarrukhWaheed Unless you have no idea about what division is, you should not be struggling with this. (And learn some maths if you are not good at it, a programmer needs lots of advanced maths!) –  Jun 30 '13 at 20:48
  • Well, Floor() is there now, so that saves a lot of struggle, (for the time being I guess). – Farrukh Waheed Jun 30 '13 at 20:51
  • I'm aware of division, multiplication, Algebra etc... but resolving equations like http://www.encyclopediaofmath.org/index.php/Floor_function, is still a horrible thing, just like it was in college. – Farrukh Waheed Jun 30 '13 at 20:53
  • This isn't accurate, and will fail if the target is a multiple of 8. 208, for instance, will give 208 and 216 with your method, while OP asked for 200 (the nearest number *before*...) and 208. – zmbq Jun 30 '13 at 20:58
  • @zmbq Surely that's not what he wants? At least it's unclear for the corner case, at best. –  Jun 30 '13 at 20:59
  • The question as posted, asked the lower part of the range to be less than the target. It is possible the question wasn't accurate enough, or @Farrukh doesn't really care one way or the other, but that would be second-guessing. – zmbq Jun 30 '13 at 21:03
  • @zmbq It would, and I think your assumption is also *kind of* second guessing. For me, it seems way more logical to let the lower bound equal to the target value in the ambiguous case, in a way analogous to [why programmers love half-open intervals.](http://stackoverflow.com/questions/11364533/why-are-slice-and-range-upper-bound-exclusive) –  Jun 30 '13 at 21:07
  • Oops.. sorry. I just logged in and while watch too many web pages, I started clicking on the sign to Actually Accept That.., while not observing the color of the sign.. So, didn't realize that I'm accepting another reply.. – Farrukh Waheed Aug 29 '13 at 06:42
2

Since you are having difficulties in understanding floor, let me briefly describe it to you.

floor(x) = the greatest integer less than or equal to x

So

floor(2.99) = 2
floor(2.00) = 2
floor(-0.99) = -1

Why floor(N/8)*8 works?

We want the greatest multiple of 8 less than or equal to N?

So we want to remove as many 8 as we possibly can until it is greater than 0.

206-8-8-8-8-8-....8=6

Beyond this if we subtract 8 it goes below 0.So we stop here.

An important observation is that division is simply repeated subtraction.

This means dividing 206/8 gives us the number of 8's that can be subtracted from 206.

When the input number is limited to positive numbers, then you can simply use integer division in C++. This is because, when you carry out integer division in C++, the result is truncated.

That is 8/3 will be set to 2.

Though 8/3 is 2.66, the result is only the integer part.The fractional part is simply ignored.(This is just a simple explanation, for negative integers, you need to follow )

So in C++, you can simply write:

((N)/8)*8

Please don't just use something because it works, try and find out why it works!

Aravind
  • 3,169
  • 3
  • 23
  • 37
  • 1
    @Farrukh Waheed, building upon Aravind's last sentence: If you don't see why it works, you can't know it really works.. It just does what you want it to do in the cases that you test it, that's all.. – Ioannis Jul 01 '13 at 09:08
  • Thanks Aravind for such detail... Will definitely work on this later as I'm now indulge in other tasks at this time.. .Really appreciated. – Farrukh Waheed Aug 29 '13 at 04:32