fraction representation in c++









up vote
0
down vote

favorite












fractions (0.9) and (0.2) make a truncation error when representing in float or double
so if we represent 0.9 or 0.2 two times:




  • the first one in float variable

  • the second one in double variable

  • it's mean that one of them is greater than the other, and the biggest is the double variable



so if we subtract the double from the float, it is expected that the result will be positive



why this code printed ( "Positive" )



float afloat = 0.9; 

if ( 0.9 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9 - afloat < 0 ) cout << "Negative" << endl;


while this code printed ("Negative") ??



float afloat = 0.2; 

if ( 0.2 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.2 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.2 - afloat < 0 ) cout << "Negative" << endl;









share|improve this question



















  • 3




    Is floating point math broken?
    – Jesper Juhl
    Nov 10 at 18:15







  • 3




    "it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
    – Holt
    Nov 10 at 18:23






  • 2




    Your hypothesis is incorrect: the bigger is the double variable. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)
    – Fabio
    Nov 10 at 18:25







  • 1




    fractions (0.9) and (0.2) -- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.
    – PaulMcKenzie
    Nov 10 at 19:12














up vote
0
down vote

favorite












fractions (0.9) and (0.2) make a truncation error when representing in float or double
so if we represent 0.9 or 0.2 two times:




  • the first one in float variable

  • the second one in double variable

  • it's mean that one of them is greater than the other, and the biggest is the double variable



so if we subtract the double from the float, it is expected that the result will be positive



why this code printed ( "Positive" )



float afloat = 0.9; 

if ( 0.9 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9 - afloat < 0 ) cout << "Negative" << endl;


while this code printed ("Negative") ??



float afloat = 0.2; 

if ( 0.2 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.2 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.2 - afloat < 0 ) cout << "Negative" << endl;









share|improve this question



















  • 3




    Is floating point math broken?
    – Jesper Juhl
    Nov 10 at 18:15







  • 3




    "it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
    – Holt
    Nov 10 at 18:23






  • 2




    Your hypothesis is incorrect: the bigger is the double variable. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)
    – Fabio
    Nov 10 at 18:25







  • 1




    fractions (0.9) and (0.2) -- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.
    – PaulMcKenzie
    Nov 10 at 19:12












up vote
0
down vote

favorite









up vote
0
down vote

favorite











fractions (0.9) and (0.2) make a truncation error when representing in float or double
so if we represent 0.9 or 0.2 two times:




  • the first one in float variable

  • the second one in double variable

  • it's mean that one of them is greater than the other, and the biggest is the double variable



so if we subtract the double from the float, it is expected that the result will be positive



why this code printed ( "Positive" )



float afloat = 0.9; 

if ( 0.9 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9 - afloat < 0 ) cout << "Negative" << endl;


while this code printed ("Negative") ??



float afloat = 0.2; 

if ( 0.2 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.2 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.2 - afloat < 0 ) cout << "Negative" << endl;









share|improve this question















fractions (0.9) and (0.2) make a truncation error when representing in float or double
so if we represent 0.9 or 0.2 two times:




  • the first one in float variable

  • the second one in double variable

  • it's mean that one of them is greater than the other, and the biggest is the double variable



so if we subtract the double from the float, it is expected that the result will be positive



why this code printed ( "Positive" )



float afloat = 0.9; 

if ( 0.9 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9 - afloat < 0 ) cout << "Negative" << endl;


while this code printed ("Negative") ??



float afloat = 0.2; 

if ( 0.2 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.2 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.2 - afloat < 0 ) cout << "Negative" << endl;






c++ binary truncation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 18:16

























asked Nov 10 at 18:14









Ehab Fawzy

313




313







  • 3




    Is floating point math broken?
    – Jesper Juhl
    Nov 10 at 18:15







  • 3




    "it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
    – Holt
    Nov 10 at 18:23






  • 2




    Your hypothesis is incorrect: the bigger is the double variable. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)
    – Fabio
    Nov 10 at 18:25







  • 1




    fractions (0.9) and (0.2) -- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.
    – PaulMcKenzie
    Nov 10 at 19:12












  • 3




    Is floating point math broken?
    – Jesper Juhl
    Nov 10 at 18:15







  • 3




    "it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
    – Holt
    Nov 10 at 18:23






  • 2




    Your hypothesis is incorrect: the bigger is the double variable. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)
    – Fabio
    Nov 10 at 18:25







  • 1




    fractions (0.9) and (0.2) -- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.
    – PaulMcKenzie
    Nov 10 at 19:12







