Weird!!!!!!

Here is my code:

void setup(){
  Serial.begin(9600);
  Serial.println("Connected to Arduino");
  Serial.println(8, DEC);
  Serial.println(8*1000, DEC);
  Serial.println(8*10000, DEC);
  Serial.println(8*100000/10, DEC);
}

void loop(){
}

Here is the output:

what the heck!!! please tell me im missing something

That is weird - I get the same thing with -0021 on a duemilanove.

Good…im not the only one.

To clarify, im on a duemilanove as well but im using -0022 now

There are multiple implementations of the println function within the Print object. I think you'll find that a different one is being called for your second-last statement. From the result it seems the implementation that takes an unsigned int is being called, ie 2^16 = 65536, 80000 - 2^16 = 14464.

If you cast the call into a long or unsigned long you'll something more sensible. It looks like a compiler bug possibly.

Cheers, G.

You saying something like this:

void setup(){
  Serial.begin(9600);
  Serial.println("Connected to Arduino");
  Serial.println(8, DEC);
  Serial.println(8*1000, DEC);
  Serial.println(long(8*10000), DEC);
  Serial.println(8*100000/10, DEC);
}

void loop(){
}

Cuz that doesnt change anything on my end

Well I normally do it this way... :-)

void setup(){
  Serial.begin(9600);
  Serial.println("Connected to Arduino");
  Serial.println(8, DEC);
  Serial.println(8*1000, DEC);
  Serial.println((long)(8*10000), DEC);
  Serial.println(8*100000/10, DEC);
}

void loop(){
}

If that's not working then there's something strange (and weird!) going on. Or I need another coffee - which is pretty much always true! :-)

G.

What happens if you simply call it with 80000 instead of the multiplication?

Yea that is the cleaner way....but nope still no difference when I just tried it.

Thanks for looking and commenting G.

change the 8 to a 9 and the odd output changes to 24464, to a 7 =4464, 6= -5536, 5=-15536, 4= -25536,3=30000, 4*9999=-25540... ...I guess the question is "where does this function misbehave"?

Tried this......printing '80000' straight prints correctly Typecasting '80000' into an 'int' does what you were expecting earlier

void setup(){
  Serial.begin(9600);
  Serial.println("Connected to Arduino");
  Serial.println(8, DEC);
  Serial.println(8*1000, DEC);
  Serial.println((long)(8*10000), DEC);
  Serial.println(80000, DEC);
  Serial.println((int)(80000), DEC);
  Serial.println(8*100000/10, DEC);
}

void loop(){
}

Hmmmmm.....any thoughts.....My head hurts.....

There's an unsigned long variant as well to try... and a double...

G.

What is your goal? I agree this is at face value odd!

My end use is taking a portion of a string like '.....,3447.0831,.....', grabbing the number out and storing it into a long for some mathematical computation. I'm sure there are a number of ways to do this and possibly get around my little conundrum but im kinda interested as to why this works like this.

My fix right now is to do the "x*100000/10". And another thing is, im not sure this is located in the print function, cuz i was having trouble with this problem when I try to reconstruct the number.

To explain a little further... Take '.....,3447.0831,.....' and make it become '34470831'. And the way i am doing that is to grab a character, convert it to a number and multiply it by the 10^x and then sum up all the characters. LIke..... 3*10000000 + 4*1000000 + 4*10000 ... 3*10 + 1. My end result never worked out correctly, and i tracked it down to the 10000th place i guess you would call it. 7*1000 was giving me the goofy number and screwing up my calc.

In C(++), with no indication otherwise, arithmetic operands and results are "int".

Serial.println(8*1000, DEC);

Both "8" and "1000" fit in an "int" so this is an "int" multiplied by an "int" with an "int" result.

Serial.println(8*10000, DEC);

Both "8" and "10000" fit in an "int" so this is an "int" multiplied by an "int". The result overflows an "int" resulting in the "weird" value.

Serial.println(8*100000/10, DEC);

"8" fits in an "int". "100000" does not fit in an "int" so the compiler treats the value as a "long". This is an "int" multiplied by a "long" (the division is irrelevant). The result is a "long".

To "fix" the "weird" case, YOU must tell the compiler to use "long" instead of "int" (pick one)...

Serial.println(8*10000L, DEC); Serial.println(8*(long)(10000), DEC); Serial.println(8L*10000, DEC); Serial.println((long)8*10000L, DEC);

The "weirdness" has nothing to do with Serial.println.

What "Coding Badly" said. Exactly, on all points...