Why as.integer() is returning incorrect value?





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







0















I am using the following expression in my R-script and getting an incorrect value. I was wondering whether it is a bug or I am doing something wrong. Any help will be appreciated.




as.integer(10.7275*1e7)




it is returning 107274999. I was expecting it to return 107275000. How can I fix that?










share|improve this question

























  • Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this

    – RAB
    Jan 3 at 4:50






  • 2





    print(10.7275, digits = 18) gives > [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to use ceiling(10.7275*1e7)

    – Suren
    Jan 3 at 4:55













  • simply use ceiling(10.7275*1e7)

    – Hunaidkhan
    Jan 3 at 4:58











  • Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).

    – Nazmul
    Jan 3 at 5:02











  • Many thanks Suren and Hunaidkhan

    – Nazmul
    Jan 3 at 5:08


















0















I am using the following expression in my R-script and getting an incorrect value. I was wondering whether it is a bug or I am doing something wrong. Any help will be appreciated.




as.integer(10.7275*1e7)




it is returning 107274999. I was expecting it to return 107275000. How can I fix that?










share|improve this question

























  • Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this

    – RAB
    Jan 3 at 4:50






  • 2





    print(10.7275, digits = 18) gives > [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to use ceiling(10.7275*1e7)

    – Suren
    Jan 3 at 4:55













  • simply use ceiling(10.7275*1e7)

    – Hunaidkhan
    Jan 3 at 4:58











  • Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).

    – Nazmul
    Jan 3 at 5:02











  • Many thanks Suren and Hunaidkhan

    – Nazmul
    Jan 3 at 5:08














0












0








0








I am using the following expression in my R-script and getting an incorrect value. I was wondering whether it is a bug or I am doing something wrong. Any help will be appreciated.




as.integer(10.7275*1e7)




it is returning 107274999. I was expecting it to return 107275000. How can I fix that?










share|improve this question
















I am using the following expression in my R-script and getting an incorrect value. I was wondering whether it is a bug or I am doing something wrong. Any help will be appreciated.




as.integer(10.7275*1e7)




it is returning 107274999. I was expecting it to return 107275000. How can I fix that?







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 10:58









Wai Ha Lee

6,126124166




6,126124166










asked Jan 3 at 4:46









NazmulNazmul

11




11













  • Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this

    – RAB
    Jan 3 at 4:50






  • 2





    print(10.7275, digits = 18) gives > [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to use ceiling(10.7275*1e7)

    – Suren
    Jan 3 at 4:55













  • simply use ceiling(10.7275*1e7)

    – Hunaidkhan
    Jan 3 at 4:58











  • Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).

    – Nazmul
    Jan 3 at 5:02











  • Many thanks Suren and Hunaidkhan

    – Nazmul
    Jan 3 at 5:08



















  • Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this

    – RAB
    Jan 3 at 4:50






  • 2





    print(10.7275, digits = 18) gives > [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to use ceiling(10.7275*1e7)

    – Suren
    Jan 3 at 4:55













  • simply use ceiling(10.7275*1e7)

    – Hunaidkhan
    Jan 3 at 4:58











  • Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).

    – Nazmul
    Jan 3 at 5:02











  • Many thanks Suren and Hunaidkhan

    – Nazmul
    Jan 3 at 5:08

















Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this

– RAB
Jan 3 at 4:50





Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this

– RAB
Jan 3 at 4:50




2




2





print(10.7275, digits = 18) gives > [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to use ceiling(10.7275*1e7)

– Suren
Jan 3 at 4:55







print(10.7275, digits = 18) gives > [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to use ceiling(10.7275*1e7)

– Suren
Jan 3 at 4:55















simply use ceiling(10.7275*1e7)

– Hunaidkhan
Jan 3 at 4:58





simply use ceiling(10.7275*1e7)

– Hunaidkhan
Jan 3 at 4:58













Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).

– Nazmul
Jan 3 at 5:02





Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).

– Nazmul
Jan 3 at 5:02













Many thanks Suren and Hunaidkhan

– Nazmul
Jan 3 at 5:08





Many thanks Suren and Hunaidkhan

– Nazmul
Jan 3 at 5:08












2 Answers
2






active

oldest

votes


















3














This is due to computer inaccuracy with decimals (as mentioned in comments by @Suren). You can get around it by:



as.integer(ceiling(10.7275*1e7))


Note:



ceiling(as.integer(10.7275*1e7)) 


will not work (it will return the value you are getting now).






share|improve this answer































    -1














    Its a floating point issue. One quick fix would be to use double precision floating point for better accuracy:



    as.double(10.7275*1e7)                       # alternatively as.numeric()

    # [1] 107275000


    Also refer to this floating point post to get more information and resolutions for floating point math as commented by @Wai Ha Lee






    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',
      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%2f54016488%2fwhy-as-integer-is-returning-incorrect-value%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









      3














      This is due to computer inaccuracy with decimals (as mentioned in comments by @Suren). You can get around it by:



      as.integer(ceiling(10.7275*1e7))


      Note:



      ceiling(as.integer(10.7275*1e7)) 


      will not work (it will return the value you are getting now).






      share|improve this answer




























        3














        This is due to computer inaccuracy with decimals (as mentioned in comments by @Suren). You can get around it by:



        as.integer(ceiling(10.7275*1e7))


        Note:



        ceiling(as.integer(10.7275*1e7)) 


        will not work (it will return the value you are getting now).






        share|improve this answer


























          3












          3








          3







          This is due to computer inaccuracy with decimals (as mentioned in comments by @Suren). You can get around it by:



          as.integer(ceiling(10.7275*1e7))


          Note:



          ceiling(as.integer(10.7275*1e7)) 


          will not work (it will return the value you are getting now).






          share|improve this answer













          This is due to computer inaccuracy with decimals (as mentioned in comments by @Suren). You can get around it by:



          as.integer(ceiling(10.7275*1e7))


          Note:



          ceiling(as.integer(10.7275*1e7)) 


          will not work (it will return the value you are getting now).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 5:08









          RABRAB

          1,416418




          1,416418

























              -1














              Its a floating point issue. One quick fix would be to use double precision floating point for better accuracy:



              as.double(10.7275*1e7)                       # alternatively as.numeric()

              # [1] 107275000


              Also refer to this floating point post to get more information and resolutions for floating point math as commented by @Wai Ha Lee






              share|improve this answer






























                -1














                Its a floating point issue. One quick fix would be to use double precision floating point for better accuracy:



                as.double(10.7275*1e7)                       # alternatively as.numeric()

                # [1] 107275000


                Also refer to this floating point post to get more information and resolutions for floating point math as commented by @Wai Ha Lee






                share|improve this answer




























                  -1












                  -1








                  -1







                  Its a floating point issue. One quick fix would be to use double precision floating point for better accuracy:



                  as.double(10.7275*1e7)                       # alternatively as.numeric()

                  # [1] 107275000


                  Also refer to this floating point post to get more information and resolutions for floating point math as commented by @Wai Ha Lee






                  share|improve this answer















                  Its a floating point issue. One quick fix would be to use double precision floating point for better accuracy:



                  as.double(10.7275*1e7)                       # alternatively as.numeric()

                  # [1] 107275000


                  Also refer to this floating point post to get more information and resolutions for floating point math as commented by @Wai Ha Lee







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 6 at 15:35

























                  answered Jan 3 at 4:58









                  Mankind_008Mankind_008

                  1,5682413




                  1,5682413






























                      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%2f54016488%2fwhy-as-integer-is-returning-incorrect-value%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

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith