Go to Post Nothing motivates people to join like the promise of a trip to far-away lands of regional/championship competition during school days :) - Joe G. [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 28-01-2005, 20:31
logicalhippo logicalhippo is offline
Registered User
AKA: Russell McClellan
#0213 (Dirty Birds)
Team Role: Programmer
 
Join Date: Jan 2005
Rookie Year: 2003
Location: Keene, NH
Posts: 11
logicalhippo is an unknown quantity at this point
Send a message via AIM to logicalhippo
Unhappy Converting floating point to integers

I'm having a little problem with converting floating point values to integers. if I do something like this:

float f = 245.56;
int i;
i = (int)f;
printf("%d\n", i);

the output will be 0. Can anyone explain this problem? Do I need an explicit conversion? If so, where could I find documentation on it?
  #2   Spotlight this post!  
Unread 28-01-2005, 22:40
colt527 colt527 is offline
Registered User
AKA: Ken Colton
FRC #0527
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Long Island
Posts: 123
colt527 is a splendid one to beholdcolt527 is a splendid one to beholdcolt527 is a splendid one to beholdcolt527 is a splendid one to beholdcolt527 is a splendid one to beholdcolt527 is a splendid one to beholdcolt527 is a splendid one to behold
Send a message via AIM to colt527
Re: Converting floating point to integers

This SHOULD make no difference, but sometimes code is like that. Dont declare a new variable for it. Just do:

float f = 245.56;
printf("%d\n", (int)f);

I've never had a problem doing it that way *shrugs* ... hope it works.

EDIT: actually, i think if you wanted to do it you way, just cast it again inside the printf, like this:

float f = 245.56;
int i;
i = (int)f;
printf("%d\n", (int)i);

I vaguely remember something about printf that if you dont cast inside the function, it does some weird things. Hope one of those works!
__________________
Mentor, Team 527 -- Plainedge Red Dragons
FIRST Volunteer
SUNY Stony Brook Computer Science 2010
kcolton@gmail.com

Last edited by colt527 : 28-01-2005 at 22:42.
  #3   Spotlight this post!  
Unread 28-01-2005, 22:52
Chriszuma's Avatar
Chriszuma Chriszuma is offline
Jack of all trades
AKA: Chris Hammond
FRC #0068 (Truck Town Thunder)
Team Role: Alumni
 
Join Date: Oct 2004
Rookie Year: 2005
Location: Clarkston, MI
Posts: 290
Chriszuma is just really niceChriszuma is just really niceChriszuma is just really niceChriszuma is just really nice
Send a message via AIM to Chriszuma
Re: Converting floating point to integers

if your code is perfectly correct, there's a pretty good chance that it's just the controller using some sort of bistromath. We've come to the conclusion that it just sucks at computing floating point numbers. You're best off making sure that all numbers are computed in integer form.
__________________
2006 T3 World Tour: Great Lakes - Waterloo - Palmetto - IRI
2006 Awards: Motorola Quality - RadioShack Innovation in Control

My website: http://zuma.phire.org/
Truck Town Thunder's website: http://trucktownthunder.com/
  #4   Spotlight this post!  
Unread 29-01-2005, 12:10
Mark McLeod's Avatar
Mark McLeod Mark McLeod is online now
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,716
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: Converting floating point to integers

Quote:
Originally Posted by logicalhippo
float f = 245.56;
int i;
i = (int)f;
printf("%d\n", i);

the output will be 0.
That code works fine for me. There's something else at work here.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
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
Top 10 Uses for the 10 Point Balls EddieMcD Chit-Chat 10 30-03-2004 18:29
FIRST @ The Point...Who's Going? archiver 2000 0 23-06-2002 23:24
Possible Configuration to keep things cheap archiver 1999 8 23-06-2002 23:03
Is it weird to see an atwood motor floating in mid air? mnkysp6353 Chit-Chat 6 19-02-2002 21:38


All times are GMT -5. The time now is 10:50.

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