Pages: [1] 2   Go Down
Author Topic: Question about data types  (Read 999 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
while (Wire.available() < 3)
  delay(1);
is the same as:
Code:
while (Wire.available() < 3)
{
  delay(1);
}
and,
Code:
while (Wire.available() < 3)
  digitalWrite(7, HIGH);
  delay(1);
  digitalWrite(7, LOW);
is the same as:
Code:
while (Wire.available() < 3)
{
  digitalWrite(7, HIGH);
}
delay(1);
digitalWrite(7, LOW);
There is also:
Code:
while (Wire.available() < 3);
(note the semicolon), which is the same as:
Code:
while (Wire.available() < 3)
{
  // do nothing
}

Moderator edit: Attempted to match-up code tags. I hope it worked. AWOL
« Last Edit: August 19, 2013, 02:26:08 am by AWOL » Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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)" ?

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've seen an "if-loop" being used instead,
An "if" by itself does not (cannot) loop.
Logged

"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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've seen an "if-loop" being used instead,
An "if" by itself does not (cannot) loop.

You're right smiley, what I meant is that an if-statement is sometimes used, for instance here: http://arduino.cc/en/Tutorial/SFRRangerReader.

Do they basically give the same results?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Thanks, but is for instance "(float) V1 / (float) V2"; the same as "float(V1) / float(V2)" ?
Yes they are the same, but the latter is not standard C it uses functions pre defined in the arduino IDE.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do they basically give the same results?
It depends on the context.
Code:
void loop ()
{
  while (Serial.available () <= someNumber) ;  // do nothing. Block "loop()"
  doSomething ();

vs.
Code:
void loop ()
{
  if (Serial.available () >= someNumber) // don't block "loop()"
  {
    doSomething();  
  
Logged

"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.

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Thanks, but is for instance "(float) V1 / (float) V2"; the same as "float(V1) / float(V2)" ?
Yes they are the same, but the latter is not standard C it uses functions pre defined in the arduino IDE.

It's standard C++ but not standard C.

The code below compiles in C++ but not C:

Code:
void foo ()
  {
  float bar = float (5);
  }

In C++ basically you are calling the constructor for type "float" and supplying it with the argument "5". C doesn't support constructors so that syntax isn't valid in C.
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Thanks, but is for instance "(float) V1 / (float) V2"; the same as "float(V1) / float(V2)" ?
Yes they are the same, but the latter is not standard C it uses functions pre defined in the arduino IDE.

Okay, I guess it's up for personal preferences then, but if I ever find myself attempting to program something in C, then I probably should get used to using standard C also when working with arduino IDE?
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino IDE uses C++, unless you happen to make an extra tab, and name it with a .c extension.
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino IDE uses C++, unless you happen to make an extra tab, and name it with a .c extension.

Okay. That's probably way ahead of my level anyway smiley
Logged

Offline Offline
God Member
*****
Karma: 7
Posts: 647
"In this house, we obey the Laws of Thermodynamics" Homer J. Simpson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

People have personal preferences and styles.
My preference is to always explicitly use the curly brackets because using them avoids simple mistakes.

As an example in the code below the indenting can easily fool you into thinking that all three statements are within the scope of the while loop when only the first is.
 
Code:
while (Wire.available() < 3)
  digitalWrite(7, HIGH);
  delay(1);
  digitalWrite(7, LOW);
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

People have personal preferences and styles.
My preference is to always explicitly use the curly brackets because using them avoids simple mistakes.

As an example in the code below the indenting can easily fool you into thinking that all three statements are within the scope of the while loop when only the first is.
 
Code:
while (Wire.available() < 3)
  digitalWrite(7, HIGH);
  delay(1);
  digitalWrite(7, LOW);

Which is exactly the reason I wrote it like that - to highlight the fact that the indenting is completely meaningless to the structure of the program.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1] 2   Go Up
Jump to: