473,732 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Floating point number to binary

Hi,

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?

Thanks
Gaurav
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #1
25 3647
Gaurav Verma writes:
I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?


I don't think it is very clear what you are trying to do. For example, what
do you think the binary representation of a decimal point is? Are you
trying to learn something, as opposed to than actually *doing* something?
If so, what are you trying to learn?
Nov 14 '05 #2
ve**********@gm ail.com (Gaurav Verma) writes:
I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?


If you want the underlying IEEE representation of a floating point
number you can use a union to access the individual bytes.

e.g. union { double d; struct { unsigned char byte[8]; } ieee ; } u;

--
Brian Gough

Network Theory Ltd,
Publishing "An Introduction to GCC" --- http://www.network-theory.co.uk/gcc/

Nov 14 '05 #3
Brian Gough wrote:
ve**********@gm ail.com (Gaurav Verma) writes:
I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help
me out with it?


If you want the underlying IEEE representation of a floating point
number you can use a union to access the individual bytes.

e.g. union {double d; struct { unsigned char byte[8];} ieee ; } u;


No you can't, at least not portably. What you can do is set up a
pointer to the storage and access it via that.

#include <stdio.h>

int main(void)
{
double d;
unsigned char *p;
unsigned int i;

d = 123.4; /* or whatever */
p = (unsigned char *)(&d);
for (i = 0; i < sizeof(double); i++) {
printf("%2x ", p[i]);
/* do other things with p[i] */
}
putchar('\n');
return 0;
}

--
"This is a wonderful answer. It's off-topic, it's incorrect,
and it doesn't answer the question." -- Richard Heathfield

"I support the Red Sox and any team that beats the Yankees"
Nov 14 '05 #4
In <41************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Brian Gough wrote:
ve**********@gm ail.com (Gaurav Verma) writes:
I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help
me out with it?
If you want the underlying IEEE representation of a floating point
number you can use a union to access the individual bytes.

e.g. union {double d; struct { unsigned char byte[8];} ieee ; } u;


No you can't, at least not portably.


Care to elaborate on the portability problems of:

union { double d; unsigned char bytes[sizeof(double)]; } u;

?
What you can do is set up a
pointer to the storage and access it via that.


Both aliasing methods are equally blessed by the C standard. And both
are plagued by byte order issues.

C99's %a printf conversion descriptor is the closest solution to the OP's
problem.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #5
Brian Gough <bj*@network-theory.co.uk> writes:
ve**********@gm ail.com (Gaurav Verma) writes:
I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?


If you want the underlying IEEE representation of a floating point
number you can use a union to access the individual bytes.

e.g. union { double d; struct { unsigned char byte[8]; } ieee ; } u;


There's no need to put the array in a structure:
union { double d; unsigned char ieee[8]; } u;
--
Ben Pfaff
email: bl*@cs.stanford .edu
web: http://benpfaff.org
Nov 14 '05 #6
CBFalconer <cb********@yah oo.com> writes:
Brian Gough wrote:
ve**********@gm ail.com (Gaurav Verma) writes:
I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help
me out with it?


If you want the underlying IEEE representation of a floating point
number you can use a union to access the individual bytes.

e.g. union {double d; struct { unsigned char byte[8];} ieee ; } u;


No you can't, at least not portably. What you can do is set up a
pointer to the storage and access it via that.


Assuming IEEE representation is inherently not portable. I don't
think the above union will make the code less portable.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #7
Dan Pop wrote:
CBFalconer <cb********@yah oo.com> writes:
Brian Gough wrote:
.... snip ...

If you want the underlying IEEE representation of a floating
point number you can use a union to access the individual bytes.

e.g. union {double d; struct {unsigned char byte[8];} ieee ;} u;


No you can't, at least not portably.


Care to elaborate on the portability problems of:

union { double d; unsigned char bytes[sizeof(double)]; } u;


As I understand it accessing a union member as other than the type
that was stored in it is either implementation or undefined.

--
"This is a wonderful answer. It's off-topic, it's incorrect,
and it doesn't answer the question." -- Richard Heathfield

"I support the Red Sox and any team that beats the Yankees"

Nov 14 '05 #8
Brian Gough <bj*@network-theory.co.uk> wrote:
ve**********@gm ail.com (Gaurav Verma) writes:
I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?


If you want the underlying IEEE representation of a floating point
number you can use a union to access the individual bytes.

e.g. union { double d; struct { unsigned char byte[8]; } ieee ; } u;


This is undefined behaviour; to be portable you should go:
unsigned char *ptr = (unsigned char *)&d;
and then access the first (sizeof d) bytes of ptr.

Also note that the size of double is not required to be 8, and it is
not required to use IEEE format (although usually it is).
Nov 14 '05 #9
On Thu, 16 Sep 2004 19:08:59 GMT, CBFalconer <cb********@yah oo.com>
wrote in comp.lang.c:
Dan Pop wrote:
CBFalconer <cb********@yah oo.com> writes:
Brian Gough wrote:
... snip ...
If you want the underlying IEEE representation of a floating
point number you can use a union to access the individual bytes.

e.g. union {double d; struct {unsigned char byte[8];} ieee ;} u;

No you can't, at least not portably.


Care to elaborate on the portability problems of:

union { double d; unsigned char bytes[sizeof(double)]; } u;


As I understand it accessing a union member as other than the type
that was stored in it is either implementation or undefined.


....except for unsigned char, which is the one and only bullet proof,
invulnerable type in C. Since unsigned char is the only type required
not to have trap representations , any object that your program has the
right to access, it may access as an array of unsigned chars without
invoking the ever dreaded UB.

See particularly paragraphs 4 and 5 of 6.2.6.1.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #10

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

Similar topics

4
7850
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a long int to store the value, and the precision (number of decimal points) is variable (it's a templated class): template <size_t _decimal_places = 4> class FixedFloat {
3
1684
by: Ketan Parikh | last post by:
Hey everybody, I am new to this group. I am from India. Consider the following code. main() { float k=0.7; if (k < 0.7)
687
23574
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
3
3796
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that programmers who don't range- check their data can e.g. multiply two very tiny values and end up with a very large one. This is (AFAICT) quite fine according to the Standard but is certainly a QoI issue and I'd like to raise ours. I believe that it's...
10
4184
by: 63q2o4i02 | last post by:
Hi, I'm using python to run some lab equipment using PyVisa. When I read a list of values from the equipment, one of the fields is 32 bits of flags, but the value is returned as a floating point number, either in ASCII format, or pure binary. In either case, since I'm using PyVisa, it converts the number to a single precision floating point, and that's what I have to work with. The question is how do I recover the bits out of this...
9
3521
by: Steven D'Aprano | last post by:
I'm looking for some way to get the next floating point number after any particular float. (Any mathematicians out there: I am aware that there is no "next real number". But floats are not real numbers, they only have a finite precision.) According to the IEEE standard, there should be a routine like next(x,y) which returns the next float starting from x in the direction of y.
8
6234
by: vendredi5h | last post by:
Hello all, I'd like to split a 64-bits word (hold as a floating point number) into two 32-bits words (hold as integers). Bitwise operators are working on Integers, not on floating point. Anyone can tell me how I can do this? Yannick
33
2769
by: dis_is_eagle | last post by:
hi....i have encountered strange problem regarding floating point comparison...the problem is... main() { float a=0.7; if(0.7 a) printf("hi"); else printf("hello");
15
8060
by: Mukesh_Singh_Nick | last post by:
Why does floating point have a rounding error? How to work around it? For example, the following: flaot f = 1234.12345678F; printf("%2f\n", f) //prints 1234.123413 and
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.