View previous topic :: View next topic |
Author |
Message |
tomasch
Joined: 14 May 2004 Posts: 3
|
How to convert float to long? |
Posted: Wed May 19, 2004 9:26 pm |
|
|
I have a 7 segment LED display routine that requires a long (16bits) data type.
I have to do some fancy calculations that require float. I know how to cast up to float but how do you "cast down"
I have tried the following...
float x=12.34
long y
both y=x and y=(long) x give y a value of 0 (zero)
Any ideas?
Thanks in Advance
Tom |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Thu May 20, 2004 7:21 am |
|
|
What are you expecting "y" to be in your example?
An assignment operation should make y=12 (truncation). If it is coming back as zero there may be a compiler bug so I'll ask the 64 cent question, what version of compiler are you using? _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
Guest
|
|
Posted: Thu May 20, 2004 8:03 am |
|
|
I'm expecting y=12 and it's 0
Version is CCS v 3.112 (I think! - How do I verify) |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu May 20, 2004 8:12 am |
|
|
Select 'Help' on the menu bar and then 'About'. This will tell you which version you have. |
|
|
Guest
|
|
Posted: Thu May 20, 2004 9:33 am |
|
|
Using the PCM version - in looking at some of the files in the directory - it's 3.112 |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Thu May 20, 2004 7:03 pm |
|
|
You probably don't want to hear this but I think it is a compiler bug.
Try being a little more specific with your variable declarations:
Code: |
float x=12.34;
signed long int y; // or "unsigned" instead of "signed", just be consistant with the cast later
y = (signed long int)x;
|
See if that helps. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
|