Jump to content
  • Advertisement
Sign in to follow this  
ClaudeFleming

I'm no mathematician, but I found this simple equation rather nifty!

This topic is 2570 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

"To round off an integer i to the next largest even multiple of another integer j, the following formula can be used:
Next_multiple = i + j - i % j

For example, to round off 256 days to the next largest number of days evenly divisible by a week,
values of i = 256 and j = 7 can be substituted into the preceding formula as follows:
next_multiple = 256 + 7 - 256 %7."

Kochan, Stephen G. (2004-07-08). Programming in C (3rd Edition) (Kindle Locations 1311-1314). Pearson Education (US). Kindle Edition.

Where are books that teach you interesting and useful math problems like these. I know school texts are useful, but I like applied math also. Yes, school texts teach applied math, but I want a masterpiece of mathematics literature that teaches you equations like these. I can understand this one after a bit of thought. But I would like a book that teaches you the 'what', the 'how', and the 'why'.

Thanks,

Lee

Share this post


Link to post
Share on other sites
Advertisement
OK, in C/C++, the order of operations is (in part): * / % + -

So, 256 + 7 - (256 % 7),
256 + 7 - (4),
263 - 4 = 259.

Share this post


Link to post
Share on other sites

[quote name='MarkS' timestamp='1322850855' post='4889849']I don't know how % fits in the order of operations in C/C++, but doesn't i + j - i = j?


% has higher precedence.
[/quote]

Already edited. Should have Googled first...unsure.gif

Share this post


Link to post
Share on other sites

"To round off an integer i to the next largest even multiple of another integer j, the following formula can be used:
Next_multiple = i + j - i % j

For example, to round off 256 days to the next largest number of days evenly divisible by a week,
values of i = 256 and j = 7 can be substituted into the preceding formula as follows:
next_multiple = 256 + 7 - 256 %7."

Kochan, Stephen G. (2004-07-08). Programming in C (3rd Edition) (Kindle Locations 1311-1314). Pearson Education (US). Kindle Edition.

Where are books that teach you interesting and useful math problems like these. I know school texts are useful, but I like applied math also. Yes, school texts teach applied math, but I want a masterpiece of mathematics literature that teaches you equations like these. I can understand this one after a bit of thought. But I would like a book that teaches you the 'what', the 'how', and the 'why'.

Thanks,

Lee


What do you mean by "next largest"?
Anyways, the algorithm finds the smallest multiple of j, that is bigger than i.
The algorithm is quite logical and straight forward.
If we want to find the biggest mutliple of j that is not larger than i, that means we're looking for a number
j*k <= i
k is an int, and so j*k is a multiple of j.
Our actual number i is either a multiple of j, or it is not a multiple of j, so:
i = j*k + rest
rest = 0 if our number is a multiple of j, else it has a value. In both cases rest = (i % j) - (this is what % does)
Now we have:
i = j*k + (i % j) <=> j*k = i - (i % j)
Remember j*k is the multiple of j that is smaller or equal to i.
If we want the smallest multiple of j that is larger than i, we simply have to add j:
answer = i + j - (i % j)


I consider this to be a more programming specific problem. What math can do is to teach you how solve such problems.
In my experience, I think they should start much earlier with things like these, rather than just memorize how to solve a specific problem.

Share this post


Link to post
Share on other sites



What do you mean by "next largest"?

I mean just what the author says. I didn't come up with that equation. That is from the author's own book, his own words.
I think this is important for every person to know, simply because it is elegant and a pleasure to know. That's sad you consider it a programming-related problem.
Not all people are smart enough to come up with that equation on their own. I love solving and especially understanding problems like these. Every mathematics curriculum
should have more and more problems like these in them.

Share this post


Link to post
Share on other sites
Numerical Recipes. Warning: while this book teaches all sorts of useful applications of math, particularly in a programming setting, it covers mostly more advanced topics. Still, in terms of your original question, it's the best I've found.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!