T-SQL Multiplcation

  • 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
  • Nice and easy -- thanks!

  • Good question. I learned something 🙂

    Kenneth FisherI 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/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

  • This was removed by the editor as SPAM

  • Try to use decimal(38,23) and you'll get the opposite result, but why? :hehe:

  • 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.

  • Good question, thanks for asking it! 🙂

  • Nice question! Almost missed it though 🙂

  • 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
    ----------------------------------------

  • 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-2 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-2 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-2 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

  • Nice and easy -- thanks!

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

  • Tom,

    Allow me to help you out a little here, you forgot your xml tags...

    <?xml version="1.0"?>

    <Tom>

    <Action="Rant">

    <Subject="ProgrammingFoolishness">

    L' Eomot Inversé (10/18/2011)


    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-2 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-2 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-2 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).

    </Subject>

    </Action>

    </Tom>

    😀

    I love your posts Tom. Not only do you crease me up with your epic sarcasm and forcefulness but you always teach me something very interesting!

    Ben

    ^ Thats me!

    ----------------------------------------
    01010111011010000110000101110100 01100001 0110001101101111011011010111000001101100011001010111010001100101 01110100011010010110110101100101 011101110110000101110011011101000110010101110010
    ----------------------------------------

  • L' Eomot Inversé (10/18/2011)


    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-2 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-2 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-2 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).

    I largely agree. And it is these little things that cause so many trip-ups. I wrote about it once upon a time[/url].

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

  • Guys,

    do you think for the examples given in this question, min(s1+s2,37-(p1+p2)) is the reduction policy as Tom suggests.

    SQLRNNR can you clarify Tom's point?

  • CoolCodeShare (10/18/2011)


    Guys,

    do you think for the examples given in this question, min(s1+s2,37-(p1+p2)) is the reduction policy as Tom suggests.

    SQLRNNR can you clarify Tom's point?

    That is the policy that Tom suggests it should be - but that is not.

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

Viewing 15 posts - 1 through 15 (of 36 total)

You must be logged in to reply to this topic. Login to reply