Go Down

Topic: Arduino calculation problem (Read 957 times) previous topic - next topic

direct99

hi,

I have a problem with arduino as I was trying to do simple calculations.


int value=(240/280)*100
Serial.println(value);


The result is Zero!

Nick Gammon

That's very unfortunate.

However, what is (240/280)? It is 0.857142857142857. Since we are dealing in integers, we truncate that, giving 0.

Then we multiply 0 by 100, giving 0.

So, the answer is 0!
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

fiddler

change your data type to Float.
Int does only hold whole numbers

fiddler

or go (240 x 100)/280
an int goes from -32,768 to 32,767, so you should just sneak in.

Still will only be an approx answer, but might be okay for your needs

direct99

the answer shouldn't be Zero.

Code: [Select]
(240/280)*100=85.71428571

AWOL

Anything multiplied by zero is zero.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon

Better read up on "int" data types then. They don't have decimal places.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

AWOL

Well, they do, but they're all to the left of the decimal point.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

fiddler

Can you please explain to me how an integer has decimal points ?
and how to divide 2 16bit ints and get a decimal point

WizenedEE

Well if you have a number that's from 0.00001 to 0.00050, you could just pretend that it's multiplied by 100,000 and say it's between 1 and 50 (or 10 and 500 or 100 and 5000)

Nick Gammon

#10
Jan 04, 2012, 04:52 am Last Edit: Jan 04, 2012, 04:56 am by Nick Gammon Reason: 1

Can you please explain to me how an integer has decimal points ?


I think AWOL is making a bit of a jest there. Integers are, like, 1, 2, 3, 4.

I suppose you could say that is 1.0, 2.0, 3.0, 4.0 where the numbers are "to the left of the decimal point". The implied decimal point.

As has been pointed out earlier, you can multiply first and then divide. For example, to express 3/4 as a percent:

Code: [Select]
3 * 100 / 4 = 75

That works because the expression is evaluated left-to-right.

However (with integers):

Code: [Select]
3 / 4 * 100 = 0

Because 3/4 is 0, and multiply 0 by 100 and you still get 0.

Multiplying first has its own hazards. eg. using signed 2-byte integers:

Code: [Select]
3000 * 100 / 4000 = -6

That's because 3000 * 100 is 30000 (0x493E0) which is truncated to 93E0, effectively becoming -6C20 (-27680) because the high-order bit (the sign bit) is set, divided by 4000, giving -6.

Example sketch, for anyone that doesn't believe me:

Code: [Select]
void setup ()
 {
 Serial.begin (115200);
 int a = 3000 * 100 / 4000;
 Serial.println (a);
 }
 
void loop () {}


Output:

Code: [Select]
-6
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
That's because 3000 * 100 is 30000

Time for some new batteries in your calculator, I think. Make sure you get the unsigned long kind.

Nick Gammon

Blasted batteries! I knew I should have paid more for them. :)

Well, perhaps 3000 * 100 is 300000.

As for my calculator, my old HP 20S now, very annoyingly, just refuses to turn on more often than not. And that's with new batteries.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up