Division by zero

S

sandeep

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
 
S

Seebs

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
 
T

Tom St Denis

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

jacob navia

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
 
S

sandeep

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

Eric Sosman

sandeep a écrit :

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

Kaz Kylheku

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

Eric Sosman

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

Nick

Eric Sosman said:
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.
 
E

Eric Sosman

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

Phil Carmody

Gareth Owen said:
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
 
J

jacob navia

Eric Sosman a écrit :
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
 
S

Serve Laurijssen

sandeep said:
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...
 
N

Nobody

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

jacob navia

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

Phil Carmody

Gareth Owen said:
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
 
P

Phil Carmody

Richard Heathfield said:
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.
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
 
E

Eric Sosman

[...]
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().
 
P

Paul N

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top