Pages: [1]   Go Down
Author Topic: Math.round in Arduino  (Read 5184 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am trying to convert a float to an int and round up/down as necessary.

This works to convert to an int.

#include <Math.h>

void setup() {
  Serial.begin(9600);
}

void loop() {

   float z = 78.923;
    int a = (int) z;
  Serial.println(z);
  Serial.println(a);
  Serial.println();
  delay(2000);
  }

However I can't get the Math.round function to work.

#include <Math.h>

void setup() {
  Serial.begin(9600);
}

void loop() {

   float z = 78.923;
//    int a = (int) z;
    int a = (int) Math.round (z);
  Serial.println(z);
  Serial.println(a);
  Serial.println();
  delay(2000);
  }

This give me an error "'Math' was not declared in this scope"

can anyone tell me what I am doing wrong?
Logged

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please use code tags when posting code! It's the # symbol next to the typewriter in the editor.

Your problem is case sensitivity. It should be

Code:
#include <math.h>

and of course

Code:
var2 = round(var1);
« Last Edit: December 09, 2011, 04:23:30 pm by madworm » Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I changed the code like this:

#include <math.h>

int a;
int b;

void setup() {
  Serial.begin(9600);
}

void loop() {
   float z = 78.923;
   a = (int) z;
   b = round(a);
  Serial.print("z = ");
  Serial.println(z);
  Serial.print("a = ");
  Serial.println(a);
  Serial.print("b = ");
  Serial.println(b);
  Serial.println();

But the result is:

z = 78.92
a = 78
b = 78

The round functions is not working.
Logged

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

Round (a);
?
Math function not working, or test method not working?   smiley-wink
« Last Edit: December 09, 2011, 04:40:15 pm by AWOL » 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.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want to round a float to the nearest whole number, just add 0.5 and cast to the appropriate integer type. Decide how you want to deal with -ve values, if you expect any. In some situations you want to always round downwards, in other situations it's appropriate to round towards zero. If you're doing anything statistical with the results, that distinction is important.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Round is working correctly, but you are doing round((int)78.923), which is 78.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please use code tags when posting code! It's the # symbol next to the typewriter in the editor.
You were already asked once and ignored it.  When posting code, please wrap them in code tags.

I changed the code like this:
The round functions is not working.
Your code works exactly as you wrote it.  See below

Code:
   float z = 78.923;   // z contains 78.923
   a = (int) z;          // a now contains 78
   b = round(a);      // b = round(78) = 78

You want:
Code:
b = round(z);
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for all your help, I have it working now.   smiley
Logged

Pages: [1]   Go Up
Jump to: