2

I need theoretical answer using mathematical approach. Is there any mathematical difference between them and how do they make impact in programming languages.

Sparsh Singhal
  • 127
  • 1
  • 5
  • Depends on your notion of *Glorified Subtraction*. What do you mean by it ? – dumbPotato21 May 30 '17 at 19:30
  • 1
    @ChandlerBing As multiplying is just glorified addition as `5*5` is just `5+5+5+5+5`, similarly division is just glorified subtraction. `12/3` is `12-3=9(1) 9-3=6(2) 6-3=3(3) 3-3=0(4)` and thus ***4*** is our answer. – Sparsh Singhal May 30 '17 at 19:38
  • Whoever simplifies multiplication as *glorified addition* is not considering negative numbers. E.g. if `3 * 7 = 7 + 7 + 7`, then what is `-3 * 7`? You can't add `7` with itself `-3` times. Same goes for simplification of division as *glorified subtraction*. So, stop trying to simplify. Or if you insist, extract the negative sign first, they apply again after. --- Also, the question title is supposed to be a *summary* of the question text, not an entirely different question. – Andreas May 30 '17 at 19:39
  • As for `1/2=0.5`, yes there is such an operator in Java. It's `/` (division) of floating-point numbers. You just have to encourage it to not be integer math, but floating-point math, by making at least one of the operands a floating-point number, e.g. `1.0/2=0.5`, `1/2d=0.5`, `1/(float)2=0.5`, etc. See "[Division of integers in Java](https://stackoverflow.com/q/7220681/5221149)". – Andreas May 30 '17 at 19:42
  • @Andreas Please check the main question now. And I wanted to know how does any programming language result in `5/-3 = -1`. And if it is dependent on any of these division methods. – Sparsh Singhal May 30 '17 at 20:02
  • @SparshSinghal "Any" programming language result in `5/-3 = -1` by defining that the language should do so. See [my answer](https://stackoverflow.com/a/44271386/5221149). – Andreas May 30 '17 at 20:31

2 Answers2

6

According to Java terminology, the question of "Truncated Division" vs "Floored division" is best answered by the javadoc of RoundingMode:

DOWN
Rounding mode to round towards zero. Never increments the digit prior to a discarded fraction (i.e., truncates). Note that this rounding mode never increases the magnitude of the calculated value.

FLOOR
Rounding mode to round towards negative infinity. If the result is positive, behave as for RoundingMode.DOWN; if negative, behave as for RoundingMode.UP. Note that this rounding mode never increases the calculated value.

The Java division operator is defined by JLS §15.17.2. Division Operator /:

Integer division rounds toward 0.

That is why 5 / -3 result in -1.


You can also look at the definition of "Truncate" vs "Floor" on Wikipedia:

There are many ways of rounding a number y to an integer q The most common ones are:

  • round down (or take the floor, or round towards minus infinity): q is the largest integer that does not exceed y.

  • round up (or take the ceiling, or round towards plus infinity): q is the smallest integer that is not less than y.

  • round towards zero (or truncate, or round away from infinity): q is the integer part of y, without its fraction digits.

  • round away from zero (or round towards infinity): if y is an integer, q is y; else q is the integer that is closest to 0 and is such that y is between 0 and q.

  • round to nearest: q is the integer that is closest to y (see below for tie-breaking rules).

As you can see, Java and Wikipedia agrees on this definition:

  • Truncate: Round towards zero
  • Floor: Round towards minus/negative infinity

Note that Java and Wikipedia disagrees on what Round Down means.

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
-1

You're dividing integers, so the result is going to be rounded to the nearest integer. Use floating point variables instead.

For example, the second part of your question, 1.0/2.0 = 0.5.

Jackson F
  • 48
  • 1
  • 6
  • You should rephrase the first part of your answer. The result is not rounded, otherwise 2/3 would be 1. It is rounded down when positive, and rounded up when negative. So it is round towards 0. – Malte Hartwig May 30 '17 at 19:36
  • 1
    The question has been closed with a duplicate flag that doesn't even answer the actual question in the title, so I'll add add some info here: Read the JavaDoc of `Math.floorDiv()`. It explains that floored division always rounds down after dividing, while truncated division (the normal `/` operator) always rounds towards 0 (i.e. cuts off everything behind the decimal point), – Malte Hartwig May 30 '17 at 19:52