test if integer

Discussion in 'C Programming' started by laura, Aug 4, 2007.

  1. laura

    laura Guest

    Hi,

    I have a variable of type double.
    I need to know if there is an integer number store there. How can I
    test that ?

    I also have a default precision for doing this operation.

    Many thanks,
    Laura
     
    laura, Aug 4, 2007
    #1
    1. Advertising

  2. laura

    pete Guest

    laura wrote:
    >
    > Hi,
    >
    > I have a variable of type double.
    > I need to know if there is an integer number store there. How can I
    > test that ?
    >
    > I also have a default precision for doing this operation.


    #include <math.h>

    if (fmod(variable, 1) == 0) {/* variable is an integer */}

    --
    pete
     
    pete, Aug 4, 2007
    #2
    1. Advertising

  3. laura wrote:
    > Hi,
    >
    > I have a variable of type double.
    > I need to know if there is an integer number store there. How can I
    > test that ?
    >
    > I also have a default precision for doing this operation.


    In addition to the following kind of test, think about whether the
    ceil() or floor() functions might provide you a test. Consider whether
    using them leads to a more reliable test or a more efficient one.

    The newer functions round(), trunc(), rint, and nearbyint() might also
    provide you with suitable tests. The rint() might be what you want,
    since it raises the "inexact" floating point exception if the argument
    is not an integer. You might find that learning how to handle floating
    point exceptions is not what you want to be doing now, though. If you
    do, check your text for a discussion of the <fenv.h> header. If your
    implementation does not provide such a header, then don't bother.

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

    int main(void)
    {
    int i;
    double base = 4080., testv, intpart, fraction;
    for (i = 2; i <= 20; i++) {
    testv = base / i;
    printf("Testing %g: ", testv);
    fraction = modf(testv, &intpart);
    if (fraction) {
    printf("fractional part = %g\n"
    " so probably not an integer.\n"
    "integer part = %g.\n\n", fraction, intpart);
    }
    else {
    printf("fractional part test == 0,\n" " so an integer.\n"
    "integer part = %g.\n\n", intpart);
    }
    }
    return 0;
    }

    Testing 2040: fractional part test == 0,
    so an integer.
    integer part = 2040.

    Testing 1360: fractional part test == 0,
    so an integer.
    integer part = 1360.

    Testing 1020: fractional part test == 0,
    so an integer.
    integer part = 1020.

    Testing 816: fractional part test == 0,
    so an integer.
    integer part = 816.

    Testing 680: fractional part test == 0,
    so an integer.
    integer part = 680.

    Testing 582.857: fractional part = 0.857143
    so probably not an integer.
    integer part = 582.

    Testing 510: fractional part test == 0,
    so an integer.
    integer part = 510.

    Testing 453.333: fractional part = 0.333333
    so probably not an integer.
    integer part = 453.

    Testing 408: fractional part test == 0,
    so an integer.
    integer part = 408.

    Testing 370.909: fractional part = 0.909091
    so probably not an integer.
    integer part = 370.

    Testing 340: fractional part test == 0,
    so an integer.
    integer part = 340.

    Testing 313.846: fractional part = 0.846154
    so probably not an integer.
    integer part = 313.

    Testing 291.429: fractional part = 0.428571
    so probably not an integer.
    integer part = 291.

    Testing 272: fractional part test == 0,
    so an integer.
    integer part = 272.

    Testing 255: fractional part test == 0,
    so an integer.
    integer part = 255.

    Testing 240: fractional part test == 0,
    so an integer.
    integer part = 240.

    Testing 226.667: fractional part = 0.666667
    so probably not an integer.
    integer part = 226.

    Testing 214.737: fractional part = 0.736842
    so probably not an integer.
    integer part = 214.

    Testing 204: fractional part test == 0,
    so an integer.
    integer part = 204.
     
    Martin Ambuhl, Aug 4, 2007
    #3
  4. In article <>,
    laura <> wrote:
    >I have a variable of type double.
    >I need to know if there is an integer number store there. How can I
    >test that ?


    Other posters have given some good ideas.

    You do need to be careful, though: if you do fmod(value,1.0) can you be
    sure that there will not be any round-off in the last unit?

    You also have a problem of resolution: if the input value is (say)
    1263330816481424704.3432 then with a IEEE 754 double, this
    would be stored as 1.2633308164814246e+18 which has been unable
    to store the last 104.3432 or so; fmod(1.2633308164814246e+18,1.0)
    would be 0 even though the input was not an integer as far as
    the human was concerned.
    --
    Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us. -- Ecclesiastes
     
    Walter Roberson, Aug 4, 2007
    #4
  5. laura

    Army1987 Guest

    On Sat, 04 Aug 2007 18:18:35 +0000, Walter Roberson wrote:

    > In article <>,
    > laura <> wrote:
    >>I have a variable of type double.
    >>I need to know if there is an integer number store there. How can I
    >>test that ?

    >
    > Other posters have given some good ideas.
    >
    > You do need to be careful, though: if you do fmod(value,1.0) can you be
    > sure that there will not be any round-off in the last unit?
    >
    > You also have a problem of resolution: if the input value is (say)
    > 1263330816481424704.3432 then with a IEEE 754 double, this
    > would be stored as 1.2633308164814246e+18 which has been unable
    > to store the last 104.3432 or so; fmod(1.2633308164814246e+18,1.0)
    > would be 0 even though the input was not an integer as far as
    > the human was concerned.

    What? She asked how to know "if there is an integer number
    store[d] there". She did not ask to know whether it is an integer
    because of rounding, considering there is no way a C program can
    determine that.

    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
     
    Army1987, Aug 5, 2007
    #5
  6. In article <>,
    Army1987 <> wrote:
    >On Sat, 04 Aug 2007 18:18:35 +0000, Walter Roberson wrote:


    >> In article <>,
    >> laura <> wrote:
    >>>I have a variable of type double.
    >>>I need to know if there is an integer number store there. How can I
    >>>test that ?


    >> You also have a problem of resolution: if the input value is (say)
    >> 1263330816481424704.3432 then with a IEEE 754 double, this
    >> would be stored as 1.2633308164814246e+18 which has been unable
    >> to store the last 104.3432 or so;


    >What? She asked how to know "if there is an integer number
    >store[d] there". She did not ask to know whether it is an integer
    >because of rounding, considering there is no way a C program can
    >determine that.


    But she asked whether there is an integer "number" stored there,
    not an integer "value". "number" is the pure concept, with "double"
    being an approximation. When one examines the approximation, one cannot
    be sure that the original -number- was an integer or not.
    --
    Programming is what happens while you're busy making other plans.
     
    Walter Roberson, Aug 5, 2007
    #6
  7. laura

    Army1987 Guest

    On Sun, 05 Aug 2007 19:51:55 +0000, Walter Roberson wrote:

    > In article <>,
    > Army1987 <> wrote:
    >>On Sat, 04 Aug 2007 18:18:35 +0000, Walter Roberson wrote:

    >
    >>> In article <>,
    >>> laura <> wrote:
    >>>>I have a variable of type double.
    >>>>I need to know if there is an integer number store there. How can I
    >>>>test that ?

    >
    >>> You also have a problem of resolution: if the input value is (say)
    >>> 1263330816481424704.3432 then with a IEEE 754 double, this
    >>> would be stored as 1.2633308164814246e+18 which has been unable
    >>> to store the last 104.3432 or so;

    >
    >>What? She asked how to know "if there is an integer number
    >>store[d] there". She did not ask to know whether it is an integer
    >>because of rounding, considering there is no way a C program can
    >>determine that.

    >
    > But she asked whether there is an integer "number" stored there,
    > not an integer "value". "number" is the pure concept, with "double"
    > being an approximation. When one examines the approximation, one cannot
    > be sure that the original -number- was an integer or not.

    If I write
    double a;
    a = 1.00000000000000000000000000000000000000000000000000000000001;
    supposing DBL_EPSILON is large enough, that number gets rounded
    *before* being stored in a. The number stored in a is 1.
    If she meant "How do I know whether the number 'intended to' be
    stored is integer", well, short of having the program read its own
    source for constants, tricky workarounds for strtod, and
    calculations with infinite precision for other things such as
    (1.0 / 3.0) * 3.0, how the hell is she supposed to do that?

    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
     
    Army1987, Aug 6, 2007
    #7
  8. In article <>,
    Army1987 <> wrote:
    >On Sun, 05 Aug 2007 19:51:55 +0000, Walter Roberson wrote:
    >
    >> In article <>,
    >> Army1987 <> wrote:
    >>>On Sat, 04 Aug 2007 18:18:35 +0000, Walter Roberson wrote:


    >>>> In article <>,
    >>>> laura <> wrote:
    >>>>>I have a variable of type double.
    >>>>>I need to know if there is an integer number store there. How can I
    >>>>>test that ?


    >>>> You also have a problem of resolution: if the input value is (say)
    >>>> 1263330816481424704.3432 then with a IEEE 754 double, this
    >>>> would be stored as 1.2633308164814246e+18 which has been unable
    >>>> to store the last 104.3432 or so;


    >If she meant "How do I know whether the number 'intended to' be
    >stored is integer", well, short of having the program read its own
    >source for constants, tricky workarounds for strtod, and
    >calculations with infinite precision for other things such as
    >(1.0 / 3.0) * 3.0, how the hell is she supposed to do that?


    Exactly. She can't. There's the problem of resolution; infinite
    resolution, just as you point out.

    When people ask questions here, what they ask to do isn't always
    possible to do. But often, what they ask to do isn't actually
    what they want to do; sometimes it is more feasible and sometimes
    it is even less feasible; we can but point out the issues and hope
    that helps.
    --
    "No one has the right to destroy another person's belief by
    demanding empirical evidence." -- Ann Landers
     
    Walter Roberson, Aug 6, 2007
    #8
    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. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,976
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    535
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    520
    Yu SONG
    Oct 15, 2004
  4. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    6
    Views:
    45,823
    JavaBean2010
    Apr 7, 2010
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    745
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page