Arduino: incorrect calculation of long integer












1














I'm doing a simple calculation with integers (on Arduino with ESP8266 12E), but I can't get the expected result and can't find the error. Can someone guide me?



#define A      200
#define B A * 62
#define C 500

void setup() {
Serial.begin(9600);
Serial.println("");

unsigned long aux = 0;

aux = (B * 500) / C; // (12400 * 500) / 500 = 12400
Serial.printf("aux = %dn", aux);
aux = aux * C; // 12400 * 500 = 6200000
Serial.printf("aux = %dn", aux);

// ERROR: Should result in "500", but is resulting in "1922000"
aux = aux / B; // 6200000 / 12400 = 500

Serial.printf("aux = %dn", aux); // It's printing "1922000"
}









share|improve this question



























    1














    I'm doing a simple calculation with integers (on Arduino with ESP8266 12E), but I can't get the expected result and can't find the error. Can someone guide me?



    #define A      200
    #define B A * 62
    #define C 500

    void setup() {
    Serial.begin(9600);
    Serial.println("");

    unsigned long aux = 0;

    aux = (B * 500) / C; // (12400 * 500) / 500 = 12400
    Serial.printf("aux = %dn", aux);
    aux = aux * C; // 12400 * 500 = 6200000
    Serial.printf("aux = %dn", aux);

    // ERROR: Should result in "500", but is resulting in "1922000"
    aux = aux / B; // 6200000 / 12400 = 500

    Serial.printf("aux = %dn", aux); // It's printing "1922000"
    }









    share|improve this question

























      1












      1








      1







      I'm doing a simple calculation with integers (on Arduino with ESP8266 12E), but I can't get the expected result and can't find the error. Can someone guide me?



      #define A      200
      #define B A * 62
      #define C 500

      void setup() {
      Serial.begin(9600);
      Serial.println("");

      unsigned long aux = 0;

      aux = (B * 500) / C; // (12400 * 500) / 500 = 12400
      Serial.printf("aux = %dn", aux);
      aux = aux * C; // 12400 * 500 = 6200000
      Serial.printf("aux = %dn", aux);

      // ERROR: Should result in "500", but is resulting in "1922000"
      aux = aux / B; // 6200000 / 12400 = 500

      Serial.printf("aux = %dn", aux); // It's printing "1922000"
      }









      share|improve this question













      I'm doing a simple calculation with integers (on Arduino with ESP8266 12E), but I can't get the expected result and can't find the error. Can someone guide me?



      #define A      200
      #define B A * 62
      #define C 500

      void setup() {
      Serial.begin(9600);
      Serial.println("");

      unsigned long aux = 0;

      aux = (B * 500) / C; // (12400 * 500) / 500 = 12400
      Serial.printf("aux = %dn", aux);
      aux = aux * C; // 12400 * 500 = 6200000
      Serial.printf("aux = %dn", aux);

      // ERROR: Should result in "500", but is resulting in "1922000"
      aux = aux / B; // 6200000 / 12400 = 500

      Serial.printf("aux = %dn", aux); // It's printing "1922000"
      }






      arduino esp8266






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      wBB

      1134




      1134






















          2 Answers
          2






          active

          oldest

          votes


















          10














          In your #define of B you missed parenthesis (). Change your definition to:



          #define B      (A * 62)


          Without parenthesis you first divide 6200000 by 200 and then multiply result by 62, which is not what you intend.






          share|improve this answer





















          • Dude, you're 100% right. I've spent several hours trying to figure out what was wrong ... Thank you so much!
            – wBB
            1 hour ago










          • @wBB remember that in C, macros are replaced in the code, exactly as you wrote them, in the preprocessor step before the code gets compiled. So it helps as a sanity-check in these cases to expand the macros yourself in your code to see if you're getting what you intended.
            – brhans
            1 hour ago



















          0














          Fully-parenthesizing macros (as noted in answer by dmz) solves one class of problem.



          Another thing you should do is, in any arithmetic expression which involves literal constants, use the L suffix on at least one of the constants involved if there's any chance the result will exceed 32767 (the maximum guaranteed-representable value for int). The type of an arithmetic operation in C is based on the types of the operands of that operation only; the type of the variable to which the result is assigned is irrelevant.






          share|improve this answer



















          • 1




            ...and the format specifier for an unsigned long is %lu not %d - the compiler for the asker's esp8266 uses a 32-bit int so they get away with some things they would not on an ATmega-based Arduino where an int is the minimum 16 bit size allowed by the specification.
            – Chris Stratton
            8 mins ago













          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("schematics", function () {
          StackExchange.schematics.init();
          });
          }, "cicuitlab");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "135"
          };
          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2felectronics.stackexchange.com%2fquestions%2f414153%2farduino-incorrect-calculation-of-long-integer%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









          10














          In your #define of B you missed parenthesis (). Change your definition to:



          #define B      (A * 62)


          Without parenthesis you first divide 6200000 by 200 and then multiply result by 62, which is not what you intend.






          share|improve this answer





















          • Dude, you're 100% right. I've spent several hours trying to figure out what was wrong ... Thank you so much!
            – wBB
            1 hour ago










          • @wBB remember that in C, macros are replaced in the code, exactly as you wrote them, in the preprocessor step before the code gets compiled. So it helps as a sanity-check in these cases to expand the macros yourself in your code to see if you're getting what you intended.
            – brhans
            1 hour ago
















          10














          In your #define of B you missed parenthesis (). Change your definition to:



          #define B      (A * 62)


          Without parenthesis you first divide 6200000 by 200 and then multiply result by 62, which is not what you intend.






          share|improve this answer





















          • Dude, you're 100% right. I've spent several hours trying to figure out what was wrong ... Thank you so much!
            – wBB
            1 hour ago










          • @wBB remember that in C, macros are replaced in the code, exactly as you wrote them, in the preprocessor step before the code gets compiled. So it helps as a sanity-check in these cases to expand the macros yourself in your code to see if you're getting what you intended.
            – brhans
            1 hour ago














          10












          10








          10






          In your #define of B you missed parenthesis (). Change your definition to:



          #define B      (A * 62)


          Without parenthesis you first divide 6200000 by 200 and then multiply result by 62, which is not what you intend.






          share|improve this answer












          In your #define of B you missed parenthesis (). Change your definition to:



          #define B      (A * 62)


          Without parenthesis you first divide 6200000 by 200 and then multiply result by 62, which is not what you intend.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          dmz

          1365




          1365












          • Dude, you're 100% right. I've spent several hours trying to figure out what was wrong ... Thank you so much!
            – wBB
            1 hour ago










          • @wBB remember that in C, macros are replaced in the code, exactly as you wrote them, in the preprocessor step before the code gets compiled. So it helps as a sanity-check in these cases to expand the macros yourself in your code to see if you're getting what you intended.
            – brhans
            1 hour ago


















          • Dude, you're 100% right. I've spent several hours trying to figure out what was wrong ... Thank you so much!
            – wBB
            1 hour ago










          • @wBB remember that in C, macros are replaced in the code, exactly as you wrote them, in the preprocessor step before the code gets compiled. So it helps as a sanity-check in these cases to expand the macros yourself in your code to see if you're getting what you intended.
            – brhans
            1 hour ago
















          Dude, you're 100% right. I've spent several hours trying to figure out what was wrong ... Thank you so much!
          – wBB
          1 hour ago




          Dude, you're 100% right. I've spent several hours trying to figure out what was wrong ... Thank you so much!
          – wBB
          1 hour ago












          @wBB remember that in C, macros are replaced in the code, exactly as you wrote them, in the preprocessor step before the code gets compiled. So it helps as a sanity-check in these cases to expand the macros yourself in your code to see if you're getting what you intended.
          – brhans
          1 hour ago




          @wBB remember that in C, macros are replaced in the code, exactly as you wrote them, in the preprocessor step before the code gets compiled. So it helps as a sanity-check in these cases to expand the macros yourself in your code to see if you're getting what you intended.
          – brhans
          1 hour ago













          0














          Fully-parenthesizing macros (as noted in answer by dmz) solves one class of problem.



          Another thing you should do is, in any arithmetic expression which involves literal constants, use the L suffix on at least one of the constants involved if there's any chance the result will exceed 32767 (the maximum guaranteed-representable value for int). The type of an arithmetic operation in C is based on the types of the operands of that operation only; the type of the variable to which the result is assigned is irrelevant.






          share|improve this answer



















          • 1




            ...and the format specifier for an unsigned long is %lu not %d - the compiler for the asker's esp8266 uses a 32-bit int so they get away with some things they would not on an ATmega-based Arduino where an int is the minimum 16 bit size allowed by the specification.
            – Chris Stratton
            8 mins ago


















          0














          Fully-parenthesizing macros (as noted in answer by dmz) solves one class of problem.



          Another thing you should do is, in any arithmetic expression which involves literal constants, use the L suffix on at least one of the constants involved if there's any chance the result will exceed 32767 (the maximum guaranteed-representable value for int). The type of an arithmetic operation in C is based on the types of the operands of that operation only; the type of the variable to which the result is assigned is irrelevant.






          share|improve this answer



















          • 1




            ...and the format specifier for an unsigned long is %lu not %d - the compiler for the asker's esp8266 uses a 32-bit int so they get away with some things they would not on an ATmega-based Arduino where an int is the minimum 16 bit size allowed by the specification.
            – Chris Stratton
            8 mins ago
















          0












          0








          0






          Fully-parenthesizing macros (as noted in answer by dmz) solves one class of problem.



          Another thing you should do is, in any arithmetic expression which involves literal constants, use the L suffix on at least one of the constants involved if there's any chance the result will exceed 32767 (the maximum guaranteed-representable value for int). The type of an arithmetic operation in C is based on the types of the operands of that operation only; the type of the variable to which the result is assigned is irrelevant.






          share|improve this answer














          Fully-parenthesizing macros (as noted in answer by dmz) solves one class of problem.



          Another thing you should do is, in any arithmetic expression which involves literal constants, use the L suffix on at least one of the constants involved if there's any chance the result will exceed 32767 (the maximum guaranteed-representable value for int). The type of an arithmetic operation in C is based on the types of the operands of that operation only; the type of the variable to which the result is assigned is irrelevant.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 mins ago

























          answered 11 mins ago









          mlp

          17215




          17215








          • 1




            ...and the format specifier for an unsigned long is %lu not %d - the compiler for the asker's esp8266 uses a 32-bit int so they get away with some things they would not on an ATmega-based Arduino where an int is the minimum 16 bit size allowed by the specification.
            – Chris Stratton
            8 mins ago
















          • 1




            ...and the format specifier for an unsigned long is %lu not %d - the compiler for the asker's esp8266 uses a 32-bit int so they get away with some things they would not on an ATmega-based Arduino where an int is the minimum 16 bit size allowed by the specification.
            – Chris Stratton
            8 mins ago










          1




          1




          ...and the format specifier for an unsigned long is %lu not %d - the compiler for the asker's esp8266 uses a 32-bit int so they get away with some things they would not on an ATmega-based Arduino where an int is the minimum 16 bit size allowed by the specification.
          – Chris Stratton
          8 mins ago






          ...and the format specifier for an unsigned long is %lu not %d - the compiler for the asker's esp8266 uses a 32-bit int so they get away with some things they would not on an ATmega-based Arduino where an int is the minimum 16 bit size allowed by the specification.
          – Chris Stratton
          8 mins ago




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Electrical Engineering 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.


          Use MathJax to format equations. MathJax reference.


          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%2felectronics.stackexchange.com%2fquestions%2f414153%2farduino-incorrect-calculation-of-long-integer%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

          Plaza Victoria

          Puebla de Zaragoza

          Musa