Why does setting a const variable (which will be stored with the same value) lead to a different result once...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







6















Pretty basic code:



#include <iostream>

int main() {
std::cout.precision(100);

double a = 9.79999999999063220457173883914947509765625;
double b = 0.057762265046662104872599030613855575211346149444580078125;
const double bConst = 0.057762265046662104872599030613855575211346149444580078125;
double c = a * b;

std::cout << " a: " << a << std::endl;
std::cout << " b: " << b << std::endl;
std::cout << " bConst: " << bConst << std::endl;
std::cout << " c: " << c << std::endl << std::endl;
std::cout << " c/b: " << c / b << std::endl;
std::cout << " c/bConst: " << c / bConst << std::endl;
}


Which outputs:



        a: 9.79999999999063220457173883914947509765625
b: 0.057762265046662104872599030613855575211346149444580078125
bConst: 0.057762265046662104872599030613855575211346149444580078125
c: 0.5660701974567474703547986791818402707576751708984375

c/b: 9.7999999999906304282148994388990104198455810546875
c/bConst: 9.79999999999063220457173883914947509765625


As you can see, b and bConst seem to be treated using the same value - i.e. it prints for both the same 0.057762265046662104872599030613855575211346149444580078125 value.
So I guess they are "stored" both the same. The only difference is that b is not const.



Then, I do the same c / b operation twice: one time using b, another time using bConst.



As you can see, it leads to two different results. And this makes me wonder.



Can you explain technically why this happens?










share|improve this question

























  • Here (mingw-w64 with g++ 8.1) both results are the same when optimized with -O2 and I get your result with -Ofast.

    – Benjamin Bihler
    Feb 15 at 10:12








  • 1





    I thought you got a grasp of the perverse effects of -Ofaston floatting operation determinism in your other question stackoverflow.com/q/54694274/5470596

    – YSC
    Feb 15 at 14:33




















6















Pretty basic code:



#include <iostream>

int main() {
std::cout.precision(100);

double a = 9.79999999999063220457173883914947509765625;
double b = 0.057762265046662104872599030613855575211346149444580078125;
const double bConst = 0.057762265046662104872599030613855575211346149444580078125;
double c = a * b;

std::cout << " a: " << a << std::endl;
std::cout << " b: " << b << std::endl;
std::cout << " bConst: " << bConst << std::endl;
std::cout << " c: " << c << std::endl << std::endl;
std::cout << " c/b: " << c / b << std::endl;
std::cout << " c/bConst: " << c / bConst << std::endl;
}


Which outputs:



        a: 9.79999999999063220457173883914947509765625
b: 0.057762265046662104872599030613855575211346149444580078125
bConst: 0.057762265046662104872599030613855575211346149444580078125
c: 0.5660701974567474703547986791818402707576751708984375

c/b: 9.7999999999906304282148994388990104198455810546875
c/bConst: 9.79999999999063220457173883914947509765625


As you can see, b and bConst seem to be treated using the same value - i.e. it prints for both the same 0.057762265046662104872599030613855575211346149444580078125 value.
So I guess they are "stored" both the same. The only difference is that b is not const.



Then, I do the same c / b operation twice: one time using b, another time using bConst.



As you can see, it leads to two different results. And this makes me wonder.



Can you explain technically why this happens?










share|improve this question

























  • Here (mingw-w64 with g++ 8.1) both results are the same when optimized with -O2 and I get your result with -Ofast.

    – Benjamin Bihler
    Feb 15 at 10:12








  • 1





    I thought you got a grasp of the perverse effects of -Ofaston floatting operation determinism in your other question stackoverflow.com/q/54694274/5470596

    – YSC
    Feb 15 at 14:33
















6












6








6


1






Pretty basic code:



#include <iostream>

int main() {
std::cout.precision(100);

double a = 9.79999999999063220457173883914947509765625;
double b = 0.057762265046662104872599030613855575211346149444580078125;
const double bConst = 0.057762265046662104872599030613855575211346149444580078125;
double c = a * b;

std::cout << " a: " << a << std::endl;
std::cout << " b: " << b << std::endl;
std::cout << " bConst: " << bConst << std::endl;
std::cout << " c: " << c << std::endl << std::endl;
std::cout << " c/b: " << c / b << std::endl;
std::cout << " c/bConst: " << c / bConst << std::endl;
}


Which outputs:



        a: 9.79999999999063220457173883914947509765625
b: 0.057762265046662104872599030613855575211346149444580078125
bConst: 0.057762265046662104872599030613855575211346149444580078125
c: 0.5660701974567474703547986791818402707576751708984375

c/b: 9.7999999999906304282148994388990104198455810546875
c/bConst: 9.79999999999063220457173883914947509765625


As you can see, b and bConst seem to be treated using the same value - i.e. it prints for both the same 0.057762265046662104872599030613855575211346149444580078125 value.
So I guess they are "stored" both the same. The only difference is that b is not const.



Then, I do the same c / b operation twice: one time using b, another time using bConst.



As you can see, it leads to two different results. And this makes me wonder.



Can you explain technically why this happens?










share|improve this question
















Pretty basic code:



#include <iostream>

int main() {
std::cout.precision(100);

double a = 9.79999999999063220457173883914947509765625;
double b = 0.057762265046662104872599030613855575211346149444580078125;
const double bConst = 0.057762265046662104872599030613855575211346149444580078125;
double c = a * b;

std::cout << " a: " << a << std::endl;
std::cout << " b: " << b << std::endl;
std::cout << " bConst: " << bConst << std::endl;
std::cout << " c: " << c << std::endl << std::endl;
std::cout << " c/b: " << c / b << std::endl;
std::cout << " c/bConst: " << c / bConst << std::endl;
}


Which outputs:



        a: 9.79999999999063220457173883914947509765625
b: 0.057762265046662104872599030613855575211346149444580078125
bConst: 0.057762265046662104872599030613855575211346149444580078125
c: 0.5660701974567474703547986791818402707576751708984375

c/b: 9.7999999999906304282148994388990104198455810546875
c/bConst: 9.79999999999063220457173883914947509765625


As you can see, b and bConst seem to be treated using the same value - i.e. it prints for both the same 0.057762265046662104872599030613855575211346149444580078125 value.
So I guess they are "stored" both the same. The only difference is that b is not const.



Then, I do the same c / b operation twice: one time using b, another time using bConst.



As you can see, it leads to two different results. And this makes me wonder.



Can you explain technically why this happens?







c++ optimization floating-point const






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 15 at 15:47









sepp2k

301k39602617




301k39602617










asked Feb 15 at 10:09









markzzzmarkzzz

19.3k94234408




19.3k94234408













  • Here (mingw-w64 with g++ 8.1) both results are the same when optimized with -O2 and I get your result with -Ofast.

    – Benjamin Bihler
    Feb 15 at 10:12








  • 1





    I thought you got a grasp of the perverse effects of -Ofaston floatting operation determinism in your other question stackoverflow.com/q/54694274/5470596

    – YSC
    Feb 15 at 14:33





















  • Here (mingw-w64 with g++ 8.1) both results are the same when optimized with -O2 and I get your result with -Ofast.

    – Benjamin Bihler
    Feb 15 at 10:12








  • 1





    I thought you got a grasp of the perverse effects of -Ofaston floatting operation determinism in your other question stackoverflow.com/q/54694274/5470596

    – YSC
    Feb 15 at 14:33



















Here (mingw-w64 with g++ 8.1) both results are the same when optimized with -O2 and I get your result with -Ofast.

– Benjamin Bihler
Feb 15 at 10:12







Here (mingw-w64 with g++ 8.1) both results are the same when optimized with -O2 and I get your result with -Ofast.

– Benjamin Bihler
Feb 15 at 10:12






1




1





I thought you got a grasp of the perverse effects of -Ofaston floatting operation determinism in your other question stackoverflow.com/q/54694274/5470596

– YSC
Feb 15 at 14:33







I thought you got a grasp of the perverse effects of -Ofaston floatting operation determinism in your other question stackoverflow.com/q/54694274/5470596

– YSC
Feb 15 at 14:33














2 Answers
2






active

oldest

votes


















7














The "issue" is due to the -freciprocal-math switch (implied by -Ofast):




Allow the reciprocal of a value to be used instead of dividing by the value if this enables optimizations. For example x / y can be replaced with x * (1/y), which is useful if (1/y) is subject to common subexpression elimination. Note that this loses precision and increases the number of flops operating on the value.




The compiler can calculate d = 1/bConst at compile time and change from:



c/bConst


to



c * d


but multiplication and division are different instructions with different performance and precision.



See: http://coliru.stacked-crooked.com/a/ba9770ec39ec5ac2






share|improve this answer


























  • Thank you for that explanation... interesting!

    – Benjamin Bihler
    Feb 15 at 12:27











  • Uhm... not sure that's the behaviour: coliru.stacked-crooked.com/a/79e5a3501af9a217 . Using c * 1 / b = c / b) (giving b = bConst). Instead, c * 1 / bConst give the same as c / bConst... so it should be somethings different.

    – markzzz
    Feb 15 at 13:05













  • Uhm... wait! If I did c * (1 / b) (instead of c * 1 / b) works as you said.

    – markzzz
    Feb 15 at 13:08





















3














You are using -Ofast in your link, which enables all -O3 optimizations and includes both -ffast-math, which in turns includes -funsafe-math-optimizations.



From what I could glean, with optimizations enabled, -funsafe-math-optimizations allows the compiler to reduce the precision of some computations. This seems to be what happens in the c/bConst case.






share|improve this answer


























  • The anwer is correct. Any idea why this is the case?

    – Benjamin Bihler
    Feb 15 at 10:15











  • What do you mean with "leads to a different result depending on the const-ness"? Shouldn't I get the same results starting with the same quotient and dividend?

    – markzzz
    Feb 15 at 10:16











  • @BenjaminBihler I'm looking into it.

    – Nelfeal
    Feb 15 at 10:16






  • 2





    @markzzz Unsafe optimizations tend to not care about this kind of logic.

    – Nelfeal
    Feb 15 at 10:17












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f54706948%2fwhy-does-setting-a-const-variable-which-will-be-stored-with-the-same-value-lea%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









7














The "issue" is due to the -freciprocal-math switch (implied by -Ofast):




Allow the reciprocal of a value to be used instead of dividing by the value if this enables optimizations. For example x / y can be replaced with x * (1/y), which is useful if (1/y) is subject to common subexpression elimination. Note that this loses precision and increases the number of flops operating on the value.




The compiler can calculate d = 1/bConst at compile time and change from:



c/bConst


to



c * d


but multiplication and division are different instructions with different performance and precision.



See: http://coliru.stacked-crooked.com/a/ba9770ec39ec5ac2






share|improve this answer


























  • Thank you for that explanation... interesting!

    – Benjamin Bihler
    Feb 15 at 12:27











  • Uhm... not sure that's the behaviour: coliru.stacked-crooked.com/a/79e5a3501af9a217 . Using c * 1 / b = c / b) (giving b = bConst). Instead, c * 1 / bConst give the same as c / bConst... so it should be somethings different.

    – markzzz
    Feb 15 at 13:05













  • Uhm... wait! If I did c * (1 / b) (instead of c * 1 / b) works as you said.

    – markzzz
    Feb 15 at 13:08


















7














The "issue" is due to the -freciprocal-math switch (implied by -Ofast):




Allow the reciprocal of a value to be used instead of dividing by the value if this enables optimizations. For example x / y can be replaced with x * (1/y), which is useful if (1/y) is subject to common subexpression elimination. Note that this loses precision and increases the number of flops operating on the value.




The compiler can calculate d = 1/bConst at compile time and change from:



c/bConst


to



c * d


but multiplication and division are different instructions with different performance and precision.



See: http://coliru.stacked-crooked.com/a/ba9770ec39ec5ac2






share|improve this answer


























  • Thank you for that explanation... interesting!

    – Benjamin Bihler
    Feb 15 at 12:27











  • Uhm... not sure that's the behaviour: coliru.stacked-crooked.com/a/79e5a3501af9a217 . Using c * 1 / b = c / b) (giving b = bConst). Instead, c * 1 / bConst give the same as c / bConst... so it should be somethings different.

    – markzzz
    Feb 15 at 13:05













  • Uhm... wait! If I did c * (1 / b) (instead of c * 1 / b) works as you said.

    – markzzz
    Feb 15 at 13:08
















7












7








7







The "issue" is due to the -freciprocal-math switch (implied by -Ofast):




Allow the reciprocal of a value to be used instead of dividing by the value if this enables optimizations. For example x / y can be replaced with x * (1/y), which is useful if (1/y) is subject to common subexpression elimination. Note that this loses precision and increases the number of flops operating on the value.




The compiler can calculate d = 1/bConst at compile time and change from:



c/bConst


to



c * d


but multiplication and division are different instructions with different performance and precision.



See: http://coliru.stacked-crooked.com/a/ba9770ec39ec5ac2






share|improve this answer















The "issue" is due to the -freciprocal-math switch (implied by -Ofast):




Allow the reciprocal of a value to be used instead of dividing by the value if this enables optimizations. For example x / y can be replaced with x * (1/y), which is useful if (1/y) is subject to common subexpression elimination. Note that this loses precision and increases the number of flops operating on the value.




The compiler can calculate d = 1/bConst at compile time and change from:



c/bConst


to



c * d


but multiplication and division are different instructions with different performance and precision.



See: http://coliru.stacked-crooked.com/a/ba9770ec39ec5ac2







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 15 at 13:32

























answered Feb 15 at 11:34









manliomanlio

14.3k105182




14.3k105182













  • Thank you for that explanation... interesting!

    – Benjamin Bihler
    Feb 15 at 12:27











  • Uhm... not sure that's the behaviour: coliru.stacked-crooked.com/a/79e5a3501af9a217 . Using c * 1 / b = c / b) (giving b = bConst). Instead, c * 1 / bConst give the same as c / bConst... so it should be somethings different.

    – markzzz
    Feb 15 at 13:05













  • Uhm... wait! If I did c * (1 / b) (instead of c * 1 / b) works as you said.

    – markzzz
    Feb 15 at 13:08





















  • Thank you for that explanation... interesting!

    – Benjamin Bihler
    Feb 15 at 12:27











  • Uhm... not sure that's the behaviour: coliru.stacked-crooked.com/a/79e5a3501af9a217 . Using c * 1 / b = c / b) (giving b = bConst). Instead, c * 1 / bConst give the same as c / bConst... so it should be somethings different.

    – markzzz
    Feb 15 at 13:05













  • Uhm... wait! If I did c * (1 / b) (instead of c * 1 / b) works as you said.

    – markzzz
    Feb 15 at 13:08



















Thank you for that explanation... interesting!

– Benjamin Bihler
Feb 15 at 12:27





Thank you for that explanation... interesting!

– Benjamin Bihler
Feb 15 at 12:27













Uhm... not sure that's the behaviour: coliru.stacked-crooked.com/a/79e5a3501af9a217 . Using c * 1 / b = c / b) (giving b = bConst). Instead, c * 1 / bConst give the same as c / bConst... so it should be somethings different.

– markzzz
Feb 15 at 13:05







Uhm... not sure that's the behaviour: coliru.stacked-crooked.com/a/79e5a3501af9a217 . Using c * 1 / b = c / b) (giving b = bConst). Instead, c * 1 / bConst give the same as c / bConst... so it should be somethings different.

– markzzz
Feb 15 at 13:05















Uhm... wait! If I did c * (1 / b) (instead of c * 1 / b) works as you said.

– markzzz
Feb 15 at 13:08







Uhm... wait! If I did c * (1 / b) (instead of c * 1 / b) works as you said.

– markzzz
Feb 15 at 13:08















3














You are using -Ofast in your link, which enables all -O3 optimizations and includes both -ffast-math, which in turns includes -funsafe-math-optimizations.



From what I could glean, with optimizations enabled, -funsafe-math-optimizations allows the compiler to reduce the precision of some computations. This seems to be what happens in the c/bConst case.






