int, float

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Link

    int, float

    I have to convert an float number to int without using casting i= (int) f,
    without printf and others. I can only use int, char and structs. Can anyone
    help?

    Thanks
    Juerg


  • David Harmon

    #2
    Re: int, float

    On Tue, 10 Feb 2004 17:07:56 +0100 in comp.lang.c++, "Link"
    <juerg.link@lil asystem.ch> was alleged to have written:[color=blue]
    >I have to convert an float number to int without using casting i= (int) f,
    >without printf and others. I can only use int, char and structs.[/color]

    What is it about the standard built-in conversion from float to int
    (what you call "casting") that makes it unsuitable for your application?
    Anything else is doing it the hard way.

    Comment

    • osmium

      #3
      Re: int, float

      Link writes:
      [color=blue]
      > I have to convert an float number to int without using casting i= (int) f,
      > without printf and others. I can only use int, char and structs. Can[/color]
      anyone[color=blue]
      > help?[/color]

      I assume this is a student assignment. The first step is to find out how
      your platform encodes a float. Look at the documentation that came with
      your compiler. It is almost certain that an int is encoded as a two's
      complement binary number and it's size will be in <limits.h>. Are you sure
      that unions are forbidden? They might turn out to be handy, too.

      Sounds like a fun and informative exercise.


      Comment

      • Link

        #4
        Re: int, float

        Yes, I have it to do in the hard way - Its only a little box (not pc) and so
        I can't use all libs (reduced memory, ...)
        You know someting about working with exp, mantissa and so, only with int and
        struct?

        "David Harmon" <source@netcom. com> schrieb im Newsbeitrag
        news:404f042b.7 6657078@news.we st.earthlink.ne t...[color=blue]
        > On Tue, 10 Feb 2004 17:07:56 +0100 in comp.lang.c++, "Link"
        > <juerg.link@lil asystem.ch> was alleged to have written:[color=green]
        > >I have to convert an float number to int without using casting i= (int)[/color][/color]
        f,[color=blue][color=green]
        > >without printf and others. I can only use int, char and structs.[/color]
        >
        > What is it about the standard built-in conversion from float to int
        > (what you call "casting") that makes it unsuitable for your application?
        > Anything else is doing it the hard way.
        >[/color]


        Comment

        • Link

          #5
          Re: int, float

          Its a work on a little box with not much memory and I'm using borland turbo
          compiler (TC)

          typedef struct _MF {
          unsigned long m : 23;
          unsigned int e : 8;
          unsigned int s : 1;
          } MF;

          union are ok.

          You know more about working with mantissa, exponent and so?

          "osmium" <r124c4u102@com cast.net> schrieb im Newsbeitrag
          news:c0b12m$15f 9kk$1@ID-179017.news.uni-berlin.de...[color=blue]
          > Link writes:
          >[color=green]
          > > I have to convert an float number to int without using casting i= (int)[/color][/color]
          f,[color=blue][color=green]
          > > without printf and others. I can only use int, char and structs. Can[/color]
          > anyone[color=green]
          > > help?[/color]
          >
          > I assume this is a student assignment. The first step is to find out how
          > your platform encodes a float. Look at the documentation that came with
          > your compiler. It is almost certain that an int is encoded as a two's
          > complement binary number and it's size will be in <limits.h>. Are you[/color]
          sure[color=blue]
          > that unions are forbidden? They might turn out to be handy, too.
          >
          > Sounds like a fun and informative exercise.
          >
          >[/color]


          Comment

          • Victor Bazarov

            #6
            Re: int, float

            "Link" <juerg.link@lil asystem.ch> wrote...[color=blue]
            > I have to convert an float number to int without using casting i= (int) f,
            > without printf and others. I can only use int, char and structs. Can[/color]
            anyone[color=blue]
            > help?[/color]

            What is it, a well-camouflaged homework? Most processors that
            can work with floating point numbers directly can load them into
            their register as FP and store as an int. Two instructions, no
            printf necessary. If you are so bound to a particular "box" as
            you claim, use assembly language. Or does your processor lack
            any support for floating-point numbers?


            Comment

            • Gianni Mariani

              #7
              Re: int, float

              Link wrote:[color=blue]
              > Its a work on a little box with not much memory and I'm using borland turbo
              > compiler (TC)
              >
              > typedef struct _MF {
              > unsigned long m : 23;
              > unsigned int e : 8;
              > unsigned int s : 1;
              > } MF;
              >
              > union are ok.
              >
              > You know more about working with mantissa, exponent and so?[/color]

              Well, you've virtually answered your question -

              given that the floating point number is:

              (s ? -1: 1 ) x m x 2^(e)

              I think you can figure it out - however, be careful with 'e'. Is 'e'
              really unsigned ? You also need to know what "m" is - is m really
              normalized - meaning is the expression really :

              (s ? -1: 1 ) x m x 2^(e-23)

              Can you figure it out from there ?

              Comment

              • David Harmon

                #8
                Re: int, float

                On Tue, 10 Feb 2004 17:39:16 +0100 in comp.lang.c++, "Link"
                <juerg.link@lil asystem.ch> was alleged to have written:[color=blue]
                >Its a work on a little box with not much memory and I'm using borland turbo
                >compiler (TC)[/color]

                Use the Borland assembly source out (the -S option if I recall) and
                look at what it generates for the cast. Then consider whether you still
                think you can do better. If you can, the assembly source will probably
                give some clue as to which way to go.

                As for comp.lang.c++, we do try to keep to standard portable stuff
                here, so twiddling mantissas and such will not be too popular.
                See the welcome message posted twice per week in comp.lang.c++ or
                available at http://www.slack.net/~shiva/welcome.txt

                Comment

                • Ron Natalie

                  #9
                  Re: int, float


                  "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:E58Wb.2718 79$xy6.1382070@ attbi_s02...[color=blue]
                  > "Link" <juerg.link@lil asystem.ch> wrote...[color=green]
                  > > I have to convert an float number to int without using casting i= (int) f,
                  > > without printf and others. I can only use int, char and structs. Can[/color]
                  > anyone[color=green]
                  > > help?[/color]
                  >
                  > What is it, a well-camouflaged homework? Most processors that
                  > can work with floating point numbers directly can load them into
                  > their register as FP and store as an int.[/color]

                  Too bad that doesn't include pentiums.

                  Comment

                  • Andy Buchanan

                    #10
                    Re: int, float

                    On Tue, 10 Feb 2004 14:21:37 -0500, "Ron Natalie" <ron@sensor.com >
                    wrote:
                    [color=blue]
                    >
                    >"Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:E58Wb.2718 79$xy6.1382070@ attbi_s02...[color=green]
                    >> "Link" <juerg.link@lil asystem.ch> wrote...[color=darkred]
                    >> > I have to convert an float number to int without using casting i= (int) f,
                    >> > without printf and others. I can only use int, char and structs. Can[/color]
                    >> anyone[color=darkred]
                    >> > help?[/color]
                    >>
                    >> What is it, a well-camouflaged homework? Most processors that
                    >> can work with floating point numbers directly can load them into
                    >> their register as FP and store as an int.[/color]
                    >
                    >Too bad that doesn't include pentiums.[/color]

                    It doesn't? FILD/FIST don't work? Bit on the slow side, but they
                    always worked for me.....

                    Comment

                    • Ron Natalie

                      #11
                      Re: int, float


                      "Andy Buchanan" <news@globbits. co.uk> wrote in message news:lqui209e3j 184m7bkllsbn9o9 q4e2hhuiu@4ax.c om...[color=blue][color=green][color=darkred]
                      >> >> What is it, a well-camouflaged homework? Most processors that
                      > >> can work with floating point numbers directly can load them into
                      > >> their register as FP and store as an int.[/color]
                      > >
                      > >Too bad that doesn't include pentiums.[/color]
                      >
                      > It doesn't? FILD/FIST don't work? Bit on the slow side, but they
                      > always worked for me.....
                      >[/color]

                      FIST isn't slow. However, FIST won't in most cases convert between
                      float and int as the processor is almost certainly in round mode. Therefore
                      you have two options:

                      1. Modify the fcw to change the state to truncation, do the fist, then put it
                      back. This is EGREGIOUSLY SLOW.
                      2. Subtract .5 from the value before doing the fist. (This is actually pretty fast
                      but I don't know ANY compiler that does this).


                      Comment

                      • Martin Eisenberg

                        #12
                        Re: int, float

                        Ron Natalie wrote:
                        [color=blue]
                        > FIST isn't slow. However, FIST won't in most cases convert
                        > between float and int as the processor is almost certainly in
                        > round mode. Therefore you have two options:
                        >
                        > 1. Modify the fcw to change the state to truncation, do the
                        > fist, then put it back. This is EGREGIOUSLY SLOW.
                        > 2. Subtract .5 from the value before doing the fist. (This is
                        > actually pretty fast but I don't know ANY compiler that does this).[/color]

                        The x87 round-to-nearest mode chooses the even value in case of a
                        tie, so 3.0 is "truncated" as 3.0 - 0.5 = 2.5 -> 2; probably not the
                        intended result.


                        Martin

                        --
                        Quidquid latine dictum sit, altum viditur.

                        Comment

                        • Ron Natalie

                          #13
                          Re: int, float


                          "Martin Eisenberg" <martin.eisenbe rgNOS@PAMudo.ed u> wrote in message news:1076591967 .725272@ostenbe rg.wh.uni-[color=blue][color=green]
                          > >
                          > > 1. Modify the fcw to change the state to truncation, do the
                          > > fist, then put it back. This is EGREGIOUSLY SLOW.
                          > > 2. Subtract .5 from the value before doing the fist. (This is
                          > > actually pretty fast but I don't know ANY compiler that does this).[/color]
                          >
                          > The x87 round-to-nearest mode chooses the even value in case of a
                          > tie, so 3.0 is "truncated" as 3.0 - 0.5 = 2.5 -> 2; probably not the
                          > intended result.
                          >[/color]
                          Prescott by the way has a new FISTTP instructions that always truncates
                          (regardless of the FCW). I guess enough C programmers finally screamed.

                          Comment

                          • Andy Buchanan

                            #14
                            Re: int, float

                            On Wed, 11 Feb 2004 12:12:07 -0500, "Ron Natalie" <ron@sensor.com >
                            wrote:
                            [color=blue]
                            >
                            >"Andy Buchanan" <news@globbits. co.uk> wrote in message news:lqui209e3j 184m7bkllsbn9o9 q4e2hhuiu@4ax.c om...[color=green][color=darkred]
                            >>> >> What is it, a well-camouflaged homework? Most processors that
                            >> >> can work with floating point numbers directly can load them into
                            >> >> their register as FP and store as an int.
                            >> >
                            >> >Too bad that doesn't include pentiums.[/color]
                            >>
                            >> It doesn't? FILD/FIST don't work? Bit on the slow side, but they
                            >> always worked for me.....
                            >>[/color]
                            >
                            >FIST isn't slow.[/color]

                            If I'm doing thousands of them it starts to add up :-)
                            IIRC it's 6 cycles or so, that hurts when I've managed
                            to finese away just about every expensive calculation
                            in an inner loop. It's all relative I suppose.
                            [color=blue]
                            > However, FIST won't in most cases convert between
                            >float and int as the processor is almost certainly in round mode. Therefore
                            >you have two options:
                            >1. Modify the fcw to change the state to truncation, do the fist, then put it
                            > back. This is EGREGIOUSLY SLOW.
                            >2. Subtract .5 from the value before doing the fist. (This is actually pretty fast
                            > but I don't know ANY compiler that does this).[/color]

                            Well okay, you're right of course, but generally when I've had to hit
                            the assembler for performance reasons I've usually been quite happy to
                            accept this single bit error.

                            Comment

                            Working...