Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Division by zero

Reply
Thread Tools

Division by zero

 
 
Phil Carmody
Guest
Posts: n/a
 
      01-16-2010
Gareth Owen <(E-Mail Removed)> writes:
> sandeep <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      01-16-2010
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
 
Reply With Quote
 
 
 
 
Serve Laurijssen
Guest
Posts: n/a
 
      01-16-2010

"sandeep" <(E-Mail Removed)> schreef in bericht
news:hiqmto$2uk$(E-Mail Removed)...
> 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...

 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      01-17-2010
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.

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-17-2010
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.
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      01-17-2010
Gareth Owen <(E-Mail Removed)> writes:
> Phil Carmody <(E-Mail Removed)> writes:
>> Gareth Owen <(E-Mail Removed)> writes:
>>> sandeep <(E-Mail Removed)> 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
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      01-17-2010
Richard Heathfield <(E-Mail Removed)> writes:
> Phil Carmody wrote:
>> Gareth Owen <(E-Mail Removed)> writes:
>>> sandeep <(E-Mail Removed)> 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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-17-2010
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      01-18-2010
On 15 Jan, 21:32, sandeep <(E-Mail Removed)> 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/...ResultCount=10
amongst other places.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
division by 7 without using division operator krypto.wizard@gmail.com C Programming 94 02-09-2007 06:57 AM
obscure division by zero conundrum subnunciation@gmail.com ASP General 2 05-11-2006 05:27 PM
Division by zero: float vs. int Razvan Java 77 10-19-2004 03:42 AM
memset all bits to zero will let float/double to zero? Zhiqiang Ye C Programming 53 06-28-2004 01:23 PM
Formatting percent and dealing with division by zero problems Bill ASP General 4 08-29-2003 03:16 PM



Advertisments