round(x) in mbed

04 Jan 2010

Hello all,

I am trying to use round in mbed not sure why is not working.

I am porting some code from arduino:

#include "mbed.h"

void myFunction( int myInt, long time){

int _rounded = round( time / myInt);

}

I get a: Identifier round not defined.

mbed.h already includes math.h and stdio.h no?

Thank you!

Angel

04 Jan 2010

I do not think round() is in the ANSI norm of C. So I am not sure but the fuction may not exist. you should use type cast with something like that :

 

int_rounded=(int)((float)(time)/(float)(myInt)+0.5);

05 Jan 2010

Thank you!

05 Jan 2010

Hi Angel,

The functions you are probably looking for are in math.h, and are called:

Thanks!,
Simon

05 Jan 2010

 

Simon Ford wrote:

Hi Angel,

The functions you are probably looking for are in math.h, and are called:

Thanks!,
Simon

 

 

05 Jan 2010

Thank you Simon!

05 Jan 2010 . Edited: 05 Jan 2010

 

Simon Ford wrote:

Hi Angel,

The functions you are probably looking for are in math.h, and are called:

Thanks!,
Simon

I think he was looking to get an int back, so he'd have to cast the returned values to int, so the suggestion by Stephane is better (or as good, anyway). However, it occurs to me that as both values are integer types, using the / operator should result in an integer division, which will be rounded down, anyway - see this discussion for more info.

Dave

05 Jan 2010 . Edited: 05 Jan 2010

Hi Angel, Dave,

Yes, the return value is float, but if you assign it to an int it should get implicitly cast, so you can write:

int x = floor(5.6);

Another one to look at is rint(x), which usually rounds to nearest.

Simon

31 Mar 2014

When I try typecasting a float to int I get nonsense numbers like -0.000000 and mbed is not recognizing round trunc floor and the and so on as functions. Anyone have any Ideas whats going on? I'm running on a FRDM-KL25Z if that helps. Also the Blue LED lights up when I try to do this.

here is my code

  1. include "mbed.h"
  2. include "math.h"
  3. include "BMP085.h"

BMP085 barom(PTC9, PTC8); Serial pc(USBTX, USBRX); DigitalOut myled(LED_GREEN);

int main() { barom.init(); while (true) { wait(2); pc.printf("%d\r\n", barom.get_temperature()); pc.printf("%d\r\n", barom.get_pressure()); pc.printf("%f\r\n", (int)(barom.get_altitude_ft())); pc.printf("%f\r\n", barom.get_altitude_ft()); myled = !myled; } }

31 Mar 2014

This is a kinda old topic ;)

If you add <<code>> and <</code>> it is better readable. You are now typecasting a float to an int, but you are still printing it as a float with %f. So that is most likely going wrong.

Regarding lighting of the blue LED, do you have anything connected to PTD1, aka D13? This is the same as the blue LED pin, so if you have anything there pulling it to ground, the LED will turn on.

31 Mar 2014

Thanks Eric! you where right about both of my problems.