share|improve this answer


























  • The anwer is correct. Any idea why this is the case?

    – Benjamin Bihler
    Feb 15 at 10:15











  • What do you mean with "leads to a different result depending on the const-ness"? Shouldn't I get the same results starting with the same quotient and dividend?

    – markzzz
    Feb 15 at 10:16











  • @BenjaminBihler I'm looking into it.

    – Nelfeal
    Feb 15 at 10:16






  • 2





    @markzzz Unsafe optimizations tend to not care about this kind of logic.

    – Nelfeal
    Feb 15 at 10:17
















3














You are using -Ofast in your link, which enables all -O3 optimizations and includes both -ffast-math, which in turns includes -funsafe-math-optimizations.



From what I could glean, with optimizations enabled, -funsafe-math-optimizations allows the compiler to reduce the precision of some computations. This seems to be what happens in the c/bConst case.






share|improve this answer


























  • The anwer is correct. Any idea why this is the case?

    – Benjamin Bihler
    Feb 15 at 10:15











  • What do you mean with "leads to a different result depending on the const-ness"? Shouldn't I get the same results starting with the same quotient and dividend?

    – markzzz
    Feb 15 at 10:16











  • @BenjaminBihler I'm looking into it.

    – Nelfeal
    Feb 15 at 10:16






  • 2





    @markzzz Unsafe optimizations tend to not care about this kind of logic.

    – Nelfeal
    Feb 15 at 10:17














3












3








3







You are using -Ofast in your link, which enables all -O3 optimizations and includes both -ffast-math, which in turns includes -funsafe-math-optimizations.



From what I could glean, with optimizations enabled, -funsafe-math-optimizations allows the compiler to reduce the precision of some computations. This seems to be what happens in the c/bConst case.






share|improve this answer















You are using -Ofast in your link, which enables all -O3 optimizations and includes both -ffast-math, which in turns includes -funsafe-math-optimizations.



From what I could glean, with optimizations enabled, -funsafe-math-optimizations allows the compiler to reduce the precision of some computations. This seems to be what happens in the c/bConst case.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 15 at 10:34

























answered Feb 15 at 10:14









NelfealNelfeal

5,2321825




5,2321825













  • The anwer is correct. Any idea why this is the case?

    – Benjamin Bihler
    Feb 15 at 10:15











  • What do you mean with "leads to a different result depending on the const-ness"? Shouldn't I get the same results starting with the same quotient and dividend?

    – markzzz
    Feb 15 at 10:16











  • @BenjaminBihler I'm looking into it.

    – Nelfeal
    Feb 15 at 10:16






  • 2





    @markzzz Unsafe optimizations tend to not care about this kind of logic.

    – Nelfeal
    Feb 15 at 10:17



















  • The anwer is correct. Any idea why this is the case?

    – Benjamin Bihler
    Feb 15 at 10:15











  • What do you mean with "leads to a different result depending on the const-ness"? Shouldn't I get the same results starting with the same quotient and dividend?

    – markzzz
    Feb 15 at 10:16











  • @BenjaminBihler I'm looking into it.

    – Nelfeal
    Feb 15 at 10:16






  • 2





    @markzzz Unsafe optimizations tend to not care about this kind of logic.

    – Nelfeal
    Feb 15 at 10:17

















The anwer is correct. Any idea why this is the case?

– Benjamin Bihler
Feb 15 at 10:15





The anwer is correct. Any idea why this is the case?

– Benjamin Bihler
Feb 15 at 10:15













What do you mean with "leads to a different result depending on the const-ness"? Shouldn't I get the same results starting with the same quotient and dividend?

– markzzz
Feb 15 at 10:16





What do you mean with "leads to a different result depending on the const-ness"? Shouldn't I get the same results starting with the same quotient and dividend?

– markzzz
Feb 15 at 10:16













@BenjaminBihler I'm looking into it.

– Nelfeal
Feb 15 at 10:16





@BenjaminBihler I'm looking into it.

– Nelfeal
Feb 15 at 10:16




2




2





@markzzz Unsafe optimizations tend to not care about this kind of logic.

– Nelfeal
Feb 15 at 10:17





@markzzz Unsafe optimizations tend to not care about this kind of logic.

– Nelfeal
Feb 15 at 10:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f54706948%2fwhy-does-setting-a-const-variable-which-will-be-stored-with-the-same-value-lea%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

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

File:DeusFollowingSea.jpg