Whenever I enter 4.2 in this code, the value of nm is 19, where 20 is expected











up vote
1
down vote

favorite












I am trying to get 20 in the variable nm. But it is returning 19.



How do I fix it? And what is causing this problem?



#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{

float m;

int n = 0;
do {
m = get_float("Enter:");

}while(m < 0);
int nm =(m * 100 );
while(nm >= 25 ){
nm = nm - 25;
n = n + 1;

}

printf("%in",n);
printf("%in", nm);
}









share|improve this question




















  • 1




    And that, kids, is why we don't ever rely on transitive equality when pushing a float through an int.
    – WhozCraig
    Nov 19 at 19:19








  • 1




    It's very tempting just to close this as a duplicate of Is floating point math broken?
    – Jonathan Leffler
    Nov 19 at 19:57










  • Possible duplicate of Is floating point math broken?
    – Sean Pianka
    Nov 19 at 20:50















up vote
1
down vote

favorite












I am trying to get 20 in the variable nm. But it is returning 19.



How do I fix it? And what is causing this problem?



#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{

float m;

int n = 0;
do {
m = get_float("Enter:");

}while(m < 0);
int nm =(m * 100 );
while(nm >= 25 ){
nm = nm - 25;
n = n + 1;

}

printf("%in",n);
printf("%in", nm);
}









share|improve this question




















  • 1




    And that, kids, is why we don't ever rely on transitive equality when pushing a float through an int.
    – WhozCraig
    Nov 19 at 19:19








  • 1




    It's very tempting just to close this as a duplicate of Is floating point math broken?
    – Jonathan Leffler
    Nov 19 at 19:57










  • Possible duplicate of Is floating point math broken?
    – Sean Pianka
    Nov 19 at 20:50













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to get 20 in the variable nm. But it is returning 19.



How do I fix it? And what is causing this problem?



#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{

float m;

int n = 0;
do {
m = get_float("Enter:");

}while(m < 0);
int nm =(m * 100 );
while(nm >= 25 ){
nm = nm - 25;
n = n + 1;

}

printf("%in",n);
printf("%in", nm);
}









share|improve this question















I am trying to get 20 in the variable nm. But it is returning 19.



How do I fix it? And what is causing this problem?



#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{

float m;

int n = 0;
do {
m = get_float("Enter:");

}while(m < 0);
int nm =(m * 100 );
while(nm >= 25 ){
nm = nm - 25;
n = n + 1;

}

printf("%in",n);
printf("%in", nm);
}






c cs50






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 at 7:05









Jonathan Leffler

556k886621015




556k886621015










asked Nov 19 at 19:16









I M Hirobumi

84




84








  • 1




    And that, kids, is why we don't ever rely on transitive equality when pushing a float through an int.
    – WhozCraig
    Nov 19 at 19:19








  • 1




    It's very tempting just to close this as a duplicate of Is floating point math broken?
    – Jonathan Leffler
    Nov 19 at 19:57










  • Possible duplicate of Is floating point math broken?
    – Sean Pianka
    Nov 19 at 20:50














  • 1




    And that, kids, is why we don't ever rely on transitive equality when pushing a float through an int.
    – WhozCraig
    Nov 19 at 19:19








  • 1




    It's very tempting just to close this as a duplicate of Is floating point math broken?
    – Jonathan Leffler
    Nov 19 at 19:57










  • Possible duplicate of Is floating point math broken?
    – Sean Pianka
    Nov 19 at 20:50








1




1




And that, kids, is why we don't ever rely on transitive equality when pushing a float through an int.
– WhozCraig
Nov 19 at 19:19






And that, kids, is why we don't ever rely on transitive equality when pushing a float through an int.
– WhozCraig
Nov 19 at 19:19






1




1




It's very tempting just to close this as a duplicate of Is floating point math broken?
– Jonathan Leffler
Nov 19 at 19:57




It's very tempting just to close this as a duplicate of Is floating point math broken?
– Jonathan Leffler
Nov 19 at 19:57












Possible duplicate of Is floating point math broken?
– Sean Pianka
Nov 19 at 20:50




