Select 1/2 = 0

# Thread: Select 1/2 = 0

1. Member
Join Date
Dec 1969
Posts
79

## Select 1/2 = 0

What would cause this?<BR><BR>declare<BR>@number1 int,<BR>@number2 int,<BR>@ratio float<BR><BR>set @number1 = 1<BR>set @number2 = 2<BR><BR>select @ratio = (@number1/@number2)<BR><BR>select @ratio -- result is 0 instead of .5

2. Senior Member
Join Date
Dec 1969
Posts
7,686

## Precedence of datatype

when you divide two int&#039;s the result is an int. Check &#039;data type preference&#039; in Books online<BR><BR>select @ratio = cast(@number1 as float)/cast(@number2 as float)

3. Senior Member
Join Date
Dec 1969
Posts
11,334

## I wonder why that is...

You would think it would coerce to whatever the result&#039;s data type is.<BR><BR>Mind you, this isn&#039;t unique to SQL, because both C++ and Java will return an int. I think... hmmm.... I&#039;ll have to check on that.

4. Senior Member
Join Date
Dec 1969
Posts
7,686

## RE: I wonder why that is...

when using two variables of the same datatype on an operator the result of the operation has that datatype.

5. Senior Member
Join Date
Dec 1969
Posts
7,686

## RE: I wonder why that is...

but don&#039;t ask me why :-)

6. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: I wonder why that is...

Yeah, just checked... Java and C++ do it as well. Only works correctly when you coerce it.<BR><BR>How odd. The output is a float type...<BR><BR>int x = 1;<BR>int y = 2;<BR>float z = x / 2;<BR><BR>Output is 0.0<BR><BR>One of those things that I just kinda "knew", but never gave it much thought.

7. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## I actually just recently..

.. read about this in my Java book.<BR><BR>And, just had issues with some math calculations in a Thumbnail program I was working on because of it.<BR><BR>I had to convert each of the variables to their end types while I was doing the math:<BR><BR>int x = 1;<BR>int y = 2;<BR>float z = (float)x / (float)y; // or whatever the syntax would be

8. Senior Member
Join Date
Dec 1969
Posts
11,334

## It gets weirder

Heh, now I just tried this (in Java... I think C++ will give a warning instead of an error, but Java throws an error)<BR><BR>int y = 1;<BR><BR>float x = y * .5;<BR><BR>test.java:53: possible loss of precision<BR>found : double<BR>required: float<BR> x = y * .5;<BR><BR> ^<BR><BR><BR>You would think it would complain about 1 / 2 as well, but that goes through... weird.<BR>

9. Senior Member
Join Date
Dec 1969
Posts
11,334

## You don't have to go that far

Once you coerce one, that&#039;s all you have to do.<BR><BR>int x = 1;<BR>int y = 2;<BR><BR>float z = (float) x / y; //only one time<BR><BR><BR><BR>That other example I just gave down below, once I changed it to a double, it compiled fine.

10. Senior Member
Join Date
Dec 1969
Posts
11,334

## And even weirder

double x;<BR>int y = 1;<BR> x = y * .5;<BR><BR>Without the coersion now, the result is 0.5<BR><BR><BR>Ugh, now I&#039;m confused.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•