Home » SQL & PL/SQL » SQL & PL/SQL » REMAINDER function return negative value
icon5.gif  REMAINDER function return negative value [message #167251] Wed, 12 April 2006 02:19 Go to next message
sanka_yanka
Messages: 184
Registered: October 2005
Location: Kolkata
Senior Member

Hi all,

I m trying to get REMAINDER from two experession using pre-defined function in Oracle as
select REMAINDER(3,2) from dual

but I'm getting a negative value -1.
I've tryed also
select REMAINDER(2,3) from dual
but it is also return neagative value.

If i query
select REMAINDER(5,2) from dual
then it returns 1.
But in the documentation I've read

REMAINDER(<m_value>, <n_value>)
Returns the modulus of a number (the remainder from dividing m by n. Same as mod except uses ROUND and doesn't use floor.

Please clarify me why it return negative number.
It's urgent.

Thanks & Regards
Sanka
Re: REMAINDER function return negative value [message #167255 is a reply to message #167251] Wed, 12 April 2006 02:39 Go to previous messageGo to next message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
From the manual

Purpose

REMAINDER returns the remainder of m divided by n.

This function takes as arguments any numeric datatype or any nonnumeric datatype that can be implicitly converted to a numeric datatype. Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that datatype, and returns that datatype.

The MOD function is similar to REMAINDER except that it uses FLOOR in its formula, whereas REMAINDER uses ROUND. Please refer to MOD .


See Also:

Table 2-11, "Implicit Type Conversion Matrix" for more information on implicit conversion and "Numeric Precedence " for information on numeric precedence


If n = 0 or m = infinity, then Oracle returns

An error if the arguments are of type NUMBER

NaN if the arguments are BINARY_FLOAT or BINARY_DOUBLE.

If n != 0, then the remainder is m - (n*N) where N is the integer nearest m/n.

If m is a floating-point number, and if the remainder is 0, then the sign of the remainder is the sign of m. Remainders of 0 are unsigned for NUMBER values.


Note the bolded section:
REMAINDER(3, 2) => m=3, n=2

If n != 0 (it is!) then remainder = m - (n*N) where N is the integer nearest m/n.
m/n = 1.5, the nearest integer to 1.5 is 2, so:

remainder = 3 - (2*2) = -1

Non-intuitive, but exactly as documented.

Use MOD(3,2) if you dont like it.

Ross Leishman
Re: REMAINDER function return negative value [message #167267 is a reply to message #167255] Wed, 12 April 2006 03:46 Go to previous message
sanka_yanka
Messages: 184
Registered: October 2005
Location: Kolkata
Senior Member

Thanks Rleishman,
I have understood it. Thanks again.

Sanka
Previous Topic: ORA - 306500
Next Topic: Please help me in this SQL given with example
Goto Forum:
  


Current Time: Wed Aug 20 11:34:10 CDT 2025