Poll
Question: division question on Arduino
 division 2 (66.7%) Arduino 1 (33.3%)
Total Voters: 3

 Pages: [1]   Go Down
 Author Topic: division  (Read 583 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 19
 « on: March 08, 2012, 09:45:29 pm » Bigger Smaller Reset

when i run this code i get zero why ??
Code:
x = (analogLow + (analogHigh * 256));
X = ((x/1024)*1.2);
Serial.print("x=");
Serial.print( X, DEC);
Serial.print(" , ");

but when i delete X = ((x/1024)*1.2);

i get the value right but i want it to do the above equation ? any suggestion  ?
 Logged

Offline
Edison Member
Karma: 43
Posts: 1556
 « Reply #1 on: March 08, 2012, 09:59:03 pm » Bigger Smaller Reset

If you have declared x and X (bad choice of names by the way) to be integers then (x/1024) will be zero whenever x is less than 1024.

Pete

 Logged

Where are the Nick Gammons of yesteryear?

Offline
Newbie
Karma: 0
Posts: 19
 « Reply #2 on: March 08, 2012, 10:21:03 pm » Bigger Smaller Reset

i actually have it less then 1024

the answer should be = 0.5859
so i should declare my X to double or float ??
am confused with double and float which one should i use ??

thanks
 Logged

Offline
Sr. Member
Karma: 0
Posts: 318
Sometimes teaching, always learning,
 « Reply #3 on: March 08, 2012, 10:40:27 pm » Bigger Smaller Reset

Use float. In Arduino, double does the same thing though.

Just wanted to add, in most cases, it is best to multiply before dividing when working with integers.
if x = 20, (x/100)*25 = 0 ...but (x*25)/100 = 5
 Logged

Offline
Newbie
Karma: 0
Posts: 19
 « Reply #4 on: March 08, 2012, 10:43:08 pm » Bigger Smaller Reset

thats good point thanks
 Logged

Offline
Edison Member
Karma: 43
Posts: 1556
 « Reply #5 on: March 08, 2012, 10:44:31 pm » Bigger Smaller Reset

One way ot fix it is to change 1024 to 1024.0 which will then force the division as floating point instead of integer.
And declare X to be float or double. Float gives about 6 digits of precision and double gives about 12 (IIRC).
You will also have to change Serial.print( X, DEC); to Serial.print( X, 4); so that X is printed as a floating point number with 4 digits after the decimal point (or however many you want).

Pete
 Logged

Where are the Nick Gammons of yesteryear?

Offline
Newbie
Karma: 0
Posts: 19
 « Reply #6 on: March 08, 2012, 10:57:55 pm » Bigger Smaller Reset

thanks for that . well can i do like if i have x= 0.26 how can i make it = 0.3 which to a proximation value ??
 Logged

Global Moderator
Netherlands
Offline
Shannon Member
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #7 on: March 09, 2012, 01:59:55 am » Bigger Smaller Reset

x = x + roundingNr;
x = int(x*Factor)/Factor;

homework: find the two variables roundingNr and Factor ...
 Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline
Sr. Member
Karma: 0
Posts: 318
Sometimes teaching, always learning,
 « Reply #8 on: March 09, 2012, 10:34:38 pm » Bigger Smaller Reset

Quote
Float gives about 6 digits of precision and double gives about 12 (IIRC).
Although that may be the case in other programming languages, in Arduino float and double are essentially the same

http://arduino.cc/en/Reference/Double
 Logged

Offline
Edison Member
Karma: 43
Posts: 1556
 « Reply #9 on: March 09, 2012, 11:03:47 pm » Bigger Smaller Reset

Oooops. Thanks John.

Pete
 Logged

Where are the Nick Gammons of yesteryear?

 Pages: [1]   Go Up