Arduino: incorrect calculation of long integer
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
add a comment |
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
add a comment |
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
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
arduino esp8266
asked 1 hour ago
wBB
1134
1134
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
28 mins ago
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
28 mins ago
add a comment |
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.
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
28 mins ago
add a comment |
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.
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.
answered 1 hour ago
dmz
1264
1264
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
28 mins ago
add a comment |
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
28 mins 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
28 mins 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
28 mins ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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