Rounding numbers

Wow…ima complete n00b at programming!! i should noe this by now!

ok i sorta learned java… cant really remember how to round in java… and im not sure if i should look for my java book and try to find out to round because im not sure if its the same in C… Does any1 noe what the command is?? plz and thxs:)

You can use the Math.round() method. Below is the two definitions of the method.

public static int round(float a)
public static long round(double a)

Usually when I want to round I just use some integer math. It can be kind of ugly but most of the times it works well and I do not have to memorize function calls. When you convert a floating point number to an int it will always round down so if you add .5 to a float then convert to an integer it will be rounded correctly.

float num = 1.5f;
int rounded = (int)(num+.5)

But anyway using a round function is probably better style…

Why did you mention C? Are you applying what you know in C to Java, or trying to use what you learned in a Java class to C?

I made a comment in your other thread, but you may want to check out the discussion on float variables. They aren’t the best choice for the RC hardware.

The problem with using a cast when trying to round is that all it does is remove the bits holding the decimal part of the number, this is not equivalent to rounding.
double number = 12.8743;
int val;
val = (int) number;

val now equals 12 … clearly not the result you want.

You could use math.Round or just write your own function

public int round(double val)
return (int)val+1;
else {
return (int)val;

math.Round is a java comand…doesnt work in C :frowning:

math.Round is a java comand…doesnt work in C

You should just follow his advice. If you do add a round function, remember to should add a declaration for the function to the header (.h) file [edit: or at the top of the .c file if you really want…] (sorry if this is obvious to you, but I’m not sure what kind of experience you have). I’m just curious… why do you need to round? You should avoid using floating point numbers on a robot’s processor, since it’s easy to run out of memory on those things. Stick to integers if at all possible. Remember that you don’t need pinpoint precision when running the robot.

Ye i noe wat u mean about the memory running out… For the auto mode i just want a delay for seconds… my code is using floating numbers… and i dont want that… i just want a insingle incrementing integer…

my code:

int i = 1;
static int time_i;

i=i+1; // This will increment ‘i’ by the value 1 every loop
time_i = ((i * 262) / 10000);

But thats going to caculate a floating number… i would just like a rounded integer from that sorta type of logic… srry if i seem confusing…

Ohh I see. Actually, this line:

time_i = ((i * 262) / 10000);

performs integer division, since time_i is an integer (you declared it as one). If you had declared time_i as a float/double, then this would give you a floating point number.

So after that line above, time_i is assigned 0 until i > 38 (i262/10000 = i0.0262 < 1, but since we have an integer it truncates the decimal part and gives you 0). For 38 <= i <= 76, time_i will be assigned 1 (you will have 1.***, and the decimal part will be removed). For 77 <= i <= 152, time_i = 2, and so on.

Awesome thanxs alot … makes more sense now:)

I believe you have declared the wrong variable as static. The way it stands now, i will be set to 1 then incremented to 2 each time through the loop, and time_i will always be computed as 0. If you change it to say

static int i = 1;
int time_i;

instead, i will remember its value between loops and be incremented as desired, and time_i will do as neilsonster said…

…at least until 125 loops have occurred, after a bit more than 3.25 seconds, after which time (i * 262) will overflow and you’ll start getting negative results.

Ok kool, i’ll just switch that now

A few notes:

  • IIRC, integer vs float division is decided by the operands, not the target. Meaning that
int i = 5.0 / 3.0 * 4.0

will be 6, while

float i = 5 / 3 * 4

will be 4.

  • For counters and times, and any other values that don’t go negative, you should be declaring them as “unsigned”. That is, “unsigned int”, “unsigned long”, etc. This doubles the upper bound and causes it to wrap to 0, not a negative number.
  • remember the data types: char = 1B, int = 2B, short long = 3B, long = 4B
  • Any time you think a calculation will over flow, cast one of the operands to a long. “((i * 262L) / 10000L)” should work fine for you.

Also, I find a series of typedef’s helpful:

typedef signed char SCHAR;
typedef unsigned char UCHAR;
typedef signed int SINT;
typedef unsigned int UINT;
typedef signed short long SSHORT;
typedef unsigned short long USHORT;
typedef signed long SLONG;
typedef unsigned long ULONG;

Put that in a “types.h”, and include it basically everywhere. A good place to define a “bool” type as well.