473,327 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 17763
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
8
by: avsrk | last post by:
Hello Folks , General C data types question , more geared up towards embedded folks . I have a positive float quantity with a fractional part (one decimal point) which occupies 4 bytes ....
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
11
by: hamishd | last post by:
Is this possible? Sorry if this question isn't relevant here. actually, I'm really trying to convert a unsigned char * to an int
9
by: mathieu | last post by:
Hi, I know I am doing something stupid here, but it's friday night and I cannot see what is the issue here: Thanks, -Mathieu #include <iostream>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.