Overflowed value prints non-overflowed value on Arduino












2















First of all, I know the problem with the code and how to get it to work. I'm mainly looking for an explanation why my output is what it is.



The following piece of code replicates the behaviour:



void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("start");
for(int i = 0; i < 70000; i++) {
if((i % 2000) == 0)
Serial.println(i);
}
}


Obviously the for loop will run forever because i will overflow at 32,767. I would expect it to overflow and print -32000.



Expected|   Actually printed
0 | 0
2000 | 2000
4000 | 4000
... | ...
30000 | 30000
32000 | 32000
-32000 | 33536
-30000 | 35536


It looks like it prints the actual iterations, since if you overflow and count to -32000 you would have 33536 iterations, but I can't figure out how it's able to print the 33536.



The same thing happens every 65536 iterations:



95536   |   161072  |   226608  |   292144  |   357680
97536 | 163072 | 228608 | 294144 | 359680
99072 | 164608 | 230144 | 295680 | 361216
101072 | 166608 | 232144 | 297680 | 363216


EDIT



When I change the loop to add 10.000 every iteration and only print every 1.000.000 to speed it up the Arduino crashes (or at least, the prints stop) at '2.147.000.000'. Which seems to point to the 32-bit idea of svtag**



EDIT2



The edits I made for the 2.147.000.000-check:



void loop() {
Serial.println("start");
for(int i = 0; i < 70000; i+=10000) {
if((i % 1000000) == 0)
Serial.println(i);
}
}


They work in the same trend with the previous example, printing 0, 1000000, 2000000,...



However, when I update the AVR package from 1.6.17 to the latest 1.6.23 I get only 0's. The original example (% 2000 & i++) still gives the same output.










share|improve this question

























  • This can be because of the difference in int definition on the Arduino and PC. Serially Arduino will send the binary equivalent of (int)33536 in 16bit, but if your PC interpret it as 32bit, then you should get the actual number. Just a hypothesis, I will check it as soon as i get the hardware.

    – svtag
    Nov 15 '18 at 15:21













  • I've also been thinking something along those lines, but then it shouldn't be able to print numbers larger than 65535. Because in 16 bit that would be the max value the arduino could output. Somehow it has some sort of shadowcopy that is 32bit it seems..

    – Lonefish
    Nov 15 '18 at 15:50






  • 1





    what Arduino board? you are on some 32bit board

    – Juraj
    Nov 15 '18 at 16:05








  • 1





    @svtag, please. it would be number of digits. the conversion to text is done on MCU

    – Juraj
    Nov 15 '18 at 16:22








  • 1





    written to Serial. in case of number, after conversion to text. number of digits. +2 for rn

    – Juraj
    Nov 15 '18 at 16:27


















2















First of all, I know the problem with the code and how to get it to work. I'm mainly looking for an explanation why my output is what it is.



The following piece of code replicates the behaviour:



void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("start");
for(int i = 0; i < 70000; i++) {
if((i % 2000) == 0)
Serial.println(i);
}
}


Obviously the for loop will run forever because i will overflow at 32,767. I would expect it to overflow and print -32000.



Expected|   Actually printed
0 | 0
2000 | 2000
4000 | 4000
... | ...
30000 | 30000
32000 | 32000
-32000 | 33536
-30000 | 35536


It looks like it prints the actual iterations, since if you overflow and count to -32000 you would have 33536 iterations, but I can't figure out how it's able to print the 33536.



The same thing happens every 65536 iterations:



95536   |   161072  |   226608  |   292144  |   357680
97536 | 163072 | 228608 | 294144 | 359680
99072 | 164608 | 230144 | 295680 | 361216
101072 | 166608 | 232144 | 297680 | 363216


EDIT



When I change the loop to add 10.000 every iteration and only print every 1.000.000 to speed it up the Arduino crashes (or at least, the prints stop) at '2.147.000.000'. Which seems to point to the 32-bit idea of svtag**



EDIT2



The edits I made for the 2.147.000.000-check:



void loop() {
Serial.println("start");
for(int i = 0; i < 70000; i+=10000) {
if((i % 1000000) == 0)
Serial.println(i);
}
}


They work in the same trend with the previous example, printing 0, 1000000, 2000000,...



However, when I update the AVR package from 1.6.17 to the latest 1.6.23 I get only 0's. The original example (% 2000 & i++) still gives the same output.










share|improve this question

























  • This can be because of the difference in int definition on the Arduino and PC. Serially Arduino will send the binary equivalent of (int)33536 in 16bit, but if your PC interpret it as 32bit, then you should get the actual number. Just a hypothesis, I will check it as soon as i get the hardware.

    – svtag
    Nov 15 '18 at 15:21













  • I've also been thinking something along those lines, but then it shouldn't be able to print numbers larger than 65535. Because in 16 bit that would be the max value the arduino could output. Somehow it has some sort of shadowcopy that is 32bit it seems..

    – Lonefish
    Nov 15 '18 at 15:50






  • 1





    what Arduino board? you are on some 32bit board

    – Juraj
    Nov 15 '18 at 16:05








  • 1





    @svtag, please. it would be number of digits. the conversion to text is done on MCU

    – Juraj
    Nov 15 '18 at 16:22








  • 1





    written to Serial. in case of number, after conversion to text. number of digits. +2 for rn

    – Juraj
    Nov 15 '18 at 16:27
















2












2








2


1






First of all, I know the problem with the code and how to get it to work. I'm mainly looking for an explanation why my output is what it is.



The following piece of code replicates the behaviour:



void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("start");
for(int i = 0; i < 70000; i++) {
if((i % 2000) == 0)
Serial.println(i);
}
}


Obviously the for loop will run forever because i will overflow at 32,767. I would expect it to overflow and print -32000.



Expected|   Actually printed
0 | 0
2000 | 2000
4000 | 4000
... | ...
30000 | 30000
32000 | 32000
-32000 | 33536
-30000 | 35536


It looks like it prints the actual iterations, since if you overflow and count to -32000 you would have 33536 iterations, but I can't figure out how it's able to print the 33536.



The same thing happens every 65536 iterations:



95536   |   161072  |   226608  |   292144  |   357680
97536 | 163072 | 228608 | 294144 | 359680
99072 | 164608 | 230144 | 295680 | 361216
101072 | 166608 | 232144 | 297680 | 363216


EDIT



When I change the loop to add 10.000 every iteration and only print every 1.000.000 to speed it up the Arduino crashes (or at least, the prints stop) at '2.147.000.000'. Which seems to point to the 32-bit idea of svtag**



EDIT2



The edits I made for the 2.147.000.000-check:



void loop() {
Serial.println("start");
for(int i = 0; i < 70000; i+=10000) {
if((i % 1000000) == 0)
Serial.println(i);
}
}


They work in the same trend with the previous example, printing 0, 1000000, 2000000,...



However, when I update the AVR package from 1.6.17 to the latest 1.6.23 I get only 0's. The original example (% 2000 & i++) still gives the same output.










share|improve this question
















First of all, I know the problem with the code and how to get it to work. I'm mainly looking for an explanation why my output is what it is.



The following piece of code replicates the behaviour:



void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("start");
for(int i = 0; i < 70000; i++) {
if((i % 2000) == 0)
Serial.println(i);
}
}


Obviously the for loop will run forever because i will overflow at 32,767. I would expect it to overflow and print -32000.



Expected|   Actually printed
0 | 0
2000 | 2000
4000 | 4000
... | ...
30000 | 30000
32000 | 32000
-32000 | 33536
-30000 | 35536


It looks like it prints the actual iterations, since if you overflow and count to -32000 you would have 33536 iterations, but I can't figure out how it's able to print the 33536.



The same thing happens every 65536 iterations:



95536   |   161072  |   226608  |   292144  |   357680
97536 | 163072 | 228608 | 294144 | 359680
99072 | 164608 | 230144 | 295680 | 361216
101072 | 166608 | 232144 | 297680 | 363216


EDIT



When I change the loop to add 10.000 every iteration and only print every 1.000.000 to speed it up the Arduino crashes (or at least, the prints stop) at '2.147.000.000'. Which seems to point to the 32-bit idea of svtag**



EDIT2



The edits I made for the 2.147.000.000-check:



void loop() {
Serial.println("start");
for(int i = 0; i < 70000; i+=10000) {
if((i % 1000000) == 0)
Serial.println(i);
}
}


They work in the same trend with the previous example, printing 0, 1000000, 2000000,...



However, when I update the AVR package from 1.6.17 to the latest 1.6.23 I get only 0's. The original example (% 2000 & i++) still gives the same output.







c++ arduino arduino-uno






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:27









gre_gor

4,13892631




4,13892631










asked Nov 15 '18 at 15:08









LonefishLonefish

3271620




3271620













  • This can be because of the difference in int definition on the Arduino and PC. Serially Arduino will send the binary equivalent of (int)33536 in 16bit, but if your PC interpret it as 32bit, then you should get the actual number. Just a hypothesis, I will check it as soon as i get the hardware.

    – svtag
    Nov 15 '18 at 15:21













  • I've also been thinking something along those lines, but then it shouldn't be able to print numbers larger than 65535. Because in 16 bit that would be the max value the arduino could output. Somehow it has some sort of shadowcopy that is 32bit it seems..

    – Lonefish
    Nov 15 '18 at 15:50






  • 1





    what Arduino board? you are on some 32bit board

    – Juraj
    Nov 15 '18 at 16:05








  • 1





    @svtag, please. it would be number of digits. the conversion to text is done on MCU

    – Juraj
    Nov 15 '18 at 16:22








  • 1





    written to Serial. in case of number, after conversion to text. number of digits. +2 for rn

    – Juraj
    Nov 15 '18 at 16:27





















  • This can be because of the difference in int definition on the Arduino and PC. Serially Arduino will send the binary equivalent of (int)33536 in 16bit, but if your PC interpret it as 32bit, then you should get the actual number. Just a hypothesis, I will check it as soon as i get the hardware.

    – svtag
    Nov 15 '18 at 15:21













  • I've also been thinking something along those lines, but then it shouldn't be able to print numbers larger than 65535. Because in 16 bit that would be the max value the arduino could output. Somehow it has some sort of shadowcopy that is 32bit it seems..

    – Lonefish
    Nov 15 '18 at 15:50






  • 1





    what Arduino board? you are on some 32bit board

    – Juraj
    Nov 15 '18 at 16:05








  • 1





    @svtag, please. it would be number of digits. the conversion to text is done on MCU

    – Juraj
    Nov 15 '18 at 16:22








  • 1





    written to Serial. in case of number, after conversion to text. number of digits. +2 for rn

    – Juraj
    Nov 15 '18 at 16:27



















