Hi,
just had a question about return value and type of some calculation.
if I have the following variables:
int a = 10000;
int b = 10000;
long c;
c = a * b;
what will be the value of c?
I know theoretically it should be 100,000,000. But isn't it true that an operation between ints will return an int?
Please correct me if I am wrong. Will c hold 100mil?
Thanks
system
February 7, 2013, 7:55pm
2
It depends on which processor you're using.
If ints are 32 bit or bigger, yes, otherwise no.
I am using arduino mega 2560. so the ints are 16 bit long.
so I guess an int calculation will return an int...
How about this:
if I have the following variables:
long a = 10000;
int b = 10000;
long c;
c = a * b;
what will be the value of c?
system
February 7, 2013, 8:01pm
4
I haven't got a calculator to hand, but it'll be negative.
how come?
I changed the type of variable 'a' to long. So the operation should return a long variable. shouldn't it???
system
February 7, 2013, 8:20pm
6
I'd expect the compiler to perform a long multiplication i.e. promoting the value of b to a long, and returning the long value 100000000.
But why don't you try it and see what actually happens, instead of asking us ?
Why guess when you can query?
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <limits.h>
#define lowByte(w) ((uint8_t)((w) & 0xff))
#define highByte(w) ((uint8_t)((w) >> 8))
void loop()
{}
void setup()
{
Serial.begin(9600);
// --- IDENTIFY ENDIAN ORDERING
short number = 0x0001L;
Serial.println((1 == ((char*)&number)[sizeof(number) - 1]) ? "Big Endian\n" : "Little Endian\n");
Serial.print("Lo Byte '0xDEAD': "); Serial.println(lowByte(0xDEAD), HEX);
Serial.print("Hi Byte '0xDEAD': "); Serial.println(highByte(0xDEAD), HEX);
// --- REPORT SIZE AND RANGES OF DATA TYPES
Serial.println("\n\nReporting size, and range, of \'intrinc\' data types -");
Serial.print("bool: "); Serial.println(sizeof(bool));
Serial.print("bits per 'char': "); Serial.println(CHAR_BIT);
Serial.print("char: "); Serial.println(sizeof(char));
Serial.print("'char' minimum: "); Serial.println(CHAR_MIN);
Serial.print("'char' maximum: "); Serial.println(CHAR_MAX);
Serial.print("'signed char' minimum: "); Serial.println(SCHAR_MIN);
Serial.print("'signed char' maximum: "); Serial.println(SCHAR_MAX);
Serial.print("'unsigned char' maximum: "); Serial.println(UCHAR_MAX);
Serial.print("short: "); Serial.println(sizeof(short));
Serial.print("'signed short' minimum: "); Serial.println(SHRT_MIN);
Serial.print("'signed short' maximum: "); Serial.println(SHRT_MAX);
Serial.print("'unsigned short' maximum: "); Serial.println(USHRT_MAX);
Serial.print("int: "); Serial.println(sizeof(int));
Serial.print("'signed int' minimum: "); Serial.println(INT_MIN);
Serial.print("'signed int' maximum: "); Serial.println(INT_MAX);
Serial.print("'unsigned int' maximum: "); Serial.println(UINT_MAX);
Serial.print("long: "); Serial.println(sizeof(long));
Serial.print("'signed long' minimum: "); Serial.println(LONG_MIN);
Serial.print("'signed long' maximum: "); Serial.println(LONG_MAX);
Serial.print("'unsigned long' maximum: "); Serial.println(ULONG_MAX);
Serial.print("long long: "); Serial.println(sizeof(long long));
#if 0
Serial.print("'signed long long' minimum: "); Serial.println(LONG_LONG_MIN);
Serial.print("'signed long long' maximum: "); Serial.println(LONG_LONG_MAX);
Serial.print("'unsigned long long' maximum: "); Serial.println(ULONG_LONG_MAX);
#endif
Serial.print("float: "); Serial.println(sizeof(float));
Serial.print("double: "); Serial.println(sizeof(double));
Serial.print("Pointer: "); Serial.println(sizeof(void*));
Serial.println("\n\nReporting size of some \'Arduino\' data types -");
Serial.print("byte size: "); Serial.println(sizeof(byte));
Serial.print("word size: "); Serial.println(sizeof(word));
Serial.end();
}
Denbo
February 7, 2013, 8:31pm
8
I originally posted this back in October
FardinB:
c = a * b;
what will be the value of c?
I know theoretically it should be 100,000,000. But isn't it true that an operation between ints will return an int?
Please correct me if I am wrong. Will c hold 100mil?
I simply don't understand why you don't write a 5-line program and find out for yourself. To be certain about our responses, we would have to do that anyway. So why don't you?
It's like asking "there's a light switch in my room - if I push it will the lights go on?". Well, try it and see.
What have you tried?
When multiplying two values the max number of bits needed for the result will be the sum of the number of bits required of the multipliers.
FardinB:
How about this:
if I have the following variables:
long a = 10000;
int b = 10000;
long c;
c = a * b;
what will be the value of c?
Here, let me do it for you:
void setup ()
{
Serial.begin (115200);
long a = 10000;
int b = 10000;
long c;
c = a * b;
Serial.println (c);
} // end of setup
void loop () { }
Output:
100000000
Change "a" to be an int, and you get, as expected:
-7936
That is because the calculation is doing int arithmetic, and then storing the result into a long.