3




3




Is floating point math broken?
– Jesper Juhl
Nov 10 at 18:15





Is floating point math broken?
– Jesper Juhl
Nov 10 at 18:15





3




3




"it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
– Holt
Nov 10 at 18:23




"it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
– Holt
Nov 10 at 18:23




2




2




Your hypothesis is incorrect: the bigger is the double variable. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)
– Fabio
Nov 10 at 18:25





Your hypothesis is incorrect: the bigger is the double variable. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)
– Fabio
Nov 10 at 18:25





1




1




fractions (0.9) and (0.2) -- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.
– PaulMcKenzie
Nov 10 at 19:12




fractions (0.9) and (0.2) -- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.
– PaulMcKenzie
Nov 10 at 19:12












1 Answer
1






active

oldest

votes

















up vote
1
down vote













The literal 0.9 is a double precision value approximation to 9/10, and your declaration



float afloat = 0.9


truncates this value to single precision, thereby losing information. This code



float afloat = 0.9f; 

if ( 0.9f - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9f - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9f - afloat < 0 ) cout << "Negative" << endl;


will generate the output



Equal


even if 0.9f is not exactly equal to the fraction 9/10. The sign of the quantization error will differ for different fractions, and for different floating point formats.






share|improve this answer






















    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',
    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%2f53241982%2ffraction-representation-in-c%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








    up vote
    1
    down vote













    The literal 0.9 is a double precision value approximation to 9/10, and your declaration



    float afloat = 0.9


    truncates this value to single precision, thereby losing information. This code



    float afloat = 0.9f; 

    if ( 0.9f - afloat > 0 ) cout << "Positive" << endl;
    else if ( 0.9f - afloat == 0 ) cout << "Equal" << endl;
    else if ( 0.9f - afloat < 0 ) cout << "Negative" << endl;


    will generate the output



    Equal


    even if 0.9f is not exactly equal to the fraction 9/10. The sign of the quantization error will differ for different fractions, and for different floating point formats.






    share|improve this answer


























      up vote
      1
      down vote













      The literal 0.9 is a double precision value approximation to 9/10, and your declaration



      float afloat = 0.9


      truncates this value to single precision, thereby losing information. This code



      float afloat = 0.9f; 

      if ( 0.9f - afloat > 0 ) cout << "Positive" << endl;
      else if ( 0.9f - afloat == 0 ) cout << "Equal" << endl;
      else if ( 0.9f - afloat < 0 ) cout << "Negative" << endl;


      will generate the output



      Equal


      even if 0.9f is not exactly equal to the fraction 9/10. The sign of the quantization error will differ for different fractions, and for different floating point formats.






      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        The literal 0.9 is a double precision value approximation to 9/10, and your declaration



        float afloat = 0.9


        truncates this value to single precision, thereby losing information. This code



        float afloat = 0.9f; 

        if ( 0.9f - afloat > 0 ) cout << "Positive" << endl;
        else if ( 0.9f - afloat == 0 ) cout << "Equal" << endl;
        else if ( 0.9f - afloat < 0 ) cout << "Negative" << endl;


        will generate the output



        Equal


        even if 0.9f is not exactly equal to the fraction 9/10. The sign of the quantization error will differ for different fractions, and for different floating point formats.






        share|improve this answer














        The literal 0.9 is a double precision value approximation to 9/10, and your declaration



        float afloat = 0.9


        truncates this value to single precision, thereby losing information. This code



        float afloat = 0.9f; 

        if ( 0.9f - afloat > 0 ) cout << "Positive" << endl;
        else if ( 0.9f - afloat == 0 ) cout << "Equal" << endl;
        else if ( 0.9f - afloat < 0 ) cout << "Negative" << endl;


        will generate the output



        Equal


        even if 0.9f is not exactly equal to the fraction 9/10. The sign of the quantization error will differ for different fractions, and for different floating point formats.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 22:11

























        answered Nov 10 at 22:06









        sveinbr

        965




        965



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241982%2ffraction-representation-in-c%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党