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


T-SQL Multiplcation


T-SQL Multiplcation

Author
Message
Mark Cowne
Mark Cowne
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14734 Visits: 26572
Comments posted to this topic are about the item T-SQL Multiplcation

____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Revenant
Revenant
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22455 Visits: 5135
Nice and easy -- thanks!
Kenneth Fisher
Kenneth Fisher
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12365 Visits: 2121
Good question. I learned something Smile

Kenneth Fisher
I was once offered a wizards hat but it got in the way of my dunce cap.
--------------------------------------------------------------------------------
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/Link to my Blog Post --> www.SQLStudies.com
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35380 Visits: 7702
Good question
thanks

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
vkarpiv
vkarpiv
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 46
Try to use decimal(38,23) and you'll get the opposite result, but why? Hehe
CoolCodeShare
CoolCodeShare
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2056 Visits: 311
vkarpiv (10/18/2011)
Try to use decimal(38,23) and you'll get the opposite result, but why? Hehe


Interesting question!!

BOL is very confusing. I think it's not being revised by MS. please refer url : http://msdn.microsoft.com/en-us/library/ms190476(v=SQL.100).aspx.

They didn't bother to update this line to specify which earlier versions.
In SQL Server, the default maximum precision of numeric and decimal data types is 38. In earlier versions of SQL Server, the default maximum is 28.


Anyways it seems like when the precision is greater than 38, they reduce it by scaling and ensuring that integral part is not truncated.

This scaling is the main thing why (38,23) is working and (38,20) not.
Håvard
Håvard
Old Hand
Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)

Group: General Forum Members
Points: 343 Visits: 142
Good question, thanks for asking it! :-)
Kwex
Kwex
SSC Eights!
SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)

Group: General Forum Members
Points: 817 Visits: 240
Nice question! Almost missed it though :-)
BenWard
BenWard
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2920 Visits: 827
I guessed it would be different due to some kind of precision thing - I now understand why that is the case.

Not sure where this will come in handy for me personally but I like to know how it works. Thanks.

Ben

^ Thats me!


----------------------------------------
01010111011010000110000101110100 01100001 0110001101101111011011010111000001101100011001010111010001100101 01110100011010010110110101100101 011101110110000101110011011101000110010101110010
----------------------------------------
Tom Thomson
Tom Thomson
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50676 Visits: 13159
Good interesting question.

Just shows what bizarre and ridiculous results you can get because of the decision not to attempt accuracy with "exact" numerics.

Try it with DECLARE @a decimal(38,33) (or decimal(38,1), for that matter)
DECLARE @b decimal(38,33)
and you will get a different result.

It's quite thoroughly broken. If the calculation of scale for multiplication were sensible
[new scale = min(s1+s2,37-(p1+p2)) is the easy option, based on exactly the same scale reduction policy as was chosen for addition and subtraction] it would be less thoroughly broken and this particular query would have a different (more useful) answer.

Of course with DECLARE @a float(25) (or any legal float definition for that matter) and DECLARE @b float(25) (or float(53), or anything in between) you get 12345.123456699999224, which is an error of about only 7 parts in a quadrillion, roughly 11.2 million times as accurate as the result using decimal(38,20). So for this calculation using ordinary 8 byte floats gives a vastly nore accurate result than using 17 byte "exact numerics" unless you juggle your scale very carefully. Perhaps that will make some of those who shriek "never use float - it's approximate" think again - or perhaps not. Using Float(24) (4-byte floats) for @b would of course bring in a much bigger error, but anyone who expects 12 significant (ecimal) digits out of a 23 bit significand can't do simple arithmetic.

I wonder when SQL will catch up with IEEE 754-2008 and offer decimal based (instead of binary based) floating point so that we can have the flexibility and accuracy and error detection and reporting associated with IEE 754 while avoiding the input and output rounding errors caused by converting into binary-based floating point. I expect it will take a long time, as SQL standardisation is a notoriously slow and clumsy process (at least it was in the early 90s, and I would be very surprised if it's any different now).

EDIT: Oh careless me. That's the wrong formula - using that would be even worse than the present mess!

Tom

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