473,396 Members | 2,109 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,396 software developers and data experts.

problems turning int into float (simple problem)

Hi all!

I have an int that comes with a value like 0x07fa and I have to turn it into
a float value of 204.2 decimal to display it.... if I try to divide it by
10 I get bogus numbers. I presume I have to make it decimal before
calculating, but I am not sure about it. Any light upon this simple (or it
seems to me so, at least) begginner problem?

TIA..

Yodai
Nov 13 '05 #1
5 4529
"Yodai" <yo***@spamnot.mail.vu> wrote in message
news:xG*********************@telenews.teleline.es. ..
Hi all!

I have an int that comes with a value like 0x07fa and I have to turn it into a float value
of 204.2 decimal to display it....
0x07FA is a valid representation of a value of
an integer type, if that integer type's size is
large enough. It might or might not be a valid
representation of a value of a floating point type.
This depends upon the floating point representation
used by your implementation. For one system, it
might mean e.g. 204.2, on others it might mean
something else entirely, or not a valid value at all.

IMO the only reasonable way to "convert" 0x07FA into
204.2 is to write e.g:

float cvt(int i)
{
return i == 0x7FA ? 204.2 : i;
} /* (returns origninal value if it was not 0x7FA) */

if I try to divide it by
10 I get bogus numbers.
If you divide it by ten, you get a value other than
what you started with. This is *not* a "conversion"
it's an arithmetic operation with a defined result.

You'll get a *single* valid quotient, not "bogus
numbers".

If you divide the integer value 0x07FA by ten, the result
will be 0xCC. (Fractional portion of the quotient
is discarded -- this is how 'integer division' works
in C.) Again, the representation of this value is a
valid integer value, but might or might not be a valid
floating point value. If it is a valid floating point
value for more than one platform, the actual represented
value might or might not be the same for all those platforms.
I presume I have to make it decimal before
calculating,
All values in a C program are represented with
binary. In C source code, integer values can be
expressed in decimal (or octal, or hexadecimal.)
Floating point values are expressed in decimal.
but I am not sure about it. Any light upon this simple (or it
seems to me so, at least) begginner problem?


/* Here's what I believe you're looking for: */
int i = 0x7FA;
float f = i / 10.0f;
printf("%f\n", f); /* prints 204.2 */

Unless using the 'bit pattern' for some special
purpose (which imo only makes sense with an integer type),
one should almost always express values in source code in
decimal, and pay attention to conversion rules while performing arithmetic.

In the above example, the expression:

f = i / 10.0f;

Causes the type 'int' object 'i' to be converted
to a floating point type, then floating point division
is performed, yielding a floating point result of 204.2

had I written:

f = i / 10;

No conversion would be performed before the division,
which would be done as integer division, yielding
a result of 204, which is converted to floating point
value of 204.0

The expression 10 is an integer constant.
10.0f and 10.0 are floating point constants.
The former has type 'float', the latter, type
'double'.

-Mike

Nov 13 '05 #2
"Yodai" <yo***@spamnot.mail.vu> writes:
I have an int that comes with a value like 0x07fa and I have to turn it into
a float value of 204.2 decimal to display it.... if I try to divide it by
10 I get bogus numbers. I presume I have to make it decimal before
calculating, but I am not sure about it. Any light upon this simple (or it
seems to me so, at least) begginner problem?


int value = 0x7fa;

printf ("%f\n", value / 10.0);
or
printf ("%d.%c\n", value / 10, value % 10 + '0');
--
"In My Egotistical Opinion, most people's C programs should be indented six
feet downward and covered with dirt." -- Blair P. Houghton
Nov 13 '05 #3


Yodai wrote:
Hi all!

I have an int that comes with a value like 0x07fa and I have to turn it into
a float value of 204.2 decimal to display it.... if I try to divide it by
10 I get bogus numbers. I presume I have to make it decimal before
calculating, but I am not sure about it. Any light upon this simple (or it
seems to me so, at least) begginner problem?


Presenting the code would help in getting to the bottom of this beginner
problem. To me, it is unclear what you mean when you say you have to
"turn" the int value into a float value of 204.2 decimal. It would
also be helpful on presenting the bogus number results.
The solution may be as simple as using the %.1f conversion specifier
when you display the float.

#include <stdio.h>

int main(void)
{
int value = 0x07fa;
float x = value/204.2f;

printf("divisor value x = %.1f\n",x);
printf("Testing:\n0x07fa / %.1f = %.1f\n",x,0x07fa/x);
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #4
Yodai wrote:

I have an int that comes with a value like 0x07fa and I have to
turn it into a float value of 204.2 decimal to display it....
if I try to divide it by 10 I get bogus numbers. I presume I have
to make it decimal before calculating, but I am not sure about
it. Any light upon this simple (or it seems to me so, at least)
begginner problem?


0x7fa is 6 less than 0x800, so the value is 8 * 256 - 6. To me
this value appears to be 2042.

Assuming this is held in the variable n, you print the value of (n
/ 10), then a '.', then the value of (n % 10). What's the
problem?

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #5
Thanks to all.... I am trying to get this clear on my program.... I'll let
you know what came out..

Cheers!
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Justin Lemkul | last post by:
Hello all, I am hoping someone out there will be able to help me. I am trying to install a program that utilizes NumPy. In installing NumPy, I realized that I was lacking Atlas. I ran into...
3
by: eternalD3 | last post by:
Hi, I have a problem to get this working on Opera 7.x+. This does not need to work on older Opera browsers There are problems on rendering the sub-level navigation. It aligns right on Firefox...
5
by: Patrick De Ridder | last post by:
How can I turn what I want to print 90 degrees using the logic below? Please tell me the code with which to make the modification. Many thanks, Patrick. using System.ComponentModel; using...
1
by: supergrover1981 | last post by:
Gidday gang, I've been teaching myself CSS over the past 2 days and for the most part I thought I had it all working. All the problems I've had have been in IE...until now. If anyone could offer...
9
by: Sheldon | last post by:
Good day Everyone, I am a still very new at learning C and I have thrown myself in the deep end. I started with a simple program and kept widening the scope. This has taught me many things about...
2
by: monkeon | last post by:
Hi, I'm pretty new to C++ I've been doing a course for a few months. I've been trying to design a simple console application that reads in floats from a text file and returns the average,...
0
Screaming Eagle
by: Screaming Eagle | last post by:
Ok, so some associates and I are building a website, and I'm in charge of the design and layout. I've decided to go with a simple navigation bar on top contained in its own div. Under the navigation...
2
by: =?Utf-8?B?RnJhbg==?= | last post by:
sHi! I have a very strange problem with the behaviour of the sizeof command. I have tested it in vc6,vc 2001 and vc 2003. The problem is quite simple. When I made a sizeof to the next struct,...
5
by: SpennH | last post by:
I have had some basic knowledge of java through flash 8. I also use a freeware 3d software called blender. And it uses python for its game engine. So I have decided to pick up some python. This is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.