integer overflow

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

    integer overflow

    Hi ,

    I am performing an integer count of a particular operation in my program.
    After a sufficiently large value an overflow occurs. At the moment I have
    gone around the problem by declaring it as a double, even that has its
    limits. Is there a method of preventing this overflow or some method of
    recovering from it. Any help in this regard would be greatly appreciated.

    Thanking you.

    Ashutosh





  • CBFalconer

    #2
    Re: integer overflow

    Ashutosh Iddya wrote:[color=blue]
    >
    > I am performing an integer count of a particular operation in my
    > program. After a sufficiently large value an overflow occurs. At
    > the moment I have gone around the problem by declaring it as a
    > double, even that has its limits. Is there a method of preventing
    > this overflow or some method of recovering from it. Any help in
    > this regard would be greatly appreciated.[/color]

    If long won't do, then you can use long long (on C99 or gnu gcc
    systems). Otherwise try:

    if (n < INT_MAX) n++;
    else {
    overflows++;
    n = 0;
    }

    and don't forget to #include <limits.h>

    It would be simpler to use unsigned types, and then:

    if (!(++n)) overflow++;

    --
    A: Because it fouls the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Ashutosh Iddya

      #3
      Re: integer overflow

      thanks for that. I will try it out and let you know how it went
      Ashutosh
      "CBFalconer " <cbfalconer@yah oo.com> wrote in message
      news:407FC7A6.A 418B595@yahoo.c om...[color=blue]
      > Ashutosh Iddya wrote:[color=green]
      > >
      > > I am performing an integer count of a particular operation in my
      > > program. After a sufficiently large value an overflow occurs. At
      > > the moment I have gone around the problem by declaring it as a
      > > double, even that has its limits. Is there a method of preventing
      > > this overflow or some method of recovering from it. Any help in
      > > this regard would be greatly appreciated.[/color]
      >
      > If long won't do, then you can use long long (on C99 or gnu gcc
      > systems). Otherwise try:
      >
      > if (n < INT_MAX) n++;
      > else {
      > overflows++;
      > n = 0;
      > }
      >
      > and don't forget to #include <limits.h>
      >
      > It would be simpler to use unsigned types, and then:
      >
      > if (!(++n)) overflow++;
      >
      > --
      > A: Because it fouls the order in which people normally read text.
      > Q: Why is top-posting such a bad thing?
      > A: Top-posting.
      > Q: What is the most annoying thing on usenet and in e-mail?
      >[/color]


      Comment

      • Darrell Grainger

        #4
        Re: integer overflow

        On Fri, 16 Apr 2004, Ashutosh Iddya wrote:
        [color=blue]
        > Hi ,
        >
        > I am performing an integer count of a particular operation in my program.
        > After a sufficiently large value an overflow occurs. At the moment I have
        > gone around the problem by declaring it as a double, even that has its
        > limits. Is there a method of preventing this overflow or some method of
        > recovering from it. Any help in this regard would be greatly appreciated.[/color]

        If overflow is happening with your data type you could detect it but
        preventing it means switching to a different data type. For an integer
        data type (long long) is your best bet. If you are only working with
        unsigned numbers then (unsigned long long) is your best bet.

        You might still get overflow. If this is the case you can try using double
        as your data type. Another option is to search the web for "big number C
        library". There are libraries you can use for numbers bigger than unsigned
        long long.

        If you go with the big number library it will probably be slower than
        using long long. You might me able to create your own limited big number
        library. If you are just counting then you only need addition and some way
        of printing the number.

        --
        Send e-mail to: darrell at cs dot toronto dot edu
        Don't send e-mail to vice.president@ whitehouse.gov

        Comment

        • Dan Pop

          #5
          Re: integer overflow

          In <407fba3a$0$165 82$5a62ac22@fre enews.iinet.net .au> "Ashutosh Iddya" <ashutosh.iddya @news.edu.au> writes:
          [color=blue]
          >I am performing an integer count of a particular operation in my program.
          >After a sufficiently large value an overflow occurs. At the moment I have
          >gone around the problem by declaring it as a double, even that has its
          >limits.[/color]

          I have a hard time imagining you're going to reach the limits of the
          double solution within a reasonable period of time, even if all you
          do is counting. Try the following program and see how long it takes
          to terminate normally.

          #include <stdio.h>

          int main()
          {
          double d = 0;
          while (d + 1 != d) d++;
          printf("%.16f\n ", d);
          return 0;
          }

          Assumming that the loop takes one CPU cycle per iteration and that the CPU
          is running at 4.5 GHz, this proggie should run for about 1e6 seconds, if
          using IEEE-754 doubles.

          BTW, how about "optimising " the while loop like this?

          while (d != ++d) ;

          ;-)

          Dan
          --
          Dan Pop
          DESY Zeuthen, RZ group
          Email: Dan.Pop@ifh.de

          Comment

          • Grumble

            #6
            Re: integer overflow

            Darrell Grainger wrote:
            [color=blue]
            > On Fri, 16 Apr 2004, Ashutosh Iddya wrote:
            >[color=green]
            >> I am performing an integer count of a particular operation in my program.
            >> After a sufficiently large value an overflow occurs. At the moment I have
            >> gone around the problem by declaring it as a double, even that has its
            >> limits. Is there a method of preventing this overflow or some method of
            >> recovering from it. Any help in this regard would be greatly appreciated.[/color]
            >
            > If overflow is happening with your data type you could detect it but
            > preventing it means switching to a different data type. For an integer
            > data type (long long) is your best bet. If you are only working with
            > unsigned numbers then (unsigned long long) is your best bet.
            >
            > You might still get overflow.[/color]

            C99's long long int is /at least/ 64 bits wide.

            If the OP's counter were incremented 100 times every cycle on a 10 GHz
            processor, it would take 213 days for the counter to overflow.
            [color=blue]
            > If this is the case you can try using double as your data type.[/color]

            Bad advice.

            An IEEE 754 double will only provide 53 bits of precision.

            See DBL_MANT_DIG and FLT_RADIX in float.h

            For my information, are there implementations where double is wider
            than 64 bits?

            Comment

            • Dan Pop

              #7
              Re: integer overflow

              In <Pine.GSO.4.58. 0404161020380.8 910@drj.pf> darrell@NOMORES PAMcs.utoronto. ca.com (Darrell Grainger) writes:
              [color=blue]
              >On Fri, 16 Apr 2004, Ashutosh Iddya wrote:
              >[color=green]
              >> I am performing an integer count of a particular operation in my program.
              >> After a sufficiently large value an overflow occurs. At the moment I have
              >> gone around the problem by declaring it as a double, even that has its
              >> limits. Is there a method of preventing this overflow or some method of
              >> recovering from it. Any help in this regard would be greatly appreciated.[/color]
              >
              >If overflow is happening with your data type you could detect it but
              >preventing it means switching to a different data type. For an integer
              >data type (long long) is your best bet. If you are only working with
              >unsigned numbers then (unsigned long long) is your best bet.[/color]

              Unless your C compiler, invoked in conforming mode, tells you that
              "long long" is a syntax error ;-)

              Are the latest Microsoft C compilers supporting long long?

              Dan
              --
              Dan Pop
              DESY Zeuthen, RZ group
              Email: Dan.Pop@ifh.de

              Comment

              • Eric Sosman

                #8
                Re: integer overflow

                Grumble wrote:[color=blue]
                > [...]
                > For my information, are there implementations where double is wider
                > than 64 bits?[/color]

                VAX H-format floating point had/has 128 bits; I don't recall
                how they're divided up between exponent and fraction. Also,
                when I was working with VAXen the C implementations were not
                able to use H-format. (For the curious: `float' used the
                32-bit F-format, and `double' used either D-format or G-format,
                both 64 bits, at your option -- and if you accidentally mixed
                G's and D's ... "The horror! The horror!")

                --
                Eric.Sosman@sun .com

                Comment

                • CBFalconer

                  #9
                  Re: integer overflow

                  Ashutosh Iddya wrote:[color=blue]
                  >
                  > thanks for that. I will try it out and let you know how it went[/color]

                  Kindly DO NOT toppost. See sig.

                  --
                  A: Because it fouls the order in which people normally read text.
                  Q: Why is top-posting such a bad thing?
                  A: Top-posting.
                  Q: What is the most annoying thing on usenet and in e-mail?


                  Comment

                  • Keith Thompson

                    #10
                    Re: integer overflow

                    "Ashutosh Iddya" <ashutosh.iddya @news.edu.au> writes:[color=blue]
                    > I am performing an integer count of a particular operation in my program.
                    > After a sufficiently large value an overflow occurs. At the moment I have
                    > gone around the problem by declaring it as a double, even that has its
                    > limits. Is there a method of preventing this overflow or some method of
                    > recovering from it. Any help in this regard would be greatly appreciated.[/color]

                    Using double is probably the wrong solution. If you repeatedly add 1
                    to a double variable, you'll never get an overflow, just a loss of
                    precision; eventually, (d + 1.0 == d).

                    The type "long long" is guaranteed to be at least 64 bits, which
                    should be more than enough for whatever it is you're counting. This
                    type is new in C99, but many C90 compilers support it as an extension.
                    (Since the C90 standard doesn't specify "long long", it's
                    theoretically possible that a C90 compiler could provide a "long long"
                    type that's smaller than 64 bits, but I don't think anyone has ever
                    done that.)

                    If 32 bits is enough, use long (or unsigned long).

                    If 32 bits isn't enough, and you don't care about portability to
                    systems that don't support long long, use long long (or
                    unsigned long long).

                    Be aware that a C90 implementation that supports long long as an
                    extension may not necessarily support everything that goes with it,
                    particularly the printf formats.

                    If you can't use long long, you might try using a pair of unsigned
                    longs (untested code follows):

                    unsigned long count_hi = 0;
                    unsigned long count_lo = 0;
                    ...
                    for (...) {
                    count_lo ++;
                    if (count_lo == 0) count_hi ++;
                    }

                    Note the use of unsigned types. Overflow is well-defined for unsigned
                    types; it wraps around. Overflow for signed types causes undefined
                    behavior; wraparound is common, but not guaranteed.

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                    Schroedinger does Shakespeare: "To be *and* not to be"

                    Comment

                    • Darrell Grainger

                      #11
                      Re: integer overflow

                      On Fri, 16 Apr 2004, Grumble wrote:
                      [color=blue]
                      > Darrell Grainger wrote:
                      >[color=green]
                      > > On Fri, 16 Apr 2004, Ashutosh Iddya wrote:
                      > >[color=darkred]
                      > >> I am performing an integer count of a particular operation in my program.
                      > >> After a sufficiently large value an overflow occurs. At the moment I have
                      > >> gone around the problem by declaring it as a double, even that has its
                      > >> limits. Is there a method of preventing this overflow or some method of
                      > >> recovering from it. Any help in this regard would be greatly appreciated.[/color]
                      > >
                      > > If overflow is happening with your data type you could detect it but
                      > > preventing it means switching to a different data type. For an integer
                      > > data type (long long) is your best bet. If you are only working with
                      > > unsigned numbers then (unsigned long long) is your best bet.
                      > >
                      > > You might still get overflow.[/color]
                      >
                      > C99's long long int is /at least/ 64 bits wide.[/color]

                      I'd doubt the OP was using a C99 compiler. It is possible but my first
                      assumption would be a C89 compiler.
                      [color=blue]
                      > If the OP's counter were incremented 100 times every cycle on a 10 GHz
                      > processor, it would take 213 days for the counter to overflow.[/color]

                      Man, slow day and my brain shuts down. Just a month ago I proved that
                      overflow on a cycle count profiler for a 1 GHz processor would take over
                      500 years to occur. I should have realized this.
                      [color=blue][color=green]
                      > > If this is the case you can try using double as your data type.[/color]
                      >
                      > Bad advice.
                      >
                      > An IEEE 754 double will only provide 53 bits of precision.
                      >
                      > See DBL_MANT_DIG and FLT_RADIX in float.h[/color]

                      I was thinking more of situations when (long long) would be 32 bit. On
                      older compilers I remember seeing support for (long long) such that
                      sizeof(long) == sizeof(long long). Essentially they made it so source with
                      (long long) would not be considered a syntax error but still only
                      supported 32 bit integers.
                      [color=blue]
                      > For my information, are there implementations where double is wider
                      > than 64 bits?[/color]

                      Not that I have seen.

                      --
                      Send e-mail to: darrell at cs dot toronto dot edu
                      Don't send e-mail to vice.president@ whitehouse.gov

                      Comment

                      • jacob navia

                        #12
                        Re: integer overflow


                        "Grumble" <invalid@kma.eu .org> a écrit dans le message de
                        news:c5otk9$3aq $1@news-rocq.inria.fr.. .[color=blue]
                        > Darrell Grainger wrote:
                        >
                        > For my information, are there implementations where double is wider
                        > than 64 bits?
                        >[/color]

                        There is the standard long double for that.
                        In lcc-win32 long double is 80 bits.

                        Then, you have the qfloat with 350 bits if you
                        really want big precision.

                        After that, the bignums is the only way, or
                        (much better) to look at the algorithm and
                        see why it is overflowing :-)




                        Comment

                        • Keith Thompson

                          #13
                          Re: integer overflow

                          Grumble <invalid@kma.eu .org> writes:
                          [...][color=blue]
                          > For my information, are there implementations where double is wider
                          > than 64 bits?[/color]

                          The standard allows it, but I doubt that any implementations actually
                          do so. If you have a floating-point type wider than 64 bits, you're
                          more likely to call it "long double" (which has been valid at least
                          since the C89/C90 standard).

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                          Schroedinger does Shakespeare: "To be *and* not to be"

                          Comment

                          • Keith Thompson

                            #14
                            Re: integer overflow

                            darrell@NOMORES PAMcs.utoronto. ca.com (Darrell Grainger) writes:
                            [...][color=blue]
                            > I was thinking more of situations when (long long) would be 32 bit. On
                            > older compilers I remember seeing support for (long long) such that
                            > sizeof(long) == sizeof(long long). Essentially they made it so source with
                            > (long long) would not be considered a syntax error but still only
                            > supported 32 bit integers.[/color]

                            Ick!

                            Can you remember which compiler did this? Did it really implement
                            "long long" as a distinct type, or did it just allow the "long"
                            keyword to be repeated with no further effect (so "long", "long long",
                            and "long long long" would all be equivalent)?

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                            Schroedinger does Shakespeare: "To be *and* not to be"

                            Comment

                            • Peter Nilsson

                              #15
                              Re: integer overflow

                              "Ashutosh Iddya" <ashutosh.iddya @news.edu.au> wrote in message
                              news:407fba3a$0 $16582$5a62ac22 @freenews.iinet .net.au...[color=blue]
                              > Hi ,
                              >
                              > I am performing an integer count of a particular operation in my program.
                              > After a sufficiently large value an overflow occurs. At the moment I have
                              > gone around the problem by declaring it as a double, even that has its
                              > limits. Is there a method of preventing this overflow or some method of
                              > recovering from it. Any help in this regard would be greatly appreciated.[/color]

                              unsigned long counter[2] = { 0 };

                              for (;;)
                              {
                              if (++counter[0] && ++counter[1])
                              puts("64+ bit counter overflowed!");
                              }

                              This is trivially extendable to as much precision as you want. But if you need a counter
                              bigger than a minimum of 64-bits, then I'd love to know what machine you're using and
                              where I can pick one up!

                              --
                              Peter


                              Comment

                              Working...