Why doesn't 'd /= d' throw a division by zero exception?Catching exception: divide by zeroc++ division by 0When to use virtual destructors?Why is “using namespace std;” considered bad practice?Why do we need virtual functions in C++?What is move semantics?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?Why is processing a sorted array faster than processing an unsorted array?Why should I use a pointer rather than the object itself?C++ code for testing the Collatz conjecture faster than hand-written assembly - why?
What is a CirKle Word™?
Tensorflow - logistic regrssion -oneHot Encoder - Transformed array of differt size for both train and test
Which note goes on which side of the stem?
Does travel insurance for short flight delays exist?
I don't have the theoretical background in my PhD topic. I can't justify getting the degree
Handling Disruptive Student on the Autistic Spectrum
Is gzip atomic?
What is the history of the university asylum law?
How to respectfully refuse to assist co-workers with IT issues?
Why doesn't 'd /= d' throw a division by zero exception?
What to say to a student who has failed?
Why were the crew so desperate to catch Truman and return him to Seahaven?
Is there any method of inflicting the incapacitated condition and no other condition?
Is there any way to keep a player from killing an NPC?
Is it possible to perform a regression where you have an unknown / unknowable feature variable?
How to gently end involvement with an online community?
Why did they avoid parodying Martian Manhunter?
Identify a problem where a potentially winning move draws because of the 50 move rule
LeetCode: Group Anagrams C#
How to find out the average duration of the peer-review process for a given journal?
Sci fi film similar to Village of the Damned
Architectural feasibility of a tiered circular stone keep
State-of-the-art algorithms for solving linear programming problems
Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?
Why doesn't 'd /= d' throw a division by zero exception?
Catching exception: divide by zeroc++ division by 0When to use virtual destructors?Why is “using namespace std;” considered bad practice?Why do we need virtual functions in C++?What is move semantics?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?Why is processing a sorted array faster than processing an unsorted array?Why should I use a pointer rather than the object itself?C++ code for testing the Collatz conjecture faster than hand-written assembly - why?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I don't quite understand why I don't get a division by zero exception:
int d = 0;
d /= d;
I expected to get a division by zero exception but instead d == 1.
c++
New contributor
minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
|
show 6 more comments
I don't quite understand why I don't get a division by zero exception:
int d = 0;
d /= d;
I expected to get a division by zero exception but instead d == 1.
c++
New contributor
minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
its a matter of definition what should be the result of0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for
– formerlyknownas_463035818
8 hours ago
2
That’s undefined behavior.
– L. F.
8 hours ago
5
There's no such thing as a division by zero exception.
– πάντα ῥεῖ
8 hours ago
I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.
– Bathsheba
8 hours ago
2
To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by athrowstatement. Nothing else (unless you're in undefined-behavior land).
– Pete Becker
7 hours ago
|
show 6 more comments
I don't quite understand why I don't get a division by zero exception:
int d = 0;
d /= d;
I expected to get a division by zero exception but instead d == 1.
c++
New contributor
minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I don't quite understand why I don't get a division by zero exception:
int d = 0;
d /= d;
I expected to get a division by zero exception but instead d == 1.
c++
c++
New contributor
minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 4 hours ago
Boann
38.7k13 gold badges93 silver badges123 bronze badges
38.7k13 gold badges93 silver badges123 bronze badges
New contributor
minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 8 hours ago
minebvminebv
443 bronze badges
443 bronze badges
New contributor
minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
its a matter of definition what should be the result of0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for
– formerlyknownas_463035818
8 hours ago
2
That’s undefined behavior.
– L. F.
8 hours ago
5
There's no such thing as a division by zero exception.
– πάντα ῥεῖ
8 hours ago
I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.
– Bathsheba
8 hours ago
2
To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by athrowstatement. Nothing else (unless you're in undefined-behavior land).
– Pete Becker
7 hours ago
|
show 6 more comments
1
its a matter of definition what should be the result of0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for
– formerlyknownas_463035818
8 hours ago
2
That’s undefined behavior.
– L. F.
8 hours ago
5
There's no such thing as a division by zero exception.
– πάντα ῥεῖ
8 hours ago
I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.
– Bathsheba
8 hours ago
2
To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by athrowstatement. Nothing else (unless you're in undefined-behavior land).
– Pete Becker
7 hours ago
1
1
its a matter of definition what should be the result of
0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for– formerlyknownas_463035818
8 hours ago
its a matter of definition what should be the result of
0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for– formerlyknownas_463035818
8 hours ago
2
2
That’s undefined behavior.
– L. F.
8 hours ago
That’s undefined behavior.
– L. F.
8 hours ago
5
5
There's no such thing as a division by zero exception.
– πάντα ῥεῖ
8 hours ago
There's no such thing as a division by zero exception.
– πάντα ῥεῖ
8 hours ago
I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.
– Bathsheba
8 hours ago
I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.
– Bathsheba
8 hours ago
2
2
To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by a
throw statement. Nothing else (unless you're in undefined-behavior land).– Pete Becker
7 hours ago
To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by a
throw statement. Nothing else (unless you're in undefined-behavior land).– Pete Becker
7 hours ago
|
show 6 more comments
2 Answers
2
active
oldest
votes
C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:
- The compiler assumes Undefined Behavior doesn't happen
- Division by Zero in C++ is undefined behavior
- Therefore, code which can cause a Division by Zero is presumed to not do so.
- And, code which must cause a Division by Zero is presumed to never happen
- Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (
d == 0) must not happen - Therefore,
d / dmust always equal 1.
However...
We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.
volatile int d = 0;
d /= d; //What happens?
So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.
Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.
There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.
With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!
– Adrian
7 hours ago
… switching to clang-cl, though, I get a 'random' value ford(nearly always 14684554) but no crash??
– Adrian
7 hours ago
@Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.
– Jesper Juhl
7 hours ago
"Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.
– Bathsheba
7 hours ago
@Jesper Juhl Agree 100%. I'm just surprised, though.
– Adrian
7 hours ago
|
show 3 more comments
The behaviour of integer division by zero is undefined by the C++ standard. It is not required to throw an exception.
(Floating point division by zero is also undefined but IEEE754 defines it.)
Your compiler is optimising d /= d to, effectively d = 1 which is a reasonable choice to make. It's allowed to make this optimisation since it's allowed to assume there is no undefined behaviour in your code - that is d cannot possibly be zero.
1
It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.
– hyde
8 hours ago
I think simply emphasizing "You compiler" would be enough.
– François Andrieux
7 hours ago
When you say it's reasonable for the compiler to assume "that isdcannot possibly be zero," do you also assume that the compiler doesn't see the line:int d = 0;?? :)
– Adrian
7 hours ago
Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.
– user4581301
7 hours ago
@user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.
– Deduplicator
7 hours ago
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
minebv is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57628986%2fwhy-doesnt-d-d-throw-a-division-by-zero-exception%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:
- The compiler assumes Undefined Behavior doesn't happen
- Division by Zero in C++ is undefined behavior
- Therefore, code which can cause a Division by Zero is presumed to not do so.
- And, code which must cause a Division by Zero is presumed to never happen
- Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (
d == 0) must not happen - Therefore,
d / dmust always equal 1.
However...
We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.
volatile int d = 0;
d /= d; //What happens?
So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.
Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.
There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.
With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!
– Adrian
7 hours ago
… switching to clang-cl, though, I get a 'random' value ford(nearly always 14684554) but no crash??
– Adrian
7 hours ago
@Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.
– Jesper Juhl
7 hours ago
"Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.
– Bathsheba
7 hours ago
@Jesper Juhl Agree 100%. I'm just surprised, though.
– Adrian
7 hours ago
|
show 3 more comments
C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:
- The compiler assumes Undefined Behavior doesn't happen
- Division by Zero in C++ is undefined behavior
- Therefore, code which can cause a Division by Zero is presumed to not do so.
- And, code which must cause a Division by Zero is presumed to never happen
- Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (
d == 0) must not happen - Therefore,
d / dmust always equal 1.
However...
We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.
volatile int d = 0;
d /= d; //What happens?
So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.
Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.
There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.
With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!
– Adrian
7 hours ago
… switching to clang-cl, though, I get a 'random' value ford(nearly always 14684554) but no crash??
– Adrian
7 hours ago
@Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.
– Jesper Juhl
7 hours ago
"Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.
– Bathsheba
7 hours ago
@Jesper Juhl Agree 100%. I'm just surprised, though.
– Adrian
7 hours ago
|
show 3 more comments
C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:
- The compiler assumes Undefined Behavior doesn't happen
- Division by Zero in C++ is undefined behavior
- Therefore, code which can cause a Division by Zero is presumed to not do so.
- And, code which must cause a Division by Zero is presumed to never happen
- Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (
d == 0) must not happen - Therefore,
d / dmust always equal 1.
However...
We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.
volatile int d = 0;
d /= d; //What happens?
So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.
Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.
There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.
C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:
- The compiler assumes Undefined Behavior doesn't happen
- Division by Zero in C++ is undefined behavior
- Therefore, code which can cause a Division by Zero is presumed to not do so.
- And, code which must cause a Division by Zero is presumed to never happen
- Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (
d == 0) must not happen - Therefore,
d / dmust always equal 1.
However...
We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.
volatile int d = 0;
d /= d; //What happens?
So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.
Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.
There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.
edited 6 hours ago
answered 7 hours ago
XiremaXirema
15.3k1 gold badge18 silver badges53 bronze badges
15.3k1 gold badge18 silver badges53 bronze badges
With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!
– Adrian
7 hours ago
… switching to clang-cl, though, I get a 'random' value ford(nearly always 14684554) but no crash??
– Adrian
7 hours ago
@Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.
– Jesper Juhl
7 hours ago
"Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.
– Bathsheba
7 hours ago
@Jesper Juhl Agree 100%. I'm just surprised, though.
– Adrian
7 hours ago
|
show 3 more comments
With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!
– Adrian
7 hours ago
… switching to clang-cl, though, I get a 'random' value ford(nearly always 14684554) but no crash??
– Adrian
7 hours ago
@Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.
– Jesper Juhl
7 hours ago
"Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.
– Bathsheba
7 hours ago
@Jesper Juhl Agree 100%. I'm just surprised, though.
– Adrian
7 hours ago
With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!
– Adrian
7 hours ago
With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!
– Adrian
7 hours ago
… switching to clang-cl, though, I get a 'random' value for
d (nearly always 14684554) but no crash??– Adrian
7 hours ago
… switching to clang-cl, though, I get a 'random' value for
d (nearly always 14684554) but no crash??– Adrian
7 hours ago
@Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.
– Jesper Juhl
7 hours ago
@Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.
– Jesper Juhl
7 hours ago
"Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.
– Bathsheba
7 hours ago
"Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.
– Bathsheba
7 hours ago
@Jesper Juhl Agree 100%. I'm just surprised, though.
– Adrian
7 hours ago
@Jesper Juhl Agree 100%. I'm just surprised, though.
– Adrian
7 hours ago
|
show 3 more comments
The behaviour of integer division by zero is undefined by the C++ standard. It is not required to throw an exception.
(Floating point division by zero is also undefined but IEEE754 defines it.)
Your compiler is optimising d /= d to, effectively d = 1 which is a reasonable choice to make. It's allowed to make this optimisation since it's allowed to assume there is no undefined behaviour in your code - that is d cannot possibly be zero.
1
It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.
– hyde
8 hours ago
I think simply emphasizing "You compiler" would be enough.
– François Andrieux
7 hours ago
When you say it's reasonable for the compiler to assume "that isdcannot possibly be zero," do you also assume that the compiler doesn't see the line:int d = 0;?? :)
– Adrian
7 hours ago
Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.
– user4581301
7 hours ago
@user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.
– Deduplicator
7 hours ago
|
show 1 more comment
The behaviour of integer division by zero is undefined by the C++ standard. It is not required to throw an exception.
(Floating point division by zero is also undefined but IEEE754 defines it.)
Your compiler is optimising d /= d to, effectively d = 1 which is a reasonable choice to make. It's allowed to make this optimisation since it's allowed to assume there is no undefined behaviour in your code - that is d cannot possibly be zero.
1
It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.
– hyde
8 hours ago
I think simply emphasizing "You compiler" would be enough.
– François Andrieux
7 hours ago
When you say it's reasonable for the compiler to assume "that isdcannot possibly be zero," do you also assume that the compiler doesn't see the line:int d = 0;?? :)
– Adrian
7 hours ago
Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.
– user4581301
7 hours ago
@user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.
– Deduplicator
7 hours ago
|
show 1 more comment
The behaviour of integer division by zero is undefined by the C++ standard. It is not required to throw an exception.
(Floating point division by zero is also undefined but IEEE754 defines it.)
Your compiler is optimising d /= d to, effectively d = 1 which is a reasonable choice to make. It's allowed to make this optimisation since it's allowed to assume there is no undefined behaviour in your code - that is d cannot possibly be zero.
The behaviour of integer division by zero is undefined by the C++ standard. It is not required to throw an exception.
(Floating point division by zero is also undefined but IEEE754 defines it.)
Your compiler is optimising d /= d to, effectively d = 1 which is a reasonable choice to make. It's allowed to make this optimisation since it's allowed to assume there is no undefined behaviour in your code - that is d cannot possibly be zero.
edited 7 hours ago
answered 8 hours ago
BathshebaBathsheba
191k28 gold badges288 silver badges408 bronze badges
191k28 gold badges288 silver badges408 bronze badges
1
It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.
– hyde
8 hours ago
I think simply emphasizing "You compiler" would be enough.
– François Andrieux
7 hours ago
When you say it's reasonable for the compiler to assume "that isdcannot possibly be zero," do you also assume that the compiler doesn't see the line:int d = 0;?? :)
– Adrian
7 hours ago
Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.
– user4581301
7 hours ago
@user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.
– Deduplicator
7 hours ago
|
show 1 more comment
1
It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.
– hyde
8 hours ago
I think simply emphasizing "You compiler" would be enough.
– François Andrieux
7 hours ago
When you say it's reasonable for the compiler to assume "that isdcannot possibly be zero," do you also assume that the compiler doesn't see the line:int d = 0;?? :)
– Adrian
7 hours ago
Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.
– user4581301
7 hours ago
@user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.
– Deduplicator
7 hours ago
1
1
It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.
– hyde
8 hours ago
It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.
– hyde
8 hours ago
I think simply emphasizing "You compiler" would be enough.
– François Andrieux
7 hours ago
I think simply emphasizing "You compiler" would be enough.
– François Andrieux
7 hours ago
When you say it's reasonable for the compiler to assume "that is
d cannot possibly be zero," do you also assume that the compiler doesn't see the line: int d = 0; ?? :)– Adrian
7 hours ago
When you say it's reasonable for the compiler to assume "that is
d cannot possibly be zero," do you also assume that the compiler doesn't see the line: int d = 0; ?? :)– Adrian
7 hours ago
Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.
– user4581301
7 hours ago
Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.
– user4581301
7 hours ago
@user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.
– Deduplicator
7 hours ago
@user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.
– Deduplicator
7 hours ago
|
show 1 more comment
minebv is a new contributor. Be nice, and check out our Code of Conduct.
minebv is a new contributor. Be nice, and check out our Code of Conduct.
minebv is a new contributor. Be nice, and check out our Code of Conduct.
minebv is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57628986%2fwhy-doesnt-d-d-throw-a-division-by-zero-exception%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
its a matter of definition what should be the result of
0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for– formerlyknownas_463035818
8 hours ago
2
That’s undefined behavior.
– L. F.
8 hours ago
5
There's no such thing as a division by zero exception.
– πάντα ῥεῖ
8 hours ago
I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.
– Bathsheba
8 hours ago
2
To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by a
throwstatement. Nothing else (unless you're in undefined-behavior land).– Pete Becker
7 hours ago