Division by zero

Discussion in 'C Programming' started by sandeep, Jan 15, 2010.

  1. sandeep

    sandeep Guest

    Hello friends

    I would like that instead of exitting with an error, division by zero
    should instead return
    * INTMAX (for Integer diviosn)
    * NAN (for FP division)

    Is there a way to achieve it? (compiler option or a macro)

    Thanks
    sandeep, Jan 15, 2010
    #1
    1. Advertising

  2. sandeep

    Seebs Guest

    On 2010-01-15, sandeep <> wrote:
    > I would like that instead of exitting with an error, division by zero
    > should instead return
    > * INTMAX (for Integer diviosn)
    > * NAN (for FP division)


    > Is there a way to achieve it? (compiler option or a macro)


    Maybe, depending on your compiler, but it's not at all obvious. There's
    a REASON that the floating-point stuff is defined the way it is, and as
    to integer division... Slow down a moment.

    What about "-1/0"? Do you want INT_MAX or INT_MIN? Why?

    In short: If you want to do this, I suggest you do it yourself, but you
    could check your compiler manual to see whether it has hooks for such a
    thing.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Jan 15, 2010
    #2
    1. Advertising

  3. sandeep

    Tom St Denis Guest

    On Jan 15, 4:32 pm, sandeep <> wrote:
    > Hello friends
    >
    > I would like that instead of exitting with an error, division by zero
    > should instead return
    > * INTMAX (for Integer diviosn)
    > * NAN (for FP division)
    >
    > Is there a way to achieve it? (compiler option or a macro)


    On some platforms you can trap the signal. Normally one would simply
    check for division by zero before dividing...
    Tom St Denis, Jan 15, 2010
    #3
  4. sandeep

    jacob navia Guest

    sandeep a écrit :
    > Hello friends
    >
    > I would like that instead of exitting with an error, division by zero
    > should instead return
    > * INTMAX (for Integer diviosn)
    > * NAN (for FP division)
    >
    > Is there a way to achieve it? (compiler option or a macro)
    >
    > Thanks


    The lcc-win compiler can do this, if you buy a special version.
    Note that NAN will normally be returned for a division by zero
    for floating point arguments in most compilers.

    To trap integer division exception you have to setup some
    signal.

    But if you are in windows, just buy lcc-win are you are done!

    jacob
    jacob navia, Jan 15, 2010
    #4
  5. sandeep

    sandeep Guest

    That would be great but is there a free version for AIX?

    I find it produces a SIGFPE exception not NAN.

    In the code there are variables that should never be zero --- but because
    (I think) of errors or some data corruption in other parts of the program
    (maybe buffer overflow, I don't know), sometimes they can be zero... I
    don't want the program to crash but to degrade gracefully in those
    situations.


    jacob navia writes:
    > sandeep a écrit :
    >> Hello friends
    >>
    >> I would like that instead of exitting with an error, division by zero
    >> should instead return
    >> * INTMAX (for Integer diviosn)
    >> * NAN (for FP division)
    >>
    >> Is there a way to achieve it? (compiler option or a macro)
    >>
    >> Thanks

    >
    > The lcc-win compiler can do this, if you buy a special version. Note
    > that NAN will normally be returned for a division by zero for floating
    > point arguments in most compilers.
    >
    > To trap integer division exception you have to setup some signal.
    >
    > But if you are in windows, just buy lcc-win are you are done!
    >
    > jacob
    sandeep, Jan 15, 2010
    #5
  6. sandeep

    Eric Sosman Guest

    On 1/15/2010 5:09 PM, jacob navia wrote:
    > sandeep a écrit :
    >> Hello friends
    >>
    >> I would like that instead of exitting with an error, division by zero
    >> should instead return
    >> * INTMAX (for Integer diviosn)
    >> * NAN (for FP division)
    >>
    >> Is there a way to achieve it? (compiler option or a macro)
    >>
    >> Thanks

    >
    > The lcc-win compiler can do this, if you buy a special version.
    > Note that NAN will normally be returned for a division by zero
    > for floating point arguments in most compilers.


    If by "most compilers" you mean "systems that use IEEE
    floating-point," then x/0. yields NaN only if x is 0 or NaN.
    For other x, it yields +Inf or -Inf.

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 15, 2010
    #6
  7. sandeep

    Kaz Kylheku Guest

    On 2010-01-15, sandeep <> wrote:
    > That would be great but is there a free version for AIX?
    >
    > I find it produces a SIGFPE exception not NAN.
    >
    > In the code there are variables that should never be zero --- but because
    > (I think) of errors or some data corruption in other parts of the program
    > (maybe buffer overflow, I don't know), sometimes they can be zero... I
    > don't want the program to crash but to degrade gracefully in those
    > situations.


    That's a bad reason for wanting to do this; find and fix the bug. INT_MAX is
    not the correct answer to a division with corrupted inputs.
    What if sometimes the corrupted values are nonzero?
    Kaz Kylheku, Jan 15, 2010
    #7
  8. sandeep

    Eric Sosman Guest

    On 1/15/2010 5:20 PM, sandeep wrote:
    > That would be great but is there a free version for AIX?
    >
    > I find it produces a SIGFPE exception not NAN.
    >
    > In the code there are variables that should never be zero --- but because
    > (I think) of errors or some data corruption in other parts of the program
    > (maybe buffer overflow, I don't know), sometimes they can be zero... I
    > don't want the program to crash but to degrade gracefully in those
    > situations.


    If by "gracefully" you mean that you'd like to see

    Sophisticated Portfolio Analyzer and Stochastic Modeler
    Analyzing portfolio ...
    Portfolio analysis complete
    Modeling market ...
    Market modeling complete
    Estimating taxes ...
    Tax estimation complete
    Formulating recommendations ...
    Recommendations:
    Sell NaN shares ACME
    Buy NaN shares ZORK
    Run completed; elapsed time 3.62 hours

    .... then you have a different notion of "graceful" than most.
    Besides, as Kaz points out: What if the corruption yields non-
    zero divisors, so you don't even get a visible warning of the
    malfunction? Your program might tell you to sell all your
    holdings and invest the proceeds in feces futures ...

    Remember the peril-sensitive sunglasses from The Hitchhiker's
    Guide to the Galaxy? They turned opaque when danger threatened,
    so as not to alarm the wearer by letting him see the threat ...
    You're trying to equip yourself with just such glasses; don't.

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 15, 2010
    #8
  9. sandeep

    Nick Guest

    Eric Sosman <> writes:

    > On 1/15/2010 5:20 PM, sandeep wrote:
    >> That would be great but is there a free version for AIX?
    >>
    >> I find it produces a SIGFPE exception not NAN.
    >>
    >> In the code there are variables that should never be zero --- but because
    >> (I think) of errors or some data corruption in other parts of the program
    >> (maybe buffer overflow, I don't know), sometimes they can be zero... I
    >> don't want the program to crash but to degrade gracefully in those
    >> situations.

    >
    > If by "gracefully" you mean that you'd like to see
    >
    > Sophisticated Portfolio Analyzer and Stochastic Modeler
    > Analyzing portfolio ...
    > Portfolio analysis complete
    > Modeling market ...
    > Market modeling complete
    > Estimating taxes ...
    > Tax estimation complete
    > Formulating recommendations ...
    > Recommendations:
    > Sell NaN shares ACME
    > Buy NaN shares ZORK
    > Run completed; elapsed time 3.62 hours


    I once saw a computer in the local Sainsbury's priced as "£Inf.Nan". I
    wish I'd had a camera with me.
    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
    Nick, Jan 16, 2010
    #9
  10. sandeep

    Eric Sosman Guest

    On 1/16/2010 3:04 AM, Nick wrote:
    >
    > I once saw a computer in the local Sainsbury's priced as "£Inf.Nan". I
    > wish I'd had a camera with me.


    Think expansively: Wish you'd had that much money with you.

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 16, 2010
    #10
  11. sandeep

    Phil Carmody Guest

    Gareth Owen <> writes:
    > sandeep <> writes:
    >
    >> Hello friends
    >>
    >> I would like that instead of exitting with an error, division by zero
    >> should instead return

    >
    >> * NAN (for FP division)

    >
    > GCC does this for FP, unless you specify -ffinite-math-only


    Why do you make such trivially falsifiable claims?

    #include <stdio.h>
    int main(int argc, char**argv)
    {
    printf("%f\n", -7./(argc-1));
    }

    -inf abounds, nary a NaN to be seen.

    Phil
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
    Phil Carmody, Jan 16, 2010
    #11
  12. sandeep

    jacob navia Guest

    Eric Sosman a écrit :
    > On 1/15/2010 5:09 PM, jacob navia wrote:
    >> sandeep a écrit :
    >>> Hello friends
    >>>
    >>> I would like that instead of exitting with an error, division by zero
    >>> should instead return
    >>> * INTMAX (for Integer diviosn)
    >>> * NAN (for FP division)
    >>>
    >>> Is there a way to achieve it? (compiler option or a macro)
    >>>
    >>> Thanks

    >>
    >> The lcc-win compiler can do this, if you buy a special version.
    >> Note that NAN will normally be returned for a division by zero
    >> for floating point arguments in most compilers.

    >
    > If by "most compilers" you mean "systems that use IEEE
    > floating-point," then x/0. yields NaN only if x is 0 or NaN.
    > For other x, it yields +Inf or -Inf.
    >


    true, sorry for the misinformation
    jacob navia, Jan 16, 2010
    #12
  13. "sandeep" <> schreef in bericht
    news:hiqmto$2uk$...
    > Hello friends
    >
    > I would like that instead of exitting with an error, division by zero
    > should instead return
    > * INTMAX (for Integer diviosn)
    > * NAN (for FP division)
    >
    > Is there a way to achieve it? (compiler option or a macro)
    >
    > Thanks


    int divide_int(int x, int n)
    {
    if (n == 0)
    return INT_MAX;
    else
    return x /n;
    }

    etc...
    Serve Laurijssen, Jan 16, 2010
    #13
  14. sandeep

    Nobody Guest

    On Fri, 15 Jan 2010 21:32:40 +0000, sandeep wrote:

    > I would like that instead of exitting with an error, division by zero
    > should instead return
    > * INTMAX (for Integer diviosn)
    > * NAN (for FP division)


    For an architecture which supports IEEE FP, FP division by zero yields
    infinity, unless the numerator is also zero, in which case it yields NaN.

    Why would you want FP division by zero to yield NaN for a result which is
    (actually) infinity?

    > Is there a way to achieve it? (compiler option or a macro)


    Most C compilers won't let you modify the behaviour of the "/" operator.
    You can write your own "div()" macro/function, or you can use C++.

    You may be able to intercept the signal generated for division by zero
    (typically SIGFPE), and even identify the cause (on POSIX systems, the
    siginfo_t passed to a handler registered with the SA_SIGINFO flag should
    have its si_code field set to FPE_INTDIV), but recovering from it is
    non-portable (and difficult), and ignoring it leads to undefined
    behaviour.
    Nobody, Jan 17, 2010
    #14
  15. sandeep

    jacob navia Guest

    Gareth Owen a écrit :
    >
    > Why are you so passive agressive about everything?


    Because he belongs to that nasty group of people here
    that loves aggressing people. He is one of the worst
    cases.

    Completely nuts. Just ignore him.
    jacob navia, Jan 17, 2010
    #15
  16. sandeep

    Phil Carmody Guest

    Gareth Owen <> writes:
    > Phil Carmody <> writes:
    >> Gareth Owen <> writes:
    >>> sandeep <> writes:
    >>>
    >>>> Hello friends
    >>>>
    >>>> I would like that instead of exitting with an error, division by zero
    >>>> should instead return
    >>>
    >>>> * NAN (for FP division)
    >>>
    >>> GCC does this for FP, unless you specify -ffinite-math-only

    >>
    >> Why do you make such trivially falsifiable claims?

    >
    > I mis-remembered. Sorry.
    >
    > Why are you so passive agressive about everything?


    You've mis-remembered the difference between 'snarky' and 'passive
    aggressive'. There's nothing passive aggressive about my posts at all.

    Phil
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
    Phil Carmody, Jan 17, 2010
    #16
  17. sandeep

    Phil Carmody Guest

    Richard Heathfield <> writes:
    > Phil Carmody wrote:
    >> Gareth Owen <> writes:
    >>> sandeep <> writes:
    >>>
    >>>> Hello friends
    >>>>
    >>>> I would like that instead of exitting with an error, division by
    >>>> zero should instead return
    >>>> * NAN (for FP division)
    >>> GCC does this for FP, unless you specify -ffinite-math-only

    >>
    >> Why do you make such trivially falsifiable claims?

    >
    > Whilst it is true that the claim is trivially falsifiable, that is not
    > what you should be complaining about. Instead, you should be
    > complaining that it is trivially falsifiED.

    [SNIP - repetition at length]

    I mean that it is capable of being proven false, nothing more, nothing
    less. That capability stems from it being false. That self-indulgent
    philosophers have hijacked the term to have additional implications in
    certain contexts is irrelevant. Any statement predicated upon a falsity
    is not worth time wasting time on.

    >> #include <stdio.h>
    >> int main(int argc, char**argv)
    >> {
    >> printf("%f\n", -7./(argc-1));
    >> }
    >>
    >> -inf abounds, nary a NaN to be seen.

    >
    > Why do you make such trivially falsified claims? I ran your program,
    > and got the output -2.333333, nary a -inf to be seen.


    Not from a division by zero, you didn't, I'll bet. Have you not
    noticed the subject line and context for this thread? Don't act
    dumb, it doesn't become you.

    Phil
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
    Phil Carmody, Jan 17, 2010
    #17
  18. sandeep

    Eric Sosman Guest

    On 1/17/2010 12:38 AM, Nobody wrote:
    > [...]
    > Most C compilers won't let you modify the behaviour of the "/" operator.
    > You can write your own "div()" macro/function, or you can use C++.


    ... but don't call it div(), for the same reason you
    wouldn't name an ultraviolet exposure calculator tan().

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 17, 2010
    #18
  19. sandeep

    Paul N Guest

    On 15 Jan, 21:32, sandeep <> wrote:
    > Hello friends
    >
    > I would like that instead of exitting with an error, division by zero
    > should instead return
    > * INTMAX (for Integer diviosn)
    > * NAN (for FP division)
    >
    > Is there a way to achieve it? (compiler option or a macro)
    >
    > Thanks


    Interestingly, people have considered doing this in hardware. See, for
    instance patent application GB 2285155 which can be found at
    http://gb.espacenet.com/search97cgi...rorTemplate=gb/en/incerror.hts&ResultCount=10
    amongst other places.
    Paul N, Jan 18, 2010
    #19
    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. Razvan
    Replies:
    77
    Views:
    13,812
    Joseph Daniel Zukiger
    Oct 19, 2004
  2. Zhiqiang Ye
    Replies:
    53
    Views:
    10,253
    Dan Pop
    Jun 28, 2004
  3. Replies:
    94
    Views:
    4,482
    ¬a\\/b
    Feb 9, 2007
  4. Bill
    Replies:
    4
    Views:
    127
    Aaron Bertrand - MVP
    Aug 29, 2003
  5. Replies:
    2
    Views:
    113
    kiana
    May 11, 2006
Loading...

Share This Page