How to check if a double is near an int?

Discussion in 'C++' started by xz, Apr 23, 2008.

  1. xz

    xz Guest

    In a program, if a double is within 0.0001 from an int, then I treat
    it as int. For example, 10.00005 will be treated as int, while 10.001
    is not.

    I am looking for an algorithm to implement this checking. Anybody
    gives an idea?

    Thanks.
    xz, Apr 23, 2008
    #1
    1. Advertising

  2. xz

    Christopher Guest

    On Apr 23, 4:56 pm, xz <> wrote:
    > In a program, if a double is within 0.0001 from an int, then I treat
    > it as int. For example, 10.00005 will be treated as int, while 10.001
    > is not.
    >
    > I am looking for an algorithm to implement this checking. Anybody
    > gives an idea?
    >
    > Thanks.


    Depends what you mean by "treat it as"

    Surely the math is simple enough to grab to decimal portion of the
    double and compare it to .0001 or any variable amount you wish.
    Getting the quotient and remainder after dividing by 1 comes to mind.
    I doubt I'd even call it an algorithm...
    Christopher, Apr 23, 2008
    #2
    1. Advertising

  3. xz

    xz Guest

    On Apr 23, 5:05 pm, Christopher <> wrote:
    > On Apr 23, 4:56 pm, xz <> wrote:
    >
    > > In a program, if a double is within 0.0001 from an int, then I treat
    > > it as int. For example, 10.00005 will be treated as int, while 10.001
    > > is not.

    >
    > > I am looking for an algorithm to implement this checking. Anybody
    > > gives an idea?

    >
    > > Thanks.

    >
    > Depends what you mean by "treat it as"
    >
    > Surely the math is simple enough to grab to decimal portion of the
    > double and compare it to .0001 or any variable amount you wish.
    > Getting the quotient and remainder after dividing by 1 comes to mind.
    > I doubt I'd even call it an algorithm...


    By "treat it as" I mean I will do different things for the doubles
    near int and for the doubles not near int.

    Your trick (if you don't wanna call it algorithm) does not really work
    for my case because I wanna 9.99999 to be "near int" however its
    decimal portion is 0.99999.
    xz, Apr 23, 2008
    #3
  4. xz

    Christopher Guest

    On Apr 23, 5:15 pm, xz <> wrote:
    > On Apr 23, 5:05 pm, Christopher <> wrote:
    >
    >
    >
    > > On Apr 23, 4:56 pm, xz <> wrote:

    >
    > > > In a program, if a double is within 0.0001 from an int, then I treat
    > > > it as int. For example, 10.00005 will be treated as int, while 10.001
    > > > is not.

    >
    > > > I am looking for an algorithm to implement this checking. Anybody
    > > > gives an idea?

    >
    > > > Thanks.

    >
    > > Depends what you mean by "treat it as"

    >
    > > Surely the math is simple enough to grab to decimal portion of the
    > > double and compare it to .0001 or any variable amount you wish.
    > > Getting the quotient and remainder after dividing by 1 comes to mind.
    > > I doubt I'd even call it an algorithm...

    >
    > By "treat it as" I mean I will do different things for the doubles
    > near int and for the doubles not near int.


    Well, that certainly sheds some light on things. "treat it as" means
    "do different things"....hmmm
    Pretty hard to help you when you won't describe what you are trying to
    do.


    > Your trick (if you don't wanna call it algorithm) does not really work
    > for my case because I wanna 9.99999 to be "near int" however its
    > decimal portion is 0.99999.


    Still the same principle:

    if( remainder - tolerance < 0.0 )
    {
    // Do whatever you mean by treat as int and round down if you wish
    }
    else if( remainder + tolerance > 1.0 )
    {
    // Do whatever you mean by treat as int and round up if you wish
    }
    Christopher, Apr 23, 2008
    #4
  5. Are you looking for a "algorithm" like this then?

    bool isNearInt( double value )
    {
    const double epsilon = 0.0001;
    const double diff = value - floor( value );
    return ( diff <= epsilon || diff >= (1.0 - epsilon) );
    }

    which should at least answer the within epsilon of an integer value.


    xz wrote:
    > On Apr 23, 5:05 pm, Christopher <> wrote:
    >> On Apr 23, 4:56 pm, xz <> wrote:
    >>
    >>> In a program, if a double is within 0.0001 from an int, then I treat
    >>> it as int. For example, 10.00005 will be treated as int, while 10.001
    >>> is not.
    >>> I am looking for an algorithm to implement this checking. Anybody
    >>> gives an idea?
    >>> Thanks.

    >> Depends what you mean by "treat it as"
    >>
    >> Surely the math is simple enough to grab to decimal portion of the
    >> double and compare it to .0001 or any variable amount you wish.
    >> Getting the quotient and remainder after dividing by 1 comes to mind.
    >> I doubt I'd even call it an algorithm...

    >
    > By "treat it as" I mean I will do different things for the doubles
    > near int and for the doubles not near int.
    >
    > Your trick (if you don't wanna call it algorithm) does not really work
    > for my case because I wanna 9.99999 to be "near int" however its
    > decimal portion is 0.99999.
    >
    >
    >
    >
    Thomas Austad, Apr 23, 2008
    #5
  6. xz

    xz Guest

    On Apr 23, 5:35 pm, Christopher <> wrote:
    > On Apr 23, 5:15 pm, xz <> wrote:
    >
    >
    >
    > > On Apr 23, 5:05 pm, Christopher <> wrote:

    >
    > > > On Apr 23, 4:56 pm, xz <> wrote:

    >
    > > > > In a program, if a double is within 0.0001 from an int, then I treat
    > > > > it as int. For example, 10.00005 will be treated as int, while 10.001
    > > > > is not.

    >
    > > > > I am looking for an algorithm to implement this checking. Anybody
    > > > > gives an idea?

    >
    > > > > Thanks.

    >
    > > > Depends what you mean by "treat it as"

    >
    > > > Surely the math is simple enough to grab to decimal portion of the
    > > > double and compare it to .0001 or any variable amount you wish.
    > > > Getting the quotient and remainder after dividing by 1 comes to mind.
    > > > I doubt I'd even call it an algorithm...

    >
    > > By "treat it as" I mean I will do different things for the doubles
    > > near int and for the doubles not near int.

    >
    > Well, that certainly sheds some light on things. "treat it as" means
    > "do different things"....hmmm
    > Pretty hard to help you when you won't describe what you are trying to
    > do.


    Sorry about not having expressed it clearly and thanks for your help.

    >
    > > Your trick (if you don't wanna call it algorithm) does not really work
    > > for my case because I wanna 9.99999 to be "near int" however its
    > > decimal portion is 0.99999.

    >
    > Still the same principle:
    >
    > if( remainder - tolerance < 0.0 )
    > {
    > // Do whatever you mean by treat as int and round down if you wish}
    >
    > else if( remainder + tolerance > 1.0 )
    > {
    > // Do whatever you mean by treat as int and round up if you wish
    >
    > }


    However, this is like a brutal force way that I actually thought
    about.
    I am wondering if there is any smarter way to do that? Like finishing
    the job in a relatively simple expression?
    Still appreciate a lot for your help.
    xz, Apr 23, 2008
    #6
  7. xz

    Guest

    On Apr 23, 5:56 pm, xz <> wrote:
    > In a program, if a double is within 0.0001 from an int, then I treat
    > it as int. For example, 10.00005 will be treated as int, while 10.001
    > is not.
    >
    > I am looking for an algorithm to implement this checking. Anybody
    > gives an idea?
    >
    > Thanks.


    Something like this? but see also http://www.parashift.com/c -faq-lite/newbie.html#faq-29.17

    bool close_to_int (double d, double tolerance) {
    double cdiff = std::fabs (std::ceil (d) - d);
    double fdiff = std::fabs (std::floor (d) - d);
    if (cdiff < fdiff)
    return cdiff <= tolerance;
    return fdiff <= tolerance;
    }
    , Apr 23, 2008
    #7
  8. xz wrote:
    > On Apr 23, 5:05 pm, Christopher <> wrote:
    >> On Apr 23, 4:56 pm, xz <> wrote:
    >>
    >>> In a program, if a double is within 0.0001 from an int, then I treat
    >>> it as int. For example, 10.00005 will be treated as int, while 10.001
    >>> is not.
    >>> I am looking for an algorithm to implement this checking. Anybody
    >>> gives an idea?
    >>> Thanks.

    >> Depends what you mean by "treat it as"
    >>
    >> Surely the math is simple enough to grab to decimal portion of the
    >> double and compare it to .0001 or any variable amount you wish.
    >> Getting the quotient and remainder after dividing by 1 comes to mind.
    >> I doubt I'd even call it an algorithm...

    >
    > By "treat it as" I mean I will do different things for the doubles
    > near int and for the doubles not near int.
    >
    > Your trick (if you don't wanna call it algorithm) does not really work
    > for my case because I wanna 9.99999 to be "near int" however its
    > decimal portion is 0.99999.


    Given:
    #include <cmath>
    const double eps = 0.0001; // your treshold
    double d = 10.00005; // your number

    The algorithm would be:
    1. Round d to nearest integer.
    2. Check if nearest integer is within treshold.

    double near = std::floor(d+0.5);
    if (std::abs(d - near) < eps) {
    int i = near;
    // use i or near
    }
    else
    {
    // use d
    }

    --
    Thomas
    Thomas J. Gritzan, Apr 23, 2008
    #8
  9. xz

    xz Guest

    On Apr 23, 5:48 pm, "Thomas J. Gritzan" <>
    wrote:
    > xz wrote:
    > > On Apr 23, 5:05 pm, Christopher <> wrote:
    > >> On Apr 23, 4:56 pm, xz <> wrote:

    >
    > >>> In a program, if a double is within 0.0001 from an int, then I treat
    > >>> it as int. For example, 10.00005 will be treated as int, while 10.001
    > >>> is not.
    > >>> I am looking for an algorithm to implement this checking. Anybody
    > >>> gives an idea?
    > >>> Thanks.
    > >> Depends what you mean by "treat it as"

    >
    > >> Surely the math is simple enough to grab to decimal portion of the
    > >> double and compare it to .0001 or any variable amount you wish.
    > >> Getting the quotient and remainder after dividing by 1 comes to mind.
    > >> I doubt I'd even call it an algorithm...

    >
    > > By "treat it as" I mean I will do different things for the doubles
    > > near int and for the doubles not near int.

    >
    > > Your trick (if you don't wanna call it algorithm) does not really work
    > > for my case because I wanna 9.99999 to be "near int" however its
    > > decimal portion is 0.99999.

    >
    > Given:
    > #include <cmath>
    > const double eps = 0.0001; // your treshold
    > double d = 10.00005; // your number
    >
    > The algorithm would be:
    > 1. Round d to nearest integer.
    > 2. Check if nearest integer is within treshold.
    >
    > double near = std::floor(d+0.5);
    > if (std::abs(d - near) < eps) {
    > int i = near;
    > // use i or near}
    >
    > else
    > {
    > // use d
    >
    > }
    >
    > --
    > Thomas



    +0.5!

    That's smart! Exactly what I wanted!

    Thanks a lot!
    xz, Apr 24, 2008
    #9
  10. xz

    xz Guest

    On Apr 23, 5:48 pm, "Thomas J. Gritzan" <>
    wrote:
    > xz wrote:
    > > On Apr 23, 5:05 pm, Christopher <> wrote:
    > >> On Apr 23, 4:56 pm, xz <> wrote:

    >
    > >>> In a program, if a double is within 0.0001 from an int, then I treat
    > >>> it as int. For example, 10.00005 will be treated as int, while 10.001
    > >>> is not.
    > >>> I am looking for an algorithm to implement this checking. Anybody
    > >>> gives an idea?
    > >>> Thanks.
    > >> Depends what you mean by "treat it as"

    >
    > >> Surely the math is simple enough to grab to decimal portion of the
    > >> double and compare it to .0001 or any variable amount you wish.
    > >> Getting the quotient and remainder after dividing by 1 comes to mind.
    > >> I doubt I'd even call it an algorithm...

    >
    > > By "treat it as" I mean I will do different things for the doubles
    > > near int and for the doubles not near int.

    >
    > > Your trick (if you don't wanna call it algorithm) does not really work
    > > for my case because I wanna 9.99999 to be "near int" however its
    > > decimal portion is 0.99999.

    >
    > Given:
    > #include <cmath>
    > const double eps = 0.0001; // your treshold
    > double d = 10.00005; // your number
    >
    > The algorithm would be:
    > 1. Round d to nearest integer.
    > 2. Check if nearest integer is within treshold.
    >
    > double near = std::floor(d+0.5);
    > if (std::abs(d - near) < eps) {


    I guess you meant fabs(...), right?

    > int i = near;
    > // use i or near}
    >
    > else
    > {
    > // use d
    >
    > }
    >
    > --
    > Thomas
    xz, Apr 24, 2008
    #10
  11. xz wrote:
    >> Given:
    >> #include <cmath>
    >> const double eps = 0.0001; // your treshold
    >> double d = 10.00005; // your number
    >>
    >> The algorithm would be:
    >> 1. Round d to nearest integer.
    >> 2. Check if nearest integer is within treshold.
    >>
    >> double near = std::floor(d+0.5);
    >> if (std::abs(d - near) < eps) {

    >
    > I guess you meant fabs(...), right?


    No. std::abs is overloaded for built-in types. fabs() has the signature:
    double fabs(double);

    So in this case you can use both.

    --
    Thomas
    Thomas J. Gritzan, Apr 24, 2008
    #11
  12. xz

    xz Guest

    On Apr 23, 9:59 pm, "Thomas J. Gritzan" <>
    wrote:
    > xz wrote:
    > >> Given:
    > >> #include <cmath>
    > >> const double eps = 0.0001; // your treshold
    > >> double d = 10.00005; // your number

    >
    > >> The algorithm would be:
    > >>    1. Round d to nearest integer.
    > >>    2. Check if nearest integer is within treshold.

    >
    > >> double near = std::floor(d+0.5);
    > >> if (std::abs(d - near) < eps) {

    >
    > > I guess you meant fabs(...), right?

    >
    > No. std::abs is overloaded for built-in types. fabs() has the signature:
    >    double fabs(double);
    >
    > So in this case you can use both.


    Thanks!

    >
    > --
    > Thomas
    xz, Apr 24, 2008
    #12
    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. David

    Incorrect syntax near 'int'

    David, Apr 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    2,448
    David
    Apr 7, 2006
  2. Sydex
    Replies:
    12
    Views:
    6,429
    Victor Bazarov
    Feb 17, 2005
  3. Schnoffos
    Replies:
    2
    Views:
    1,189
    Martien Verbruggen
    Jun 27, 2003
  4. Hal Styli
    Replies:
    14
    Views:
    1,604
    Old Wolf
    Jan 20, 2004
  5. Shriramana Sharma
    Replies:
    8
    Views:
    261
    Gerhard Fiedler
    Jun 18, 2013
Loading...

Share This Page