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
Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?
Is there a difference between "Fahrstuhl" and "Aufzug"?
How to get the last not-null value in an ordered column of a huge table?
Help/tips for a first time writer?
How to find image of a complex function with given constraints?
Why don't programming languages automatically manage the synchronous/asynchronous problem?
what's the use of '% to gdp' type of variables?
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
Why the last AS PATH item always is `I` or `?`?
What was Carter Burkes job for "the company" in "Aliens"?
What is the difference between Statistical Mechanics and Quantum Mechanics
Traduction de « Life is a roller coaster »
Regression vs Random Forest - Combination of features
How many extra stops do monopods offer for tele photographs?
Is "three point ish" an acceptable use of ish?
In the "Harry Potter and the Order of the Phoenix" videogame, what potion is used to sabotage Umbridge's speakers?
What day is it again?
Why doesn't UK go for the same deal Japan has with EU to resolve Brexit?
What flight has the highest ratio of timezone difference to flight time?
Defamation due to breach of confidentiality
What does "shotgun unity" refer to here in this sentence?
Easy to read palindrome checker
Can I calculate next year's exemptions based on this year's refund/amount owed?
Calculate the Mean mean of two numbers
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
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
add a comment |
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
add a comment |
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
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
sql-server floating-point
asked 2 days ago
HeinziHeinzi
1,3721532
1,3721532
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
Associativity has no relation to determinism, so that bit is misleading.
– Mooing Duck
yesterday
Non-associativity of floating point addition leads to non-deterministic behaviour of the SQL Server aggregate functionSUM(), would you agree @MooingDuck?
– mustaccio
yesterday
No? Integer Division is a clear counterexample. It is non-associative, but entirely deterministic. Likewise, floating point division should be non-associative and still deterministic. From that, we conclude it's reasonable for addition to be non-associative and still deterministic. That being said, if the order of additions isn't deterministic, then the result will likewise not be deterministic, so your first and last sentence are still correct regardless.
– Mooing Duck
yesterday
Integer division is a counterexample for the SQL ServerSUM()over floating point arguments, how exactly?
– mustaccio
yesterday
Integer division is non-associative and deterministic. Therefore, arithmetic operations associativity is not related to determinism. Therefore any non-associativity ofSUM()must be irrelevant toward it's determinism. I agree thatSUMappears to be non deterministic, but you should remove mentions of associativity, since that's unrelated.
– Mooing Duck
yesterday
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Associativity has no relation to determinism, so that bit is misleading.
– Mooing Duck
yesterday
Non-associativity of floating point addition leads to non-deterministic behaviour of the SQL Server aggregate functionSUM(), would you agree @MooingDuck?
– mustaccio
yesterday
No? Integer Division is a clear counterexample. It is non-associative, but entirely deterministic. Likewise, floating point division should be non-associative and still deterministic. From that, we conclude it's reasonable for addition to be non-associative and still deterministic. That being said, if the order of additions isn't deterministic, then the result will likewise not be deterministic, so your first and last sentence are still correct regardless.
– Mooing Duck
yesterday
Integer division is a counterexample for the SQL ServerSUM()over floating point arguments, how exactly?
– mustaccio
yesterday
Integer division is non-associative and deterministic. Therefore, arithmetic operations associativity is not related to determinism. Therefore any non-associativity ofSUM()must be irrelevant toward it's determinism. I agree thatSUMappears to be non deterministic, but you should remove mentions of associativity, since that's unrelated.
– Mooing Duck
yesterday
add a comment |
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.
Associativity has no relation to determinism, so that bit is misleading.
– Mooing Duck
yesterday
Non-associativity of floating point addition leads to non-deterministic behaviour of the SQL Server aggregate functionSUM(), would you agree @MooingDuck?
– mustaccio
yesterday
No? Integer Division is a clear counterexample. It is non-associative, but entirely deterministic. Likewise, floating point division should be non-associative and still deterministic. From that, we conclude it's reasonable for addition to be non-associative and still deterministic. That being said, if the order of additions isn't deterministic, then the result will likewise not be deterministic, so your first and last sentence are still correct regardless.
– Mooing Duck
yesterday
Integer division is a counterexample for the SQL ServerSUM()over floating point arguments, how exactly?
– mustaccio
yesterday
Integer division is non-associative and deterministic. Therefore, arithmetic operations associativity is not related to determinism. Therefore any non-associativity ofSUM()must be irrelevant toward it's determinism. I agree thatSUMappears to be non deterministic, but you should remove mentions of associativity, since that's unrelated.
– Mooing Duck
yesterday
add a comment |
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.
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.
answered 2 days ago
mustacciomustaccio
10k72239
10k72239
Associativity has no relation to determinism, so that bit is misleading.
– Mooing Duck
yesterday
Non-associativity of floating point addition leads to non-deterministic behaviour of the SQL Server aggregate functionSUM(), would you agree @MooingDuck?
– mustaccio
yesterday
No? Integer Division is a clear counterexample. It is non-associative, but entirely deterministic. Likewise, floating point division should be non-associative and still deterministic. From that, we conclude it's reasonable for addition to be non-associative and still deterministic. That being said, if the order of additions isn't deterministic, then the result will likewise not be deterministic, so your first and last sentence are still correct regardless.
– Mooing Duck
yesterday
Integer division is a counterexample for the SQL ServerSUM()over floating point arguments, how exactly?
– mustaccio
yesterday
Integer division is non-associative and deterministic. Therefore, arithmetic operations associativity is not related to determinism. Therefore any non-associativity ofSUM()must be irrelevant toward it's determinism. I agree thatSUMappears to be non deterministic, but you should remove mentions of associativity, since that's unrelated.
– Mooing Duck
yesterday
add a comment |
Associativity has no relation to determinism, so that bit is misleading.
– Mooing Duck
yesterday
Non-associativity of floating point addition leads to non-deterministic behaviour of the SQL Server aggregate functionSUM(), would you agree @MooingDuck?
– mustaccio
yesterday
No? Integer Division is a clear counterexample. It is non-associative, but entirely deterministic. Likewise, floating point division should be non-associative and still deterministic. From that, we conclude it's reasonable for addition to be non-associative and still deterministic. That being said, if the order of additions isn't deterministic, then the result will likewise not be deterministic, so your first and last sentence are still correct regardless.
– Mooing Duck
yesterday
Integer division is a counterexample for the SQL ServerSUM()over floating point arguments, how exactly?
– mustaccio
yesterday
Integer division is non-associative and deterministic. Therefore, arithmetic operations associativity is not related to determinism. Therefore any non-associativity ofSUM()must be irrelevant toward it's determinism. I agree thatSUMappears to be non deterministic, but you should remove mentions of associativity, since that's unrelated.
– Mooing Duck
yesterday
Associativity has no relation to determinism, so that bit is misleading.
– Mooing Duck
yesterday
Associativity has no relation to determinism, so that bit is misleading.
– Mooing Duck
yesterday
Non-associativity of floating point addition leads to non-deterministic behaviour of the SQL Server aggregate function
SUM(), would you agree @MooingDuck?– mustaccio
yesterday
Non-associativity of floating point addition leads to non-deterministic behaviour of the SQL Server aggregate function
SUM(), would you agree @MooingDuck?– mustaccio
yesterday
No? Integer Division is a clear counterexample. It is non-associative, but entirely deterministic. Likewise, floating point division should be non-associative and still deterministic. From that, we conclude it's reasonable for addition to be non-associative and still deterministic. That being said, if the order of additions isn't deterministic, then the result will likewise not be deterministic, so your first and last sentence are still correct regardless.
– Mooing Duck
yesterday
No? Integer Division is a clear counterexample. It is non-associative, but entirely deterministic. Likewise, floating point division should be non-associative and still deterministic. From that, we conclude it's reasonable for addition to be non-associative and still deterministic. That being said, if the order of additions isn't deterministic, then the result will likewise not be deterministic, so your first and last sentence are still correct regardless.
– Mooing Duck
yesterday
Integer division is a counterexample for the SQL Server
SUM() over floating point arguments, how exactly?– mustaccio
yesterday
Integer division is a counterexample for the SQL Server
SUM() over floating point arguments, how exactly?– mustaccio
yesterday
Integer division is non-associative and deterministic. Therefore, arithmetic operations associativity is not related to determinism. Therefore any non-associativity of
SUM() must be irrelevant toward it's determinism. I agree that SUM appears to be non deterministic, but you should remove mentions of associativity, since that's unrelated.– Mooing Duck
yesterday
Integer division is non-associative and deterministic. Therefore, arithmetic operations associativity is not related to determinism. Therefore any non-associativity of
SUM() must be irrelevant toward it's determinism. I agree that SUM appears to be non deterministic, but you should remove mentions of associativity, since that's unrelated.– Mooing Duck
yesterday
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
