Maths adding problem

So I have look at the forum to find a simple anser to this, but have not found one.
I have this simple code:

int x=0;
//int32_t x=0;
//long x=0;
//double x=0;

void loop()
{
x=532 * 4620;
Serial.println(x);  
}

But in every case it adds up wrong. Any idea what I need to declare x as to make it add up correctly.

Your post was MOVED to its current location as it is more suitable.

By the way the answer I get is -32528

int x=0;

x is declared as an int
An int on most Arduinos (which are you using ?) can hold a maximum value of 32,768

What is the result of 532 * 4620 ?
Will it fit in an int variable ?

Declare x a an unsigned long and see what happens

Hi, @Uknod
Welcome to the forum.

What answer were you expecting?

Where is the void setup() and serial.begin() statements?

Tom... :grinning: :+1: :coffee: :australia:

Yep, unsigned long worked, so why doesn't long work?

long is a signed variable with a maximum value of 2,147,483,647 which is smaller that the result of the multiplication (2,457,840)

EDIT- take no notice of what I wrote, it is incorrect. See later in the topic for more contributions

Ah, got it, thanks very much problem solved.

Can you maybe change the topic title to better reflect the subject?

Check the numbers again, I almost made the same mistake.

Not sure why unsigned works and signed does not, but math is usually done with integers, so you may need to tell the compiler that at least one of the numbers is a long for the multiplication.

Whaaaaaa????
2,147,483,647 is SMALLER THAN 2,457,840

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).

Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from >-2,147,483,648 to 2,147,483,647.

I tried using long and I got the wrong answer as @Uknod , but that doesn't explain why.

Ofcourse unsigned long worked;

Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won’t store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).

Tom... :grinning: :+1: :coffee: :australia:

I get the wrong answer using unsigned long, although it is positive because an unsigned number can never be negative. The compiler gives a warning of overflow regardless of the use of long or unsigned long.

Tell the compiler to do the math using longs:

  x = 532 * 4620L;

Yes, thats what I thought, I tohought I was loosing it there, so why long doesn;t work is a mystery, but like a lt of things arduino, you just have to go with what works, unsigned long works so I'm happy.

Change it to what, it seems pretty well named to me, arduino can't add up.

Hi,
My test code.

int x = 0;
long y = 0;
unsigned long z = 0;

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

void loop()
{
  x = 532 * 4620;
  Serial.println(" equation => variable = 532 * 4620 ");
  Serial.println(" ");
  Serial.print(" int x ");
  Serial.println(x);
  y = 532 * 4620;
  Serial.print(" long y ");
  Serial.println(y);
  z = 532 * 4620;
  Serial.print(" unsigned long z ");
  Serial.println(z);
  Serial.println("=============================================");
  delay(2000);
}

And my unsigned long == 4294934768

equation => variable = 532 * 4620

int x -32528
long y -32528
unsigned long z 4294934768

Calculated value = 2,457,840

Tom... :grinning: :+1: :coffee: :australia:

Which is not the product of 532 and 4620, it is the result of the compiler extending -32528 to an unsigned long.

What, that makes no sense, as my code is now working fine using unsigned long.

Which arduino are you using?

A genuine UNO, I'll try it on a copy nano, shall I.

Hi,
Revised test code.

int x = 0;
long y = 0;
unsigned long z = 0;

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

void loop()
{
  x = 532 * 4620;
  Serial.println(" equation => variable = 532 * 4620 ");
  Serial.println(" ");
  Serial.print(" int x ");
  Serial.println(x);
  y = 532 * 4620;
  Serial.print(" long y ");
  Serial.println(y);
  z = 532 * 4620;
  Serial.print(" unsigned long z ");
  Serial.println(z);
  z = 532L * 4620L;
  Serial.println(" z = 532L * 4620L  ");
  Serial.print(" unsigned long z ");
  Serial.println(z);
  Serial.println("=============================================");
  delay(2000);
}

Output;

equation => variable = 532 * 4620

int x -32528
long y -32528
unsigned long z 4294934768
z = 532L * 4620L
unsigned long z 2457840

Tom... :grinning: :+1: :coffee: :australia: