Modulo (%) works with decimal data type, but not with float or real. WHAT FOR?

Why modulo works fine with decimal, but not with float / real MSDN claims about this " must be a valid expression of any of the data types in the categories of integer and monetary data types, or a 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 not compatible in the operator module.

+6
source share
4 answers

If your question is about documentation?

MSDN Documentation

A dividend must be a valid expression of any of the data types into integer and monetary data data types or numeric data types.

He says that the whole, money (money, smallmoney) and numerical

Therefore, the decimal value is not supported. The documentation states that "a numerical value is functionally the same as a decimal", but maybe it has a different meaning in some context, such as this.

I assume that float is treated as a numeric data type, where as decimal is not

Read the data types . It contains information about the types of cash data. (money, small money)

Update:

The following is a list of the list of data types. You can follow the links to numbers and money to find what types of data fall into it.

+2
source

Because float and real represent real numbers that can always be separated without a remainder. Modulo definition:

a mod n = r if a = n*q + r , where a , n , q and r are integers , for the smallest possible absolute value of r .

Take a look at http://en.wikipedia.org/wiki/Modulo_operation

For other, non-integer, but fixed point variables, a modulo definition can be found because the two values โ€‹โ€‹of a fixed point cannot always be separated without a remainder. However, usually you want to use modulo for integer arithmetic, otherwise you will get different results from the expected.

+1
source

Perhaps the restriction is intended to protect you from unexpected results. Modulo operations tend to produce unexpected results when used with inaccurate arithmetic.

Integer and monetary numeric types are suitable for exact arithmetic, while binary floating-point types are not suitable. For example, do you expect 0.3% 0.01 to give you exactly zero, a number close to zero, or a number close to 0.01? Money types can do this for sure. Binary floating point results are more surprising.

+1
source

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

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

0
source

Source: https://habr.com/ru/post/910057/


All Articles