View previous topic :: View next topic |
Author |
Message |
Guest
|
Float to integer |
Posted: Wed Feb 02, 2005 5:42 am |
|
|
I have got a float variable of 32 bits,
I am trying to make a casting to integer:
integer_variable = (int) float_variable;
The float variable contains some data, but after the above command, the integer_variable remains zero.
How can I solve this?
Thank you |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Wed Feb 02, 2005 7:18 am |
|
|
Help us out here and tell us what the data is!! If it is between 1 and -1 (negative exponent), the result of the conversion should be 0. Also, what is the expected range of data that you will be trying to convert? |
|
|
Ian McCrum
Joined: 26 Oct 2003 Posts: 14 Location: Northern Ireland
|
|
Posted: Sat Feb 05, 2005 1:25 pm |
|
|
A really inefficient way to convert floats is to use sprintf to convert floats to an ascii string, you can then see what value you have and it may be a more convenient way to store things anyway. You can always use atoi() or atol() to convert the asciii to an int or long.
In your particular case, setup a number of test cases, it may be that casts are not doing what you want.
Cheers _________________ Ian McCrum, email address held at
www.eej.ulst.ac.uk/~ian |
|
|
|