Possible duplicate of Is floating point math broken?
– Sean Pianka
Nov 19 at 20:50












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










A 32-bit float can encode about 232 different values exactly. Due to the binary nature of the typical float, 4.2 is not one of them.



Instead m has the value of 4.1999998...



// closest float
4.19999980926513671875
// hoped for value
4.2
// next closest float
4.200000286102294921875


Multiplexing by 100 incurs some rounding and the best answer to m*100 is then 419.999969482421875.



int nm =(m * 100 ); results nm == 419 as assignment of a float to int truncates the fractional portion away.





Consider rounding to the nearest integer rather than truncating via int assignment and use double constants.



#include <math.h>

// int nm =(m * 100 );
int nm = lround(m * 100.0);
// or
int nm = round(m * 100.0);





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%2f53381216%2fwhenever-i-enter-4-2-in-this-code-the-value-of-nm-is-19-where-20-is-expected%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



    accepted










    A 32-bit float can encode about 232 different values exactly. Due to the binary nature of the typical float, 4.2 is not one of them.



    Instead m has the value of 4.1999998...



    // closest float
    4.19999980926513671875
    // hoped for value
    4.2
    // next closest float
    4.200000286102294921875


    Multiplexing by 100 incurs some rounding and the best answer to m*100 is then 419.999969482421875.



    int nm =(m * 100 ); results nm == 419 as assignment of a float to int truncates the fractional portion away.





    Consider rounding to the nearest integer rather than truncating via int assignment and use double constants.



    #include <math.h>

    // int nm =(m * 100 );
    int nm = lround(m * 100.0);
    // or
    int nm = round(m * 100.0);





    share|improve this answer



























      up vote
      1
      down vote



      accepted










      A 32-bit float can encode about 232 different values exactly. Due to the binary nature of the typical float, 4.2 is not one of them.



      Instead m has the value of 4.1999998...



      // closest float
      4.19999980926513671875
      // hoped for value
      4.2
      // next closest float
      4.200000286102294921875


      Multiplexing by 100 incurs some rounding and the best answer to m*100 is then 419.999969482421875.



      int nm =(m * 100 ); results nm == 419 as assignment of a float to int truncates the fractional portion away.





      Consider rounding to the nearest integer rather than truncating via int assignment and use double constants.



      #include <math.h>

      // int nm =(m * 100 );
      int nm = lround(m * 100.0);
      // or
      int nm = round(m * 100.0);





      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        A 32-bit float can encode about 232 different values exactly. Due to the binary nature of the typical float, 4.2 is not one of them.



        Instead m has the value of 4.1999998...



        // closest float
        4.19999980926513671875
        // hoped for value
        4.2
        // next closest float
        4.200000286102294921875


        Multiplexing by 100 incurs some rounding and the best answer to m*100 is then 419.999969482421875.



        int nm =(m * 100 ); results nm == 419 as assignment of a float to int truncates the fractional portion away.





        Consider rounding to the nearest integer rather than truncating via int assignment and use double constants.



        #include <math.h>

        // int nm =(m * 100 );
        int nm = lround(m * 100.0);
        // or
        int nm = round(m * 100.0);





        share|improve this answer














        A 32-bit float can encode about 232 different values exactly. Due to the binary nature of the typical float, 4.2 is not one of them.



        Instead m has the value of 4.1999998...



        // closest float
        4.19999980926513671875
        // hoped for value
        4.2
        // next closest float
        4.200000286102294921875


        Multiplexing by 100 incurs some rounding and the best answer to m*100 is then 419.999969482421875.



        int nm =(m * 100 ); results nm == 419 as assignment of a float to int truncates the fractional portion away.





        Consider rounding to the nearest integer rather than truncating via int assignment and use double constants.



        #include <math.h>

        // int nm =(m * 100 );
        int nm = lround(m * 100.0);
        // or
        int nm = round(m * 100.0);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 19:36

























        answered Nov 19 at 19:31









        chux

        78.9k869145




        78.9k869145






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53381216%2fwhenever-i-enter-4-2-in-this-code-the-value-of-nm-is-19-where-20-is-expected%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python