floating point values

Discussion in 'C Programming' started by aegis, Dec 14, 2004.

  1. aegis

    aegis Guest

    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
    aegis, Dec 14, 2004
    #1
    1. Advertising

  2. aegis

    -berlin.de Guest

    aegis <> wrote:
    > 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


    Use modf() to get the fraction, multiply by 1000 and round to the
    nearest integer. Homework?
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Dec 14, 2004
    #2
    1. Advertising

  3. aegis

    Mike Wahler Guest

    "aegis" <> wrote in message
    news:...
    > how can I take the fractional part of a floating point value
    > such that I can store that value into an integer type?


    The fractional portion of a floating-point value will always
    be less than 1, so when converted to integer, it will always
    be zero.

    >
    > 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.


    This requires *changing* this value (via multiplication)
    from .180 to 180. IOW this does not involve a 'conversion'
    but a modification.

    >
    > 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


    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("floating point value: %.3f\n"
    "integer portion: %.3f\n"
    "fractional portion: %.3f\n",
    foo, ipart, frac);

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

    return 0;
    }

    Output:

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

    -Mike
    Mike Wahler, Dec 14, 2004
    #3
  4. aegis wrote:
    > 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.


    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
    Andrey Tarasevich, Dec 14, 2004
    #4
  5. Mike Wahler wrote:
    >
    > #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};


    round.c:10: warning: unused variable `s' ;)

    > float adj = 0.005f; /* for rounding */


    ITYM 0.5f

    >
    > printf("floating point value: %.3f\n"
    > "integer portion: %.3f\n"
    > "fractional portion: %.3f\n",
    > foo, ipart, frac);
    >
    > ifrac = (int)(frac * 1000 + adj);


    This is somewhat simplistic, i.e. it may not round as expected if foo
    (and thus possibly frac) is negative.

    > printf("adjusted result: %d\n", ifrac);
    >
    > return 0;
    > }


    --
    Peter
    Peter Nilsson, Dec 14, 2004
    #5
  6. aegis

    Mike Wahler Guest

    "Peter Nilsson" <> wrote in message
    news:...
    > Mike Wahler wrote:
    > >
    > > #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};

    >
    > round.c:10: warning: unused variable `s' ;)


    I meant to take that out. :)

    >
    > > float adj = 0.005f; /* for rounding */

    >
    > ITYM 0.5f


    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. :)

    >
    > >
    > > printf("floating point value: %.3f\n"
    > > "integer portion: %.3f\n"
    > > "fractional portion: %.3f\n",
    > > foo, ipart, frac);
    > >
    > > ifrac = (int)(frac * 1000 + adj);

    >
    > This is somewhat simplistic, i.e. it may not round as expected if foo
    > (and thus possibly frac) is negative.


    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'.

    >
    > > printf("adjusted result: %d\n", ifrac);
    > >
    > > return 0;
    > > }


    -Mike
    Mike Wahler, Dec 14, 2004
    #6
  7. On Tue, 14 Dec 2004 20:35:23 +0000, Mike Wahler wrote:

    ....

    >> > float adj = 0.005f; /* for rounding */

    >>
    >> ITYM 0.5f

    >
    > No. We're rounding to nearest thousand, not one.


    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
    Lawrence Kirby, Dec 15, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,607
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,456
  3. Replies:
    4
    Views:
    1,270
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,285
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    239
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page