Non-deterministic sum of floats The Next CEO of Stack OverflowAre RANK() and DENSE_RANK() deterministic or non-deterministic?SQL join with multiple columns as FK to same list tableMySQL returns floats roundedSelect data divided in groups evenly distributed by valueRound-tripping column datatype causes size of table to growCan I make this multiple join query faster?A Dynamic where clause in MS SQL ServerPerformance gap between WHERE IN (1,2,3,4) vs IN (select * from STRING_SPLIT('1,2,3,4',','))How to move a Daily Partition to a Monthly Partitioned TableTrying to insert only unique items into one table and then create a many to many relationship in a join table only if that join does not exist

A "random" question: usage of "random" as adjective in Spanish

Indicator light circuit

If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?

Cannot send `--` into tmux panes from vim

How do I go from 300 unfinished/half written blog posts, to published posts?

Does it take more energy to get to Venus or to Mars?

Unreliable Magic - Is it worth it?

Complex fractions

Why do airplanes bank sharply to the right after air-to-air refueling?

How to log in to Centos 7 using RDP from Win10

What exact does MIB represent in SNMP? How is it different from OID?

Why does the UK parliament need a vote on the political declaration?

Why do remote companies require working in the US?

What was the first Unix version to run on a microcomputer?

How to count occurrences of text in a file?

How do we know the LHC results are robust?

Anatomically Correct Strange Women In Ponds Distributing Swords

Elegant way to replace substring in a regex with optional groups in Python?

Rotate a column

SOQL: Aggregate, Grouping By and WHERE Clauses not working

Can I equip Skullclamp on a creature I am sacrificing?

What do "high sea" and "carry" mean in this sentence?

Received an invoice from my ex-employer billing me for training

Giving the same color to different shapefiles in QGIS



Non-deterministic sum of floats



The Next CEO of Stack OverflowAre RANK() and DENSE_RANK() deterministic or non-deterministic?SQL join with multiple columns as FK to same list tableMySQL returns floats roundedSelect data divided in groups evenly distributed by valueRound-tripping column datatype causes size of table to growCan I make this multiple join query faster?A Dynamic where clause in MS SQL ServerPerformance gap between WHERE IN (1,2,3,4) vs IN (select * from STRING_SPLIT('1,2,3,4',','))How to move a Daily Partition to a Monthly Partitioned TableTrying to insert only unique items into one table and then create a many to many relationship in a join table only if that join does not exist










9















Let me state the obvious fist: I completely understand that floating point types cannot accurately represent decimal values. This is not about that! Nevertheless, floating point calculations are supposed to be deterministic.



Now that this is out of the way, let me show you the curious case I observed today. I have a list of floating-point values, and I want to sum them up:



CREATE TABLE #someFloats (val float);
INSERT INTO #someFloats (val) VALUES (1), (1), (1.2), (1.2), (1.2), (3), (5);

