Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Rounding numbers (http://www.chiefdelphi.com/forums/showthread.php?t=50332)

teh_pwnerer795 07-12-2006 23:39

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

MattD 08-12-2006 00:18

Re: Rounding numbers
 
You can use the Math.round() method. Below is the two definitions of the method.

Code:

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


theycallhimtom 08-12-2006 00:29

Re: Rounding numbers
 
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...

Tom Bottiglieri 08-12-2006 00:50

Re: Rounding numbers
 
Quote:

Originally Posted by teh_pwnerer795
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:)

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?

Dave Scheck 08-12-2006 11:10

Re: Rounding numbers
 
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.

Salik Syed 14-12-2006 22:49

Re: Rounding numbers
 
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)
{
if(val-(int)val>=.5)
{
return (int)val+1;
}
else {
return (int)val;
}
}

teh_pwnerer795 15-12-2006 18:04

Re: Rounding numbers
 
Quote:

Originally Posted by Salik Syed (Post 541952)
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)
{
if(val-(int)val>=.5)
{
return (int)val+1;
}
else {
return (int)val;
}
}


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

neilsonster 15-12-2006 18:50

Re: Rounding numbers
 
Quote:

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

Originally Posted by Salik Syed (Post 541952)
[...] or just write your own function

public int round(double val)
{
if(val-(int)val>=.5)
{
return (int)val+1;
}
else {
return (int)val;
}
}

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.

teh_pwnerer795 15-12-2006 19:18

Re: Rounding numbers
 
Quote:

Originally Posted by neilsonster (Post 542105)
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....

neilsonster 15-12-2006 19:48

Re: Rounding numbers
 
Ohh I see. Actually, this line:

Code:

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 (i*262/10000 = i*0.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.

teh_pwnerer795 15-12-2006 19:57

Re: Rounding numbers
 
Quote:

Originally Posted by neilsonster (Post 542115)
Ohh I see. Actually, this line:

Code:

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 (i*262/10000 = i*0.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:)

Alan Anderson 15-12-2006 23:23

Re: Rounding numbers
 
Quote:

Originally Posted by teh_pwnerer795 (Post 542110)
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);


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
Code:

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.

teh_pwnerer795 16-12-2006 10:24

Re: Rounding numbers
 
Quote:

Originally Posted by Alan Anderson (Post 542163)
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
Code:

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

Astronouth7303 17-12-2006 12:16

Re: Rounding numbers
 
A few notes:
  • IIRC, integer vs float division is decided by the operands, not the target. Meaning that
    Code:

    int i = 5.0 / 3.0 * 4.0
    will be 6, while
    Code:

    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:
Code:

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.


All times are GMT -5. The time now is 01:20.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi