0

I have a question that seems really simple, but I think there's something in Processing that I'm not grokking. The issue is that when I add two ints together and the final amount is > 32k, the amount becomes negative.

Seems like an overflow issue except that the Processing reference states

Datatype for integers, numbers without a decimal point. Integers can be as large as 2,147,483,647 and as low as -2,147,483,648.

Here's the offending code and the Serial Monitor output:

int start_millis = millis();
int end_millis = start_millis + 5000;
  
Serial.println(start_millis);
Serial.println("");
Serial.println(end_millis);

And the monitor output:

!---Motion Detected; Flicker starting ---!
17616
22616
!--- Flicker Over ---!

!---Motion Detected; Flicker starting ---!
22986
27986
!--- Flicker Over ---!

!---Motion Detected; Flicker starting ---!
29569
-30967
phuclv
  • 37,963
  • 15
  • 156
  • 475
GilloD
  • 561
  • 2
  • 6
  • 18
  • 2
    Post and ye shall recieve. From the Arduino docs: On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). – GilloD Oct 16 '19 at 02:28

1 Answers1

1

"Datatype for integers, numbers without a decimal point. Integers can be as large as 2,147,483,647 and as low as -2,147,483,648. "

What's the "Processing reference"? Its statement above is wrong

Edit: as commented below, the statement is correct in language, but you're looking at the wrong manual. Processing is not C or C++ so checking it for other languages' features makes absolutely zero sense

In C and C++ standards int is a type that has at least 16 bits. In 8-bit and 16-bit architectures for obvious practical and performance reasons int is a 16-bit type. Therefore adding 2 numbers whose sum is larger than 32767 results in an overflow (since INT_MAX == 32767 in that case)

From Arduino docs:

On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). On the Arduino Due and SAMD based boards (like MKR1000 and Zero), an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1).

https://www.arduino.cc/reference/en/language/variables/data-types/int/

For more information read

Community
  • 1
  • 1
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • [Processing](https://processing.org/) is built on top of Java, not on C or C++, so its [reference](https://processing.org/reference/) is correct. – Kevin Workman Oct 16 '19 at 03:12
  • 1
    Right, I'm saying OP is confused because the Processing reference is for Java, but they're using it on Arduino, so they're at the mercy of C++ types, not Java types. In other words, the Processing reference is not wrong, because it's not talking about Arduino. – Kevin Workman Oct 16 '19 at 03:32