Go Down

Topic: Math.round in Arduino (Read 6767 times) previous topic - next topic

Babak1

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?

madworm

#1
Dec 09, 2011, 10:20 pm Last Edit: Dec 09, 2011, 10:23 pm by madworm Reason: 1
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: [Select]
#include <math.h>

and of course

Code: [Select]
var2 = round(var1);
• 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!

Babak1

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.

AWOL

#3
Dec 09, 2011, 10:38 pm Last Edit: Dec 09, 2011, 10:40 pm by AWOL Reason: 1
Round (a);
?
Math function not working, or test method not working?   ;)
"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.

PeterH

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.
I only provide help via the forum - please do not contact me for private consultancy.

dxw00d

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

James C4S


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: [Select]

   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: [Select]
b = round(z);
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Babak1

Thank you for all your help, I have it working now.   :)

Go Up