float to demoted integer conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dipanmodi
    New Member
    • Apr 2010
    • 1

    float to demoted integer conversion

    i want to make program of : Read float, and convert this in to demoted integer without using int.
    It is by using the reverse the IEEE float format.
    If you have any sollution please give me.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    So you want to explicitly decode the bit pattern of a floating point number. You need to know how floating point numbers are encoded for your particular compiler. You need to know the endianness of your compiler. These two things can vary from one compiler to another, so nobody can offer specific assistance until you make these determinations.

    I wouldn't recommend doing this. I assume it is an assignment.

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      I'm confused...

      You want to take a float, convert it to an int, then put that int into a float?

      So essentially you want to concatenate the floating point part of the float and just take the whole number? Or do you want the floor of the float?

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        @RedSon ... I believe he wants to write a floating point value into a union that overlays an integral array on top of the floating point type. Then he wants to parse the implementation-dependent encoding of that floating point value in the array in order to explicitly extract the integer portion of the value.

        Nobody would do it this way in the real world (unless they were writing a compiler), so this must be a homework assignment to teach floating point encoding to the students.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Ahh I see,

          So here:

          Take the floor of the float, that becomes your integral part then you subtract the floor from the float and that becomes your floating point part. If this is an assignment then you don't need to worry about speed (or really accuracy much). If your professor is asking you to do this at the bit level then we are going to need more information.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Function modf() would be a better choice than floor() and subtraction. However, my impression is that he has to do this at the bit level.

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              True, but I'm trying to make it like a step by step thing, really brute force for a person who is learning the language.

              But either way.

              I'm surprised a professor would assign a task like this unless they were using some kind of educational compiler or some simulated platform environment.

              Comment

              Working...