Go to Post We can't quite define FIRST according to past standards and traditions, because it is shaping the future and does not conform to today's model of sports. - JaneYoung [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 07-12-2006, 23:39
teh_pwnerer795 teh_pwnerer795 is offline
Registered User
AKA: !!Mitch!!
FRC #1392
Team Role: Programmer
 
Join Date: Nov 2006
Rookie Year: 2006
Location: Ajax, Canada
Posts: 142
teh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really nice
Send a message via MSN to teh_pwnerer795 Send a message via Yahoo to teh_pwnerer795
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
__________________
Great Art is Created; Master Pieces are Stolen

[n00b]Teh_Foxdonut teh_pwnerer795 - CounterStrike:Source
  #2   Spotlight this post!  
Unread 08-12-2006, 00:18
MattD's Avatar
MattD MattD is offline
Registered User
AKA: Matthew Douglas
FRC #0228 (GUS Robotics)
Team Role: Alumni
 
Join Date: Feb 2006
Rookie Year: 2005
Location: Indianapolis, IN
Posts: 185
MattD is a splendid one to beholdMattD is a splendid one to beholdMattD is a splendid one to beholdMattD is a splendid one to beholdMattD is a splendid one to beholdMattD is a splendid one to beholdMattD is a splendid one to behold
Send a message via AIM to MattD
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)
__________________
GUS Robotics Team 228

2010 WPI Engineering Inspiration Award
2010 WPI Regional Champions (Thanks 230 & 20!)
2010 CT VEX Champions
2010 CT VEX Innovate Award
2009 QCC VEX Champions
2009 CT Motorola Quality Award
2007 CT J&J Sportsmanship Award
2006 CT Best Website Award
  #3   Spotlight this post!  
Unread 08-12-2006, 00:29
theycallhimtom theycallhimtom is offline
Registered User
AKA: Tom Conerly
FRC #1540 (The Flaming Chickens)
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Lake Oswego, Oregon
Posts: 52
theycallhimtom has a spectacular aura abouttheycallhimtom has a spectacular aura abouttheycallhimtom has a spectacular aura about
Send a message via AIM to theycallhimtom Send a message via MSN to theycallhimtom
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...
  #4   Spotlight this post!  
Unread 08-12-2006, 00:50
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,187
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
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?
  #5   Spotlight this post!  
Unread 08-12-2006, 11:10
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
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.

Last edited by Dave Scheck : 08-12-2006 at 14:28.
  #6   Spotlight this post!  
Unread 14-12-2006, 22:49
Salik Syed Salik Syed is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Alumni
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Stanford CA.
Posts: 514
Salik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud of
Send a message via AIM to Salik Syed
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;
}
}
__________________
Team 701
  #7   Spotlight this post!  
Unread 15-12-2006, 18:04
teh_pwnerer795 teh_pwnerer795 is offline
Registered User
AKA: !!Mitch!!
FRC #1392
Team Role: Programmer
 
Join Date: Nov 2006
Rookie Year: 2006
Location: Ajax, Canada
Posts: 142
teh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really nice
Send a message via MSN to teh_pwnerer795 Send a message via Yahoo to teh_pwnerer795
Re: Rounding numbers

Quote:
Originally Posted by Salik Syed View Post
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
__________________
Great Art is Created; Master Pieces are Stolen

[n00b]Teh_Foxdonut teh_pwnerer795 - CounterStrike:Source
  #8   Spotlight this post!  
Unread 15-12-2006, 18:50
neilsonster's Avatar
neilsonster neilsonster is offline
Registered User
AKA: Andrew Neilson
FRC #0772 (Sabre Bytes)
Team Role: College Student
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Waterloo, ON, Canada
Posts: 188
neilsonster is a name known to allneilsonster is a name known to allneilsonster is a name known to allneilsonster is a name known to allneilsonster is a name known to allneilsonster is a name known to all
Send a message via MSN to neilsonster
Re: Rounding numbers

Quote:
math.Round is a java comand...doesnt work in C
Quote:
Originally Posted by Salik Syed View Post
[...] 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.
__________________
Andrew Neilson - Team 772 Alumnus
University of Waterloo Computer Science, Class of 2010

Last edited by neilsonster : 15-12-2006 at 18:57.
  #9   Spotlight this post!  
Unread 15-12-2006, 19:18
teh_pwnerer795 teh_pwnerer795 is offline
Registered User
AKA: !!Mitch!!
FRC #1392
Team Role: Programmer
 
Join Date: Nov 2006
Rookie Year: 2006
Location: Ajax, Canada
Posts: 142
teh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really nice
Send a message via MSN to teh_pwnerer795 Send a message via Yahoo to teh_pwnerer795
Re: Rounding numbers

Quote:
Originally Posted by neilsonster View Post
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....
__________________
Great Art is Created; Master Pieces are Stolen

[n00b]Teh_Foxdonut teh_pwnerer795 - CounterStrike:Source
  #10   Spotlight this post!  
Unread 15-12-2006, 19:48
neilsonster's Avatar
neilsonster neilsonster is offline
Registered User
AKA: Andrew Neilson
FRC #0772 (Sabre Bytes)
Team Role: College Student
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Waterloo, ON, Canada
Posts: 188
neilsonster is a name known to allneilsonster is a name known to allneilsonster is a name known to allneilsonster is a name known to allneilsonster is a name known to allneilsonster is a name known to all
Send a message via MSN to neilsonster
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.
__________________
Andrew Neilson - Team 772 Alumnus
University of Waterloo Computer Science, Class of 2010
  #11   Spotlight this post!  
Unread 15-12-2006, 19:57
teh_pwnerer795 teh_pwnerer795 is offline
Registered User
AKA: !!Mitch!!
FRC #1392
Team Role: Programmer
 
Join Date: Nov 2006
Rookie Year: 2006
Location: Ajax, Canada
Posts: 142
teh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really nice
Send a message via MSN to teh_pwnerer795 Send a message via Yahoo to teh_pwnerer795
Re: Rounding numbers

Quote:
Originally Posted by neilsonster View Post
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
__________________
Great Art is Created; Master Pieces are Stolen

[n00b]Teh_Foxdonut teh_pwnerer795 - CounterStrike:Source
  #12   Spotlight this post!  
Unread 15-12-2006, 23:23
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Rounding numbers

Quote:
Originally Posted by teh_pwnerer795 View Post
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.
  #13   Spotlight this post!  
Unread 16-12-2006, 10:24
teh_pwnerer795 teh_pwnerer795 is offline
Registered User
AKA: !!Mitch!!
FRC #1392
Team Role: Programmer
 
Join Date: Nov 2006
Rookie Year: 2006
Location: Ajax, Canada
Posts: 142
teh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really nice
Send a message via MSN to teh_pwnerer795 Send a message via Yahoo to teh_pwnerer795
Re: Rounding numbers

Quote:
Originally Posted by Alan Anderson View Post
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
__________________
Great Art is Created; Master Pieces are Stolen

[n00b]Teh_Foxdonut teh_pwnerer795 - CounterStrike:Source
  #14   Spotlight this post!  
Unread 17-12-2006, 12:16
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
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.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Negative numbers? Calvin Programming 6 09-02-2005 10:46
Part Numbers DDRAngelKurumi Kit & Additional Hardware 1 29-01-2005 18:38
Random Numbers Astronouth7303 Programming 9 18-01-2004 21:39
Team Numbers archiver 2000 2 24-06-2002 00:24
Numbers at VCU Madison Regional Competitions 5 10-03-2002 02:26


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

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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