Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modulo (%) works with Decimal datatype but not with float or real. WHY?

Why modulo runs fine with decimal but not with float/real MSDN states about it "must be a valid expression of any one of the data types in the integer and monetary data type categories, or the numeric data type." why not floating values, because it is an approximate value ??

--Runs fine
declare @pri decimal
set @pri = 3.25
select @pri%2

Result 1

--Gives an error 402
declare @pri float
set @pri = 3.25
select @pri%2

Msg 402, Level 16, State 1, Line 3 The data types float and numeric are incompatible in the modulo operator.

like image 729
Shantanu Gupta Avatar asked Mar 06 '12 09:03

Shantanu Gupta


2 Answers

If your question about the documentation?

MSDN documentation

dividend must be a valid expression of any one of the data types in the integer and monetary data type categories, or the numeric data type.

It says integer, monetary (money, smallmoney) and numeric

So, decimal is not supported. The documentation says "numeric is functionally same as decimal" but may be it has different meaning in some context like this one.

I guess, float is treated as numeric datatype where as decimal is not

Read the data types. It has information on monetary data types. (money, smallmoney)

Update:

Here is the catagory wise data type list. You can follow the links to numeric and money to find what datatypes falls in it

like image 186
dejjub-AIS Avatar answered Oct 16 '22 05:10

dejjub-AIS


What are you trying to achieve here? Modulo is an integer operator, it computes the remainder of the integer division.

If you expect 1.25 as result convert pri to an integer, do the division (this will give you 1) multiply by 2 and subtract the result from pri (3.25 - ((3/2) *2).

like image 25
Stefan Avatar answered Oct 16 '22 07:10

Stefan