470,862 Members | 1,734 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

Converting a float to a unsigned int?

Is there anyway to convert a float to an unsigned int without loss of
precision?

Here is what I have....

float giga_hertz;
unisigned int hertz;

giga_hertz = 4.2;
hertz = (unsigned int) (1000000000 * giga_hertz);

I can't get hertz to equal 4,200,000,000.

May 11 '06 #1
6 17148
subaruwrx88011 wrote:
Is there anyway to convert a float to an unsigned int without loss of
precision?

Here is what I have....

float giga_hertz;
unisigned int hertz;

giga_hertz = 4.2;
hertz = (unsigned int) (1000000000 * giga_hertz);

I can't get hertz to equal 4,200,000,000.


I presume your unsigned int is, or more than, 32 bits. If that's so,
do

hertz = (unsigned int) (1000000000.5 * giga_hertz);

If that's not so, you will never get 4,200,000,000. You need 32 bits.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 11 '06 #2
"subaruwrx88011" writes:
Is there anyway to convert a float to an unsigned int without loss of
precision?

Here is what I have....

float giga_hertz;
unisigned int hertz;

giga_hertz = 4.2;
You probably lost it here ^
hertz = (unsigned int) (1000000000 * giga_hertz);

I can't get hertz to equal 4,200,000,000.


A better question would be "Can I convert an integer to a float without loss
if precision?" The answer is no. Floats and doubles are approximations for
the underlying real numbers.
May 11 '06 #3
subaruwrx88011 wrote:
Is there anyway to convert a float to an unsigned int without loss of
precision?

Here is what I have....

float giga_hertz;
unisigned int hertz;

giga_hertz = 4.2;
hertz = (unsigned int) (1000000000 * giga_hertz);

I can't get hertz to equal 4,200,000,000.


Probably because floating point representation is inexact (in C++ or
any language). See this FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-29.16

Cheers! --M

May 11 '06 #4
osmium wrote:
"subaruwrx88011" writes:
Is there anyway to convert a float to an unsigned int without loss of
precision?

Here is what I have....

float giga_hertz;
unisigned int hertz;

giga_hertz = 4.2;


You probably lost it here ^
hertz = (unsigned int) (1000000000 * giga_hertz);

I can't get hertz to equal 4,200,000,000.


A better question would be "Can I convert an integer to a float
without loss if precision?" The answer is no. Floats and doubles
are approximations for the underlying real numbers.


Actually, that's not necessarily true. If the number of significant
bits in the integer is fewer than (or the same as) that of the mantissa
of the FP number, then the integer is represented exactly, IIRC.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 11 '06 #5
Victor Bazarov wrote:
subaruwrx88011 wrote:
Is there anyway to convert a float to an unsigned int without loss of
precision?

Here is what I have....

float giga_hertz;
unisigned int hertz;

giga_hertz = 4.2;
hertz = (unsigned int) (1000000000 * giga_hertz);
Consider making "1000000000" a float value instead
of an integer (i.e. "1000000000.0"). Can save you
a conversion.

I can't get hertz to equal 4,200,000,000.

Welcome to the wonderful world of computer floating point.
There are many books and articles on computer floating
point gotcha's that you need to look into.

You can start with:

http://en.wikipedia.org/wiki/Floating_point
http://docs.sun.com/source/806-3568/ncg_goldberg.html

I presume your unsigned int is, or more than, 32 bits. If that's so,
do

hertz = (unsigned int) (1000000000.5 * giga_hertz);
GAAA! That is so "not going to work right"!
Not even for the values given.

Assuming positive values of "giga_hertz", you might try
"(unsigned int) (1000000000.0 * giga_hertz + 0.5)" to round
up. If negative values are allowed, or the number of decimals
in "giga_hertz" gets large, then it gets more difficult.

If that's not so, you will never get 4,200,000,000. You need 32 bits.


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
May 11 '06 #6
subaruwrx88011 wrote:
Is there anyway to convert a float to an unsigned int without loss of
precision?

float giga_hertz;
unisigned int hertz;

giga_hertz = 4.2;
hertz = (unsigned int) (1000000000 * giga_hertz);

I can't get hertz to equal 4,200,000,000.


The problem is that you cannot have a float containing a
value of exactly 4.2 . When you assign 4.2 to a float then
it actually ends up with the closest possible value to 4.2
that floats can store (which might be something like
4.1999942203).

So the question of whether you can convert this to an
unsigned int is moot.

May 12 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by avsrk | last post: by
116 posts views Thread by Dilip | last post: by
11 posts views Thread by hamishd | last post: by
9 posts views Thread by mathieu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.