Jump to content

Integer Division and Inequalities


MJW
 Share

Recommended Posts

To some this might seem trivial, but I think it could be useful.

 

Suppose n and d are positive integers. Let n/d represent floating-point division and [n/d] represent (truncated) integer division.

 

Suppose I want to find,

The largest integer less than n/d;

The largest integer less than or equal to n/d;

The smallest integer greater than or equal to n/d;

The smallest integer greater than n/d.

----------

 

1) The largest integer less than or equal to n/d.

 

This one is easy. It's just [n/d]

 

2) The largest integer less than n/d.

 

The only case where [n/d] is not less than n/d is when d evenly divides n. If we subtract 1 from n, it will reduce the result by 1 for the evenly divisible values of n and leave the other values unchanged. However, care must be taken if n can be 0, since division of both positive and negative values truncates toward 0. In that case, we can add d to the numerator, and then subtract 1 from the truncated division. So, the correct value is:

 

[(n - 1)/d] or,

[(n + d - 1)/d] - 1 if n could be 0.

 

3) The smallest integer greater than n/d.

 

This is [n/d] + 1 (or [(n + d)/d])

 

4) The smallest integer greater than or equal to n/d.

 

The situation is the same smallest integer greater than n/d, except the result for evenly divisible values of n must be reduced by 1. Using the same method as the largest integer less than n/d, the value is:

 

[(n - 1)/d] + 1 or

[(n + d - 1)/d] if n could be 0.

 

----------

 

Summarizing.

 

The largest integer less than n/d: [(n - 1)/d] or [(n + d - 1)/d] - 1

The largest integer less than or equal to n/d: [n/d]

The smallest integer greater than or equal to n/d:  [(n - 1)/d] + 1 or [(n + d - 1)/d]

The smallest integer greater than n/d: [n/d] + 1

  • Upvote 1
Link to comment
Share on other sites

Actually, I plan to do that. I thought I'd write a comment explaining how the Color Clearer plugin works. I found the problem of maximizing the transparency while exactly matching the original blended pixels to be interesting, and the techniques might be useful for other plugins. It's usually simpler to do everything in floating point, but because that's not how PDN does the blending computations, the results don't always exactly match.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...