This can be because of the difference in int definition on the Arduino and PC. Serially Arduino will send the binary equivalent of (int)33536 in 16bit, but if your PC interpret it as 32bit, then you should get the actual number. Just a hypothesis, I will check it as soon as i get the hardware.

– svtag
Nov 15 '18 at 15:21







This can be because of the difference in int definition on the Arduino and PC. Serially Arduino will send the binary equivalent of (int)33536 in 16bit, but if your PC interpret it as 32bit, then you should get the actual number. Just a hypothesis, I will check it as soon as i get the hardware.

– svtag
Nov 15 '18 at 15:21















I've also been thinking something along those lines, but then it shouldn't be able to print numbers larger than 65535. Because in 16 bit that would be the max value the arduino could output. Somehow it has some sort of shadowcopy that is 32bit it seems..

– Lonefish
Nov 15 '18 at 15:50





I've also been thinking something along those lines, but then it shouldn't be able to print numbers larger than 65535. Because in 16 bit that would be the max value the arduino could output. Somehow it has some sort of shadowcopy that is 32bit it seems..

– Lonefish
Nov 15 '18 at 15:50




1




1





what Arduino board? you are on some 32bit board

– Juraj
Nov 15 '18 at 16:05







what Arduino board? you are on some 32bit board

– Juraj
Nov 15 '18 at 16:05






1




1





@svtag, please. it would be number of digits. the conversion to text is done on MCU

– Juraj
Nov 15 '18 at 16:22







@svtag, please. it would be number of digits. the conversion to text is done on MCU

– Juraj
Nov 15 '18 at 16:22






1




1





written to Serial. in case of number, after conversion to text. number of digits. +2 for rn

– Juraj
Nov 15 '18 at 16:27







written to Serial. in case of number, after conversion to text. number of digits. +2 for rn

– Juraj
Nov 15 '18 at 16:27














1 Answer
1






active

oldest

votes


















0














The compiler may have automatically casted the i to usigned int when you are doing the println or the value is going to the wrong overload. Try using Serial.println((int)i);






share|improve this answer
























  • It manages to count beyond 65535, it counts until 2.147.000.000 (printing only millions), so it's a 32bit value somehow..

    – Lonefish
    Nov 15 '18 at 16:33











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%2f53322390%2foverflowed-value-prints-non-overflowed-value-on-arduino%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









0














The compiler may have automatically casted the i to usigned int when you are doing the println or the value is going to the wrong overload. Try using Serial.println((int)i);






share|improve this answer
























  • It manages to count beyond 65535, it counts until 2.147.000.000 (printing only millions), so it's a 32bit value somehow..

    – Lonefish
    Nov 15 '18 at 16:33
















0














The compiler may have automatically casted the i to usigned int when you are doing the println or the value is going to the wrong overload. Try using Serial.println((int)i);






share|improve this answer
























  • It manages to count beyond 65535, it counts until 2.147.000.000 (printing only millions), so it's a 32bit value somehow..

    – Lonefish
    Nov 15 '18 at 16:33














0












0








0







The compiler may have automatically casted the i to usigned int when you are doing the println or the value is going to the wrong overload. Try using Serial.println((int)i);






share|improve this answer













The compiler may have automatically casted the i to usigned int when you are doing the println or the value is going to the wrong overload. Try using Serial.println((int)i);







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 16:31









Branislav BrzakBranislav Brzak

93




93













  • It manages to count beyond 65535, it counts until 2.147.000.000 (printing only millions), so it's a 32bit value somehow..

    – Lonefish
    Nov 15 '18 at 16:33



















  • It manages to count beyond 65535, it counts until 2.147.000.000 (printing only millions), so it's a 32bit value somehow..

    – Lonefish
    Nov 15 '18 at 16:33

















It manages to count beyond 65535, it counts until 2.147.000.000 (printing only millions), so it's a 32bit value somehow..

– Lonefish
Nov 15 '18 at 16:33





It manages to count beyond 65535, it counts until 2.147.000.000 (printing only millions), so it's a 32bit value somehow..

– Lonefish
Nov 15 '18 at 16:33


















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%2f53322390%2foverflowed-value-prints-non-overflowed-value-on-arduino%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()