A
A
artanets2014-05-26 20:51:24
Arduino
artanets, 2014-05-26 20:51:24

Why can't arduino multiply beyond 5 digits?

Faced with the fact that when the result of calculating a numeric variable from more than a five-digit number, arduino starts to give the wrong result.
It has 1000*10=10000 (seems to be correct)
And already 1000*100= -31072
Here is the code I used to multiply:

int _ABVAR_1_varname;

void setup()
{
_ABVAR_1_varname = 0;
Serial.begin(9600);
}

void loop()
{
_ABVAR_1_varname = ( 1000 * 100 ) ;
Serial.print( _ABVAR_1_varname );
Serial.println("");
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2014-05-26
@artanets

Because int on it is 16-bit, and can only take values ​​from -32768 to 32767. Replace int with long, and 1000 * 100 with 1000l * 100l and you will be (temporarily) happy.

G
gabirx, 2014-06-05
@gabirx

or on unsigned long

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question