Rounding integer division without logical operators

Michael Dorner

I want a function

int rounded_division(const int a, const int b) { 
    return round(1.0 * a/b); 
}

So we have, for example,

rounded_division(3, 2) // = 2
rounded_division(2, 2) // = 1
rounded_division(1, 2) // = 1
rounded_division(0, 2) // = 0
rounded_division(-1, 2) // = -1
rounded_division(-2, 2) // = -1
rounded_division(-3, -2) // = 2

Or in code, where a and b are 32 bit signed integers:

int rounded_division(const int a, const int b) {
    return ((a < 0) ^ (b < 0)) ? ((a - b / 2) / b) : ((a + b / 2) / b);
}

And here comes the tricky part: How to implement this guy efficiently (not using larger 64 bit values) and without a logical operators such as ?:, &&, ...? Is it possible at all?

The reason why I am wondering of avoiding logical operators, because the processor I have to implement this function for, has no conditional instructions (more about missing conditional instructions on ARM.).

chux - Reinstate Monica

a/b + a%b/(b/2 + b%2) works quite well - not failed in billion+ test cases. It meets all OP's goals: No overflow, no long long, no branching, works over entire range of int when a/b is defined.

No 32-bit dependency. If using C99 or later, no implementation behavior restrictions.

int rounded_division(int a, int b) {
  int q = a / b;
  int r = a % b;
  return q + r/(b/2 + b%2);
}

This works with 2's complement, 1s' complement and sign-magnitude as all operations are math ones.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rounding up integer without using float, double, or division

From Dev

VB Script Integer division vs Float Division Rounding up

From Dev

Integer division without using the / or * operator

From Dev

Logical comparison operators without control statements

From Dev

Determine triangle type WITHOUT logical operators JAVA

From Dev

Integer arithmetic produces a strange result (rounding after division?)

From Dev

Logical AND (&&) and OR (||) operators

From Dev

Logical AND (&&) and OR (||) operators

From Dev

Logical operators

From Dev

Logical Operators and increment operators

From Java

How to inverse a positive integer in C without * or - operators?

From Java

How to inverse a positive integer in C without * or - operators?

From Java

How to negate a positive integer in C without * or - operators?

From Dev

How to print 2 characters in C without using logical operators?

From Dev

C# rounding with division

From Dev

SSRS Division Rounding

From Dev

BigDecimal division is rounding up

From Dev

Determine a menu choice without using logical operators, relational operators, or selection constructs

From Dev

Python float division "rounding error" on division by 100

From Dev

What is the type of the logical operators?

From Dev

Logical operators with three lists

From Dev

Logical Operators in Tweepy Filter

From Dev

Precedence of the shell logical operators &&, ||

From Dev

Combining logical operators in Ruby

From Dev

Are logical operators in assertions acceptable?

From Dev

Vectorizing logical operators in Excel

From Dev

Logical operators on enums

From Dev

Gradients of Logical Operators in Tensorflow

From Dev

Increment and logical operators precedence

Related Related

HotTag

Archive