floating point values

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

    #1

    floating point values

    how can I take the fractional part of a floating point value
    such that I can store that value into an integer type?

    float foo = 6.180;

    now take the fractional part
    such that you can store 180 into an object
    of type int or any other integer type.

    I'm looking for way not involving the use of
    float -> string -> parse string -> get 180 -> convert to int
    but a process involving
    float -> int

  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: floating point values

    aegis <aegis@mad.scie ntist.com> wrote:[color=blue]
    > how can I take the fractional part of a floating point value
    > such that I can store that value into an integer type?[/color]
    [color=blue]
    > float foo = 6.180;[/color]
    [color=blue]
    > now take the fractional part
    > such that you can store 180 into an object
    > of type int or any other integer type.[/color]
    [color=blue]
    > I'm looking for way not involving the use of
    > float -> string -> parse string -> get 180 -> convert to int
    > but a process involving
    > float -> int[/color]

    Use modf() to get the fraction, multiply by 1000 and round to the
    nearest integer. Homework?
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • Andrey Tarasevich

      #3
      Re: floating point values

      aegis wrote:[color=blue]
      > how can I take the fractional part of a floating point value
      > such that I can store that value into an integer type?
      >
      > float foo = 6.180;
      >
      > now take the fractional part
      > such that you can store 180 into an object
      > of type int or any other integer type.[/color]

      Sorry, but that doesn't exactly make sense. As a number, '6.180' is not
      different from '6.18', '6.18000', '6.1800' etc. You are saying that
      fractional part of value '6.180' is '0.180', right? But why not '0.1800'
      or '0.180000000'?

      You need to explain more clearly what is it you are trying to do.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Mike Wahler

        #4
        Re: floating point values


        "aegis" <aegis@mad.scie ntist.com> wrote in message
        news:1102987316 .325101.204140@ c13g2000cwb.goo glegroups.com.. .[color=blue]
        > how can I take the fractional part of a floating point value
        > such that I can store that value into an integer type?[/color]

        The fractional portion of a floating-point value will always
        be less than 1, so when converted to integer, it will always
        be zero.
        [color=blue]
        >
        > float foo = 6.180;
        >
        > now take the fractional part
        > such that you can store 180 into an object
        > of type int or any other integer type.[/color]

        This requires *changing* this value (via multiplication)
        from .180 to 180. IOW this does not involve a 'conversion'
        but a modification.
        [color=blue]
        >
        > I'm looking for way not involving the use of
        > float -> string -> parse string -> get 180 -> convert to int
        > but a process involving
        > float -> int[/color]

        Then you'll be subject to the inherent inaccuracy of binary
        floating point, and must allow for and correct for it.

        #include <math.h>
        #include <stdio.h>

        int main()
        {
        float foo = 6.180f;
        double ipart = 0;
        double frac = modf(foo, &ipart);
        int ifrac = 0;
        char s[30] = {0};
        float adj = 0.005f; /* for rounding */

        printf("floatin g point value: %.3f\n"
        "integer portion: %.3f\n"
        "fractional portion: %.3f\n",
        foo, ipart, frac);

        ifrac = (int)(frac * 1000 + adj);
        printf("adjuste d result: %d\n", ifrac);

        return 0;
        }

        Output:

        floating point value: 6.18
        integer portion: 6.000
        fractional portion: 0.180
        adjusted result: 180

        -Mike



        Comment

        • Peter Nilsson

          #5
          Re: floating point values

          Mike Wahler wrote:[color=blue]
          >
          > #include <math.h>
          > #include <stdio.h>
          >
          > int main()
          > {
          > float foo = 6.180f;
          > double ipart = 0;
          > double frac = modf(foo, &ipart);
          > int ifrac = 0;
          > char s[30] = {0};[/color]

          round.c:10: warning: unused variable `s' ;)
          [color=blue]
          > float adj = 0.005f; /* for rounding */[/color]

          ITYM 0.5f
          [color=blue]
          >
          > printf("floatin g point value: %.3f\n"
          > "integer portion: %.3f\n"
          > "fractional portion: %.3f\n",
          > foo, ipart, frac);
          >
          > ifrac = (int)(frac * 1000 + adj);[/color]

          This is somewhat simplistic, i.e. it may not round as expected if foo
          (and thus possibly frac) is negative.
          [color=blue]
          > printf("adjuste d result: %d\n", ifrac);
          >
          > return 0;
          > }[/color]

          --
          Peter

          Comment

          • Mike Wahler

            #6
            Re: floating point values


            "Peter Nilsson" <airia@acay.com .au> wrote in message
            news:1103000800 .045627.112610@ c13g2000cwb.goo glegroups.com.. .[color=blue]
            > Mike Wahler wrote:[color=green]
            > >
            > > #include <math.h>
            > > #include <stdio.h>
            > >
            > > int main()
            > > {
            > > float foo = 6.180f;
            > > double ipart = 0;
            > > double frac = modf(foo, &ipart);
            > > int ifrac = 0;
            > > char s[30] = {0};[/color]
            >
            > round.c:10: warning: unused variable `s' ;)[/color]

            I meant to take that out. :-)
            [color=blue]
            >[color=green]
            > > float adj = 0.005f; /* for rounding */[/color]
            >
            > ITYM 0.5f[/color]

            No. We're rounding to nearest thousand, not one.
            OF course this has the 'fragility' that it doesn't
            handle other numbers of wanted digits after the
            decimal point. But that's OP's problem. :-)
            [color=blue]
            >[color=green]
            > >
            > > printf("floatin g point value: %.3f\n"
            > > "integer portion: %.3f\n"
            > > "fractional portion: %.3f\n",
            > > foo, ipart, frac);
            > >
            > > ifrac = (int)(frac * 1000 + adj);[/color]
            >
            > This is somewhat simplistic, i.e. it may not round as expected if foo
            > (and thus possibly frac) is negative.[/color]

            Yes, it is simplistic. Had not the OP specifically disallowed it,
            I would have advised creating a formatted string, parsing that
            for the '.', and converting the desired subsequent characters
            to type 'int'.
            [color=blue]
            >[color=green]
            > > printf("adjuste d result: %d\n", ifrac);
            > >
            > > return 0;
            > > }[/color][/color]

            -Mike


            Comment

            • Lawrence Kirby

              #7
              Re: floating point values

              On Tue, 14 Dec 2004 20:35:23 +0000, Mike Wahler wrote:

              ....
              [color=blue][color=green][color=darkred]
              >> > float adj = 0.005f; /* for rounding */[/color]
              >>
              >> ITYM 0.5f[/color]
              >
              > No. We're rounding to nearest thousand, not one.[/color]

              You round after scaling so you would at that point be trying to round
              to the nearest integer. It is also handy that 0.5 is exactly representable
              in binary floating point.

              Lawrence

              Comment

              Working...