# Evaluating large numbers

I discovered that the result of a calculation inside an if statement is limited to a certain size. When the calculation is moved outside the statement, large numbers can be evaluated. I think the best way to do this is to create an extra variable that contains the value against another variable is matched, see Test 3 in my example below. Any thoughts on this? Is there a way to do this in a single line?

``````void setup(){
Serial.begin(9600); // Start communication over serial port.

long i  = 10000000;
// Test 0
if ( i > 10000001){
Serial.println("test 0 says true");
}
else{
Serial.println("test 0 says false");
}
// Test 2
if ( i < 10000001){
Serial.println("test 1 says true");
}
else{
Serial.println("test 1 says false");
}
// Test 2
if ( i < i + 1 ){
Serial.println("test 2 says true");
}
else{
Serial.println("test 2 says false");
}
// Test 3
long j = i + 1;
if ( i < j ){
Serial.println("test 3 says true");
}
else{
Serial.println("test 3 says false");
}
// Test 4
if ( i < 10*1000*1000+1 ){// Wrongly evaluated!
Serial.println("test 4 says true");
}
else{
Serial.println("test 4 says false");
}
// Test 5
if ( i < long(10*1000*1000+1) ){ // Wrongly evaluated - conversion doesn't seem to work
Serial.println("test 5 says true");
}
else{
Serial.println("test 5 says false");
}
}
void loop(){

}
``````

Results in:
test 0 says false
test 1 says true
test 2 says true
test 3 says true
test 4 says false
test 5 says false

So tests 4 and 5 return wrong results because of the large numbers in the calculation in the if statement!

Is there a way to do this in a single line?

Numeric literals are assumed to be integer values. If you need them to be interpreted differently, you need to use L or UL as a suffix.

long i = 10000000L;
// Test 0
if ( i > 10000001L){

kslstn:

``````if ( i < long(10*1000*1000+1) ){ // Wrongly evaluated - conversion doesn't seem to work
``````

Yes, of course you are doing wrong calculations as you have programmed it.

Default numeric type is “int” ans so “1010001000+1” ist much more than an 16-bit int can hold.

Perhaps try:

``````if ( i < 10L*1000L*1000L+1 )
``````

Perhaps you wanted doing calculation with ‘long’ constants?
So put letter ‘L’ behind the constant to be ‘long’!

Any long or unsigned long number in your code needs to be 1000000L or 1000L to have it read as a long instead on a regular integer. 1000000UL for unsigned long.

For casting try (long) 10*1000*1000+1

Alright, thanks! I never had to bother about size of variables, nice!

For others like me: unsigned long is a 4 byte variable that only stores positive values, long is also 4 bytes and can hold negatives.