SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Round up or down I


Round up or down I

Author
Message
Oleg Netchaev
Oleg Netchaev
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3078 Visits: 1910
mtillman-921105 (8/3/2010)
However, I can accept your line of reasoning, but I still don't agree.

I can easily accept your line. I know, however, that it happens more often than not that we do have to agree with whatever implementation is, regardless whether it is consistent between different languages or not. If I try to use this in T-SQL:

declare @i smallint, @j smallint;
select @i = 32767, @j = 2;
print @i * @j;


I get "Arithmetic overflow error converting expression to data type smallint" error. When I try to use similar in C#:

short i = 32767;
i *= (short)2;
Console.WriteLine(i);


I get -2 printed in the Console window. I understand that neither of the above is what someone would perceive as reasonable, but I still accept both implementations. T-SQL does not allow overflows while C# simply wraps around (kinda like the sine) which is a great feature and it is used extensively in cryptography. So short 32767 + 2 is equal to -32767, and if I don't like it, I can force the T-SQL like behaviour by including the manipulation into checked {} block.

Oleg
Hugo Kornelis
Hugo Kornelis
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34338 Visits: 13119
mtillman-921105 (8/3/2010)
Third, us humans don't think this way typically I don't believe (thus, the good main question here). Since SQL is our tool, we should dictate how it works rather than vice-versa.

Which, for the record, can be done by explicitly casting the arguments to the desired data type. Which you'll only do if you know how the tool works.

Thanks for the interesting discussion, mtillman and Oleg! :-)


Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
mtillman-921105
mtillman-921105
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3232 Visits: 3854
Hugo Kornelis (8/3/2010)
mtillman-921105 (8/3/2010)
Third, us humans don't think this way typically I don't believe (thus, the good main question here). Since SQL is our tool, we should dictate how it works rather than vice-versa.

Which, for the record, can be done by explicitly casting the arguments to the desired data type. Which you'll only do if you know how the tool works.

Thanks for the interesting discussion, mtillman and Oleg! :-)



You're welcome Hugo. Cool Thanks for the terrific question too!

______________________________________________________________________
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)

Group: General Forum Members
Points: 144417 Visits: 18651
nice question. Thanks for the explanation.

Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw
Learn Extended Events

Bhuvnesh
Bhuvnesh
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32344 Visits: 4079
i loved this internal hidden casting in case of integer values

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Duncan Pryde
Duncan Pryde
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5384 Visits: 1552
Completely missed what the question was asking until after I'd answered it. Spent far too long looking at the ROUND() part of it, and not the integer division part.

I should have seen it was worth 2 points and paid more attention. Or if the questions still showed who wrote them actually on the page (rather than just in the email, which I don't always look at) I could have seen it was by Hugo and stopped for a second before hitting submit. ;-)

Anyway, good question, it's important to be reminded of these things once in a while.

Duncan
Dennissinned
Dennissinned
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1174 Visits: 289
Good Question. Thank you
Jamie-2229
Jamie-2229
SSCertifiable
SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)

Group: General Forum Members
Points: 5563 Visits: 862
Ditto... good question. Sql rounding is confusing when you have been trained to add .5 to an integer to round - as it doesn't work that way. Questions like this are fascinating. Exposes the shell and opens it up to scrutiny.

Jamie
terrykzncs
terrykzncs
SSC Eights!
SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)

Group: General Forum Members
Points: 916 Visits: 134
This is a good question. Thanks.

It should have been easy to spot the conversion to Int, but I was more focussed on the 1 in the ROUND function. Blush
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search