Converting a float to a unsigned int?

S

subaruwrx88011

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.
 
V

Victor Bazarov

subaruwrx88011 said:
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
 
O

osmium

subaruwrx88011 said:
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.
 
V

Victor Bazarov

osmium said:
You probably lost it here ^


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
 
K

Kevin Handy

Consider making "1000000000" a float value instead
of an integer (i.e. "1000000000.0"). Can save you
a conversion.

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.
 
O

Old Wolf

subaruwrx88011 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top