Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A few things about division by zero in C [duplicate]

Tags:

c++

c

visual-c++

Possible Duplicate:
Value returning 1.#INF000

I always thought division by 0 would result in a compiled program crashing

However I discovered today (using VC++ 2010 Express) that division by 0 gives something called 1.#INF000 and it is supposed to be positive infinity

When it was passed to a function, it got passed as -1.#IND000

What is this all about?

Searching 1.#INF000 and -1.#IND000 on google do not provide any clear explanations either

Is it just something specific to VC++ ?

like image 766
user13267 Avatar asked Sep 27 '12 08:09

user13267


People also ask

What happens if you divide by 0 in C?

Because what happens is that if we can say that zero, 5, or basically any number, then that means that that "c" is not unique. So, in this scenario the first part doesn't work. So, that means that this is going to be undefined. So zero divided by zero is undefined.

What type of error is dividing by zero in C?

Dividing a number by Zero is a mathematical error (not defined) and we can use exception handling to gracefully overcome such operations. If you write a code without using exception handling then the output of division by zero will be shown as infinity which cannot be further processed.

How is the division by zero defined in programming?

Division by zero is the operation of taking the quotient of any number and 0, i.e., . The uniqueness of division breaks down when dividing by zero, since the product is the same for any , so. cannot be recovered by inverting the process of multiplication.

What type of error is divide by zero?

Definition. Division by zero is a logic software bug that in most cases causes a run-time error when a number is divided by zero.


2 Answers

Floating point division by zero behaves differently than integer division by zero.

The IEEE floating point standard differentiates between +inf and -inf, while integers cannot store infinity. Integer division by zero results in undefined behaviour. Floating point division by zero is defined by the floating point standard and results in +inf or -inf.

Edit:

As pointed out by Luchian, C++ implementations are not required to follow the IEEE Floating point standard. If the implementation you use doesn't follow the IEEE Floating point standard the result of floating point division by zero is undefined.

like image 137
Klas Lindbäck Avatar answered Oct 11 '22 05:10

Klas Lindbäck


Edit: The question is about C++ and the result in C++ is undefined, as clearly stated by the standard, not the IEEE or whatever other entity that doesn't, in fact, regulate the C++ language. The standard does. C++ implementations might follow IEEE rules, but in this case it's clear the behavior is undefined.

I always thought division by 0 would result in a compiled program crashing

Nope, it results in undefined behavior. Anything can happen, a crash is not guaranteed.

According to the C++ Standard:

5.6 Multiplicative operators

4) The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined; otherwise (a/b)*b + a%b is equal to a. If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined79). (emphasis mine)

like image 35
Luchian Grigore Avatar answered Oct 11 '22 03:10

Luchian Grigore