SELECT STR(SUM(#someFloats.val), 30, 15) FROM #someFloats;

DROP TABLE #someFloats;

-- yields:
-- 13.600000000000001


So far, so good - no surprises here. We all know that 1.2 can't be represented exactly in binary representation, so the "imprecise" result is expected.



Now the following strange thing happens when I left-join another table:



CREATE TABLE #A (a int);
INSERT INTO #A (a) VALUES (1), (2);

CREATE TABLE #someFloats (val float);
INSERT INTO #someFloats (val) VALUES (1), (1), (1.2), (1.2), (1.2), (3), (5);

SELECT #A.a, STR(SUM(#someFloats.val), 30, 15)
FROM #someFloats LEFT JOIN #A ON 1 = 1
GROUP BY #A.a;

DROP TABLE #someFloats;
DROP TABLE #A;

-- yields
-- 1 13.600000000000001
-- 2 13.599999999999998


(sql fiddle, you can also see the execution plan there)



I have the same sum over the same values, but a different floating-point error. If I add more rows to table #A, we can see that the value alternates between those two values. I was only able to reproduce this issue with a LEFT JOIN; INNER JOIN works as expected here.



This is inconvenient, because it means that a DISTINCT, GROUP BY or PIVOT sees them as different values (which is actually how we discovered this issue).



The obvious solution is to round the value, but I'm curious: Is there a logical explanation for this behavior?










share|improve this question


























    9















    Let me state the obvious fist: I completely understand that floating point types cannot accurately represent decimal values. This is not about that! Nevertheless, floating point calculations are supposed to be deterministic.



    Now that this is out of the way, let me show you the curious case I observed today. I have a list of floating-point values, and I want to sum them up:



    CREATE TABLE #someFloats (val float);
    INSERT INTO #someFloats (val) VALUES (1), (1), (1.2), (1.2), (1.2), (3), (5);

    SELECT STR(SUM(#someFloats.val), 30, 15) FROM #someFloats;

    DROP TABLE #someFloats;

    -- yields:
    -- 13.600000000000001


    So far, so good - no surprises here. We all know that 1.2 can't be represented exactly in binary representation, so the "imprecise" result is expected.



    Now the following strange thing happens when I left-join another table:



    CREATE TABLE #A (a int);
    INSERT INTO #A (a) VALUES (1), (2);

    CREATE TABLE #someFloats (val float);
    INSERT INTO #someFloats (val) VALUES (1), (1), (1.2), (1.2), (1.2), (3), (5);

    SELECT #A.a, STR(SUM(#someFloats.val), 30, 15)
    FROM #someFloats LEFT JOIN #A ON 1 = 1
    GROUP BY #A.a;

    DROP TABLE #someFloats;
    DROP TABLE #A;

    -- yields
    -- 1 13.600000000000001
    -- 2 13.599999999999998


    (sql fiddle, you can also see the execution plan there)



    I have the same sum over the same values, but a different floating-point error. If I add more rows to table #A, we can see that the value alternates between those two values. I was only able to reproduce this issue with a LEFT JOIN; INNER JOIN works as expected here.



    This is inconvenient, because it means that a DISTINCT, GROUP BY or PIVOT sees them as different values (which is actually how we discovered this issue).



    The obvious solution is to round the value, but I'm curious: Is there a logical explanation for this behavior?










    share|improve this question
























      9












      9








      9


      1






      Let me state the obvious fist: I completely understand that floating point types cannot accurately represent decimal values. This is not about that! Nevertheless, floating point calculations are supposed to be deterministic.



      Now that this is out of the way, let me show you the curious case I observed today. I have a list of floating-point values, and I want to sum them up:



      CREATE TABLE #someFloats (val float);
      INSERT INTO #someFloats (val) VALUES (1), (1), (1.2), (1.2), (1.2), (3), (5);

      SELECT STR(SUM(#someFloats.val), 30, 15) FROM #someFloats;

      DROP TABLE #someFloats;

      -- yields:
      -- 13.600000000000001


      So far, so good - no surprises here. We all know that 1.2 can't be represented exactly in binary representation, so the "imprecise" result is expected.



      Now the following strange thing happens when I left-join another table:



      CREATE TABLE #A (a int);
      INSERT INTO #A (a) VALUES (1), (2);

      CREATE TABLE #someFloats (val float);
      INSERT INTO #someFloats (val) VALUES (1), (1), (1.2), (1.2), (1.2), (3), (5);

      SELECT #A.a, STR(SUM(#someFloats.val), 30, 15)
      FROM #someFloats LEFT JOIN #A ON 1 = 1
      GROUP BY #A.a;

      DROP TABLE #someFloats;
      DROP TABLE #A;

      -- yields
      -- 1 13.600000000000001
      -- 2 13.599999999999998


      (sql fiddle, you can also see the execution plan there)



      I have the same sum over the same values, but a different floating-point error. If I add more rows to table #A, we can see that the value alternates between those two values. I was only able to reproduce this issue with a LEFT JOIN; INNER JOIN works as expected here.



      This is inconvenient, because it means that a DISTINCT, GROUP BY or PIVOT sees them as different values (which is actually how we discovered this issue).



      The obvious solution is to round the value, but I'm curious: Is there a logical explanation for this behavior?










      share|improve this question














      Let me state the obvious fist: I completely understand that floating point types cannot accurately represent decimal values. This is not about that! Nevertheless, floating point calculations are supposed to be deterministic.



      Now that this is out of the way, let me show you the curious case I observed today. I have a list of floating-point values, and I want to sum them up:



      CREATE TABLE #someFloats (val float);
      INSERT INTO #someFloats (val) VALUES (1), (1), (1.2), (1.2), (1.2), (3), (5);

      SELECT STR(SUM(#someFloats.val), 30, 15) FROM #someFloats;

      DROP TABLE #someFloats;

      -- yields:
      -- 13.600000000000001


      So far, so good - no surprises here. We all know that 1.2 can't be represented exactly in binary representation, so the "imprecise" result is expected.



      Now the following strange thing happens when I left-join another table:



      CREATE TABLE #A (a int);
      INSERT INTO #A (a) VALUES (1), (2);

      CREATE TABLE #someFloats (val float);
      INSERT INTO #someFloats (val) VALUES (1), (1), (1.2), (1.2), (1.2), (3), (5);

      SELECT #A.a, STR(SUM(#someFloats.val), 30, 15)
      FROM #someFloats LEFT JOIN #A ON 1 = 1
      GROUP BY #A.a;

      DROP TABLE #someFloats;
      DROP TABLE #A;

      -- yields
      -- 1 13.600000000000001
      -- 2 13.599999999999998


      (sql fiddle, you can also see the execution plan there)



      I have the same sum over the same values, but a different floating-point error. If I add more rows to table #A, we can see that the value alternates between those two values. I was only able to reproduce this issue with a LEFT JOIN; INNER JOIN works as expected here.



      This is inconvenient, because it means that a DISTINCT, GROUP BY or PIVOT sees them as different values (which is actually how we discovered this issue).



      The obvious solution is to round the value, but I'm curious: Is there a logical explanation for this behavior?







      sql-server floating-point






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      HeinziHeinzi

      1,3721532




      1,3721532




















          1 Answer
          1






          active

          oldest

          votes


















          7














          Actually, the link you're referring to does not say that floating point arithmetic calculations are always deterministic. In fact, in one of the answers it's mentioned that addition is not associative (meaning (a + b) + c does not necessarily equal a + (b + c)), which is also said in this answer.



          If stream aggregation happens to process rows of each group in different order, this could explain the behaviour you observe.






          share|improve this answer























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "182"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233513%2fnon-deterministic-sum-of-floats%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            7














            Actually, the link you're referring to does not say that floating point arithmetic calculations are always deterministic. In fact, in one of the answers it's mentioned that addition is not associative (meaning (a + b) + c does not necessarily equal a + (b + c)), which is also said in this answer.



            If stream aggregation happens to process rows of each group in different order, this could explain the behaviour you observe.






            share|improve this answer



























              7














              Actually, the link you're referring to does not say that floating point arithmetic calculations are always deterministic. In fact, in one of the answers it's mentioned that addition is not associative (meaning (a + b) + c does not necessarily equal a + (b + c)), which is also said in this answer.



              If stream aggregation happens to process rows of each group in different order, this could explain the behaviour you observe.






              share|improve this answer

























                7












                7








                7







                Actually, the link you're referring to does not say that floating point arithmetic calculations are always deterministic. In fact, in one of the answers it's mentioned that addition is not associative (meaning (a + b) + c does not necessarily equal a + (b + c)), which is also said in this answer.



                If stream aggregation happens to process rows of each group in different order, this could explain the behaviour you observe.






                share|improve this answer













                Actually, the link you're referring to does not say that floating point arithmetic calculations are always deterministic. In fact, in one of the answers it's mentioned that addition is not associative (meaning (a + b) + c does not necessarily equal a + (b + c)), which is also said in this answer.



                If stream aggregation happens to process rows of each group in different order, this could explain the behaviour you observe.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 3 hours ago









                mustacciomustaccio

                9,96672238




                9,96672238



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Database Administrators Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233513%2fnon-deterministic-sum-of-floats%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Благоевград Съдържание География | История | Население | Политика | Икономика и инфрастуктура | Здравеопазване | Образование и наука | Култура и забавления | Забележителности | Личности | Литература | Външни препратки | Бележки | Навигация42°01′18.99″ с. ш. 23°05′51″ и. д. / 42.021944° с. ш. 23.0975° и. д.*БлагоевградразширитередактиранеОфициален уебсайт на община БлагоевградНовинарски портал на Благоевград – blagoevgrad.euСайтове за БлагоевградНационален статистически институтdariknews.bgГригоровичъ, Викторъ. „Очеркъ путешествія по Европейской Турціи“. Москва, 1877.Стрезов, Георги. Два санджака от Източна Македония. Периодично списание на Българското книжовно дружество в Средец, кн. XXXVII и XXXVIII, 1891, стр. 18 – 19.Македония. Етнография и статистикаГаджанов, Димитър Г. Мюсюлманското население в Новоосвободените земи, в: Научна експедиция в Македония и Поморавието 1916, Военноиздателски комплекс „Св. Георги Победоносец“, Университетско издателство „Св. Климент Охридски“, София, 1993, стр. 244.паметник на незнайния четник&cd=18&hl=en&ct=clnk&client=firefox-a „История на днешен Благоевград“, взето от www.museumblg.com на 16 март 2010 г.„Справка за населението на град Благоевград, община Благоевград, област Благоевград, НСИ“„The population of all towns and villages in Blagoevgrad Province with 50 inhabitants or more according to census results and latest official estimates“„Ethnic composition, all places: 2011 census“История на Неврокопска епархия.Национален статистически институтМюсюлманско изповедание. Главно мюфтийствоНационален публичен регистър на храмовете в БългарияМюсюлманско изповедание. Главно мюфтийствоwww.dnes.bg Джамията в Благоевград не била паленаwww.sesc-bg.orgСписък на побратимени градовеТехническо побратимяванеГУМ грейва в цветовете на нощен Лас Вегас под името „Largo“, „МОЛ Благоевград“..., в. „Струма“grabo.bgwww.cinemaxbg.comррр4238731-067cad53a-0546-417b-a3d3-51e49b1d2232147736077147736077

                    What is the best defense strategy for Survival in Grand Theft Auto Online?What is JP used for in Grand Theft Auto Online?How do I setup a Crew HQ in Grand Theft Auto Online?How does stealth work in Grand Theft Auto Online?Is it possible to own more than 10 cars in Grand Theft Auto online?Where to find truck/trailers in Grand Theft Auto OnlineWhat are some of the best missions to do on Grand Theft Auto 5 onlineFastest Car in Grand Theft Auto V PCHow to setup a Crew vs Crew online session in Grand Theft Auto Online?Grand theft auto 5 crossplayingRestart Grand Theft Auto V Online?

                    How does Billy Russo acquire his 'Jigsaw' mask? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why does Bane wear the mask?Why does Kylo Ren wear a mask?Why did Captain America remove his mask while fighting Batroc the Leaper?How did the OA acquire her wisdom?Is Billy Breckenridge gay?How does Adrian Toomes hide his earnings from the IRS?What is the state of affairs on Nootka Sound by the end of season 1?How did Tia Dalma acquire Captain Barbossa's body?How is one “Deemed Worthy”, to acquire the Greatsword “Dawn”?How did Karen acquire the handgun?