Go Down

Topic: Question about data types (Read 1 time) previous topic - next topic

Lars81

Okay, so I'm currently working on a project using the BMP085 barometric sensor, and I've found a nice tutorial over at Sparkfun (https://www.sparkfun.com/tutorials/253) which helps me complete it, but there are some things that I don't fully understand. Several places they use the term "(long)", "(float)" or "(unsigned long)" in front of some variables some places but not consistently, even though these variables have been defined as such further above. What's the reason for that? On the reference page they use float(variable), long(variable) etc, is it just the same as mentioned above?

Also they use the term  "while(Wire.available() < 3)" without the curly brackets, how does that work? On the reference page on arduino.cc they always use it with the curly brackets, still the same?

majenko

#1
Aug 18, 2013, 11:25 pm Last Edit: Aug 19, 2013, 09:26 am by AWOL Reason: 1
Adding a variable type in front of a variable in brackets is called "type casting".  It is how you convert a variable from one type to another in C and C++.  The float(...) etc functions provided by Arduino are just an abstraction of this functionality and are non-standard.

Any flow-control statement (while, if, etc) without curly brackets will affect the following line only.  For instance:

Code: [Select]

while (Wire.available() < 3)
 delay(1);

is the same as:
Code: [Select]

while (Wire.available() < 3)
{
 delay(1);
}

and,
Code: [Select]

while (Wire.available() < 3)
 digitalWrite(7, HIGH);
 delay(1);
 digitalWrite(7, LOW);

is the same as:
Code: [Select]

while (Wire.available() < 3)
{
 digitalWrite(7, HIGH);
}
delay(1);
digitalWrite(7, LOW);

There is also:
Code: [Select]

while (Wire.available() < 3);

(note the semicolon), which is the same as:
Code: [Select]

while (Wire.available() < 3)
{
 // do nothing
}


Moderator edit: Attempted to match-up code tags. I hope it worked. AWOL
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Grumpy_Mike

Quote
Several places they use the term "(long)", "(float)" or "(unsigned long)" in front of some variables some places but not consistently, even though these variables have been defined as such further above.

It is called casting, and it converts one data type into an other temporarily for use in calculations.
So suppose you want to divide two integers and  put the result into a float.
V1 and V2 being ints and result being float
Code: [Select]

result = V1 / V2; // will give a int division and then put the result into a float

result = (float) V1 / (float) V2; // will give you a floating point division.

Lars81


Adding a variable type in front of a variable in brackets is called "type casting".  It is how you convert a variable from one type to another in C and C++.  The float(...) etc functions provided by Arduino are just an abstraction of this functionality and are non-standard.

Any flow-control statement (while, if, etc) without curly brackets will affect the following line only.  For instance:




Thanks, that was very helpful!

In the code I referred to they use the version with the semicolon, but without curly brackets:
Code: [Select]

Wire.requestFrom(BMP085_ADDRESS, 2);
  while(Wire.available()<2) ;
  msb = Wire.read();
  lsb = Wire.read();
 
  return (int) msb<<8 | lsb;


Does that mean it will do nothing (because of the semicolon), untill it has received 2 bytes, and when it eventually does receive 2 bytes it will release the while loop and execute the following lines?

Other places I've seen an "if-loop" being used instead, is it basically the same or does one have an advantage over the other?

Lars81


Quote
Several places they use the term "(long)", "(float)" or "(unsigned long)" in front of some variables some places but not consistently, even though these variables have been defined as such further above.

It is called casting, and it converts one data type into an other temporarily for use in calculations.
So suppose you want to divide two integers and  put the result into a float.
V1 and V2 being ints and result being float
Code: [Select]

result = V1 / V2; // will give a int division and then put the result into a float

result = (float) V1 / (float) V2; // will give you a floating point division.



Thanks, but is for instance "(float) V1 / (float) V2"; the same as "float(V1) / float(V2)" ?


Go Up