Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why remainder(35,10) is -5 when remainder(25,10) is 5 in oracle?

I am running following query in oracle 11g using Sql Developer.

SELECT remainder(25,10),remainder(35,10) FROM dual;

Output

REMAINDER(25,10)       REMAINDER(35,10)       
---------------------- ---------------------- 
5                      -5                     

I can use MOD() to get the desired result, but my question is why it is returning +5 for one and -5 for other?

like image 486
Rajeev Avatar asked Apr 16 '15 10:04

Rajeev


1 Answers

According to the documentation, REMAINDER(m, n) is defined as

m - (n * X) where X is the integer nearest m / n

Apparently, Oracle applies "round to even" when the result of the division is halfway between two numbers:

For REMAINDER(25, 10), you get

25 / 10 = 2.5 --> nearest integer is 2 (round to even)
25 - (10 * 2) = 5

For REMAINDER(35, 10), you get

35 / 10 = 3.5 --> nearest integer is 4 (round to even)
35 - (10 * 4) = -5
like image 98
pascalhein Avatar answered Nov 17 '22 22:11

pascalhein