6

Unlike in C, in Java is the result of x/y and x%y well-defined even for negative operands. Surprisingly, it's defined by rounding towards zero, and not by rounding down (i.e., towards negative infinity). Does anybody have taken any advantage of this definition?

In most cases I just don't care, but sometimes I had to work around this, e.g., when computing an index using modulo array.length.

This is no rant, I'm really interested if there are uses for this definition.

asked Apr 15, 2011 at 21:39
4
  • 1
    that's how it's done in the hardware. one small thing, try to keep you array.length a power of 2 and use &(array.length-1) Commented Apr 15, 2011 at 21:50
  • A lot of people think it's important to preserve stuff like -(x/y) == -x/y == y/-x == -(-x/-y) Commented Apr 14, 2024 at 12:37
  • Note in both C (and C++) and Java % is rem(ainder) not mod(ulo). In C integer division involving negative was originally implementation-defined and in practice whatever the hardware did, but since 1999 the standard has required toward-zero. FORTRAN has required toward-zero since 1977 at least, and AIUI was similarly based on early hardware. Though a language can choose to be different than hardware; python does. Commented Oct 21, 2024 at 13:19
  • It’s been decades as it rounds towards 0 in c/c++ Commented Apr 12, 2025 at 7:45

3 Answers 3

7

It's easier to implement a division routine if you can round toward zero. Often, a division involving a negative is sign-flipped, and then the division carried out on a positive equivalent, and then the answer flipped back again. So the effect is naturally going to be round toward zero.

answered Apr 15, 2011 at 21:42
Sign up to request clarification or add additional context in comments.

2 Comments

That's true, however, such a definition is a common source of errors and doing it otherwise would not be that expensive. I still wonder if there's anybody having taken any advantage of this (strange but standard) definition?
@maaartinus: The first thing that comes to mind is that round-toward-zero will give unbiased quantization error (assuming data is evenly distributed between +ve and -ve), whereas round-toward-minus-infinity will result in a bias.
1

Here's one example where it's useful, however slightly:

maxPage = (rows.length - 1) / ROWS_PER_PAGE

(No need to special case for maxPage = 0 when rows.length = 0, assuming ROWS_PER_PAGE > 1.)

answered Jan 12, 2016 at 19:07

Comments

0

The problem with division of two integers is that the result may be a non-integer. This is opposed to the three other standard arithmetic operators (+,-,*). So you have to resolve this.

There are multiple ways to do this (truncation / round towards zero, floor/ceil division, Euclidean division). From a mathematical point of view, floor division or the closely related Euclidean division seem the most useful.

However, a big drawback of floor division is that it "behaves" less as the real-numbered division operation. In particular, arithmetic rewriting rules allow one to reorder divisions and multiplications. E.g., -(x/y) is the same as (-x)/y or x/(-y) for real-numbered division. Floor division does not have this property: -(1/2) is not the same as (-1)/2, as the former yields 0 and the latter -1. Division that rounds towards zero does preserve this reordering property, allowing greater freedom to apply classic arithmetic rewriting rules.

answered Apr 14, 2024 at 12:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.