Floating point conversion

This is a discussion on Floating point conversion within the C Programming forums, part of the General Programming Boards category; Hi guys, I've been looking everywhere to find something about the problem below but can't find anything. As always, you ...

  1. #1
    1ST R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    Floating point conversion

    Hi guys,

    I've been looking everywhere to find something about the problem below but can't find anything. As always, you are my last help

    Here's - in two lines - what I'm trying to do:

    1. The user enters a decimal value (e.g. 2.1)
    2. The conversion function should return the hexadecimal floating point value for this (in this example 40066666).

    The following website:
    http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html
    does this for me, but I need it in C so I can integrate it in one of my programs.

    I know there's a lot of websites that explain the theory of this conversion and I think I should be able to make it myself (not knowing how long it would take me). I'm pretty sure though, that the source code for this conversion should be available somewhere (or maybe it's even a standard function in some header file?) and that could save me a lot of time.
    Therefore my question: does anyone know where I can find a C program (source code) that does the above conversion for me? Please note, this is not lazyness... people seem to have graduated only on doing this conversion.

    Any help would be appreciated. Thanks very much in advance,

    René
    Last edited by rkooij; 08-28-2006 at 04:01 AM.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  2. #2
    1ST R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    PS I had found something interesting but in that example <decimal.h> is used. Again, I've been searching everywhere for it, does anyone know where I could find that header file?
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > PS I had found something interesting but in that example <decimal.h> is used
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Is this cheating?
    Code:
    union {
      unsigned long i;
      float f;
    } v;
    v.f = 2.1;
    printf("%lx\n", v.i );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    1ST R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by Salem
    > PS I had found something interesting but in that example <decimal.h> is used
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Thanks for that - although it's not very helpful :P, I didn't see it before indeed.

    Quote Originally Posted by Salem
    Is this cheating?
    Maybe it is, but... do you really think I care?

    I LOVE YOU

    Thanks!!
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Maybe it is, but... do you really think I care?
    I couldn't help but laugh at this.
    My best code is written with the delete key.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Way to go Salem. Helping people cheat.

    Now the poor OP will go turn that in and fail the class for using something he doesn't even know works or why it works.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Sometimes, it's just fun to post a mousetrap to see what happens
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This particular mousetrap is waiting to snap shut on the first one to try to compile this in 64-bit Linux.

    Evil, Salem, evil.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    1ST R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by CornedBee
    This particular mousetrap is waiting to snap shut on the first one to try to compile this in 64-bit Linux.
    Lol, if that's the problem then there isn't a problem. I'm 99% sure I will never compile in 64-bit Linux.
    Anyway, yes I agree I have no idea why / how this works, because I've never worked with union and my theoretical knowledge is nigh to zero. I don't like using something when I don't know how it works, but in this case, I had been looking everywhere to find a solution. When a solution this simple was offered I couldn't resist. As soon as I have time, I will try to find out why/how this works.
    And btw, this is not some college assignment, it's something that has practical use for my company
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  10. #10
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    This is remarkable way to solve this as Salem did. I admit I couldn't think something like this.
    The short and sweet introduction to union is:
    http://gd.tuwien.ac.at/languages/c/p...rown/c_099.htm

    Cheers
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  2. Floating point #'s, why so much talk about it?
    By scuzzo84 in forum C Programming
    Replies: 5
    Last Post: 09-20-2005, 04:29 PM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21