|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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! Last edited by colt527 : 28-01-2005 at 22:42. |
|
#3
|
||||
|
||||
|
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.
|
|
#4
|
|||||
|
|||||
|
Re: Converting floating point to integers
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |