in C++, I wonder how to get the correct answer 0.5 for "INT_MAX / (INT_MAX + INT_MAX)"? I tried cast the divisor/both divisor and dividend to long, and also cast divisor to double, all return -1073741823. Could anyone give me some advice ? Thanks.
Asked
Active
Viewed 400 times
-4
-
`0.5` is a `double` so you need floating point division. Also `INT_MAX + INT_MAX` is going to give you a wrong result. – Richard Critten Mar 14 '18 at 17:55
-
multiply one of the terms on the denominator by 1.0 to force argument promotion. – Bathsheba Mar 14 '18 at 17:55
-
7`double correct_answer = 0.5;` – StoryTeller - Unslander Monica Mar 14 '18 at 17:56
-
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve] – 463035818_is_not_an_ai Mar 14 '18 at 17:56
-
1@storyteller: constexpr surely? ;-) – Bathsheba Mar 14 '18 at 17:57
-
2@Bathsheba - You know me, I don't like to spoon feed the OP :P – StoryTeller - Unslander Monica Mar 14 '18 at 17:57
-
It's just an example, here I just want to know how to get the right result of the division (a / 2a = 0.5) when both divisor (a) and dividend (2a) are very large. – Eric Wong Mar 14 '18 at 18:02
-
@EricWong Why? It will **always** be `.5`. – NathanOliver Mar 14 '18 at 18:04
-
examples are fine, but just an example without code isnt. Plese take the 2 minutes to write 2 lines for a [mcve]. If you put casts in the right place it should work, but without seeing your code we cannot know what is wrong – 463035818_is_not_an_ai Mar 14 '18 at 18:04
-
1@EricWong - Apologies if I offended you. I was only jesting. Though you should have provided those details when originally posting. If your question was "How do protect against possible overflow for this equation" it would have been a *good question*! Instead you phrased it in a way that left room for a silly interpretation, like mine. – StoryTeller - Unslander Monica Mar 14 '18 at 18:04
-
@StoryTeller Thanks for the advice, I am a newbie to stackoverflow so I don't know much about the rules. – Eric Wong Mar 14 '18 at 18:15
-
@EricWong - Don't worry about it. You can take the [tour] to learn about the Stack Overflow model. We have a pretty good [help], and it even has a great set of articles about [ask] good questions. – StoryTeller - Unslander Monica Mar 14 '18 at 18:19
1 Answers
0
What you are trying is impossible for 2 reasons.
First, INT_MAX + INT_MAX
cannot be represented by an int
, which is logical since the maximum value of an int
is INT_MAX
. This leads to an overflow.
Second, even if you try to do (3 / (3 + 3))
you will get 0 since you are working with integers and not fractional numbers. Do do what you want to do you can try:
static_cast<double>(INT_MAX) / (static_cast<double>(INT_MAX) + static_cast<double>(INT_MAX))
though this has no interest.

Benjamin Barrois
- 2,566
- 13
- 30
-
2You actually only need one `static_cast
` if you are careful where you put it – Justin Mar 14 '18 at 18:02 -
@Justin Correct as far as the language is concerned, but for human readers, I'd probably leave in at least the cast of the first `INT_MAX`. – Mar 14 '18 at 18:08
-
4Then why answer if it's nonsense? Conserve your effort for decent questions. If you do decide to answer however, give it your best effort. – StoryTeller - Unslander Monica Mar 14 '18 at 18:08
-
i second storyteller, by answering a question that you consider as low quality you actually decrease the chances that it will be improved (dont want to point at someone, happens to myself often enough, just saying...) – 463035818_is_not_an_ai Mar 14 '18 at 18:10
-
@StoryTeller: If I had answered "do `INT_MAX / (static_cast
(INT_MAX) + INT_MAX)`" do you think it would have helped the OP? And there is absolutely no difference nor for the compiler neither for the execution, so what's the point? – Benjamin Barrois Mar 14 '18 at 18:13 -
Actually, it would have helped the OP, since it makes the code much more readable. Since code is read mostly by humans, it should be as readable as possible. And if you even included a word about how the clever little cast makes everything work, I would have even considered your answer useful (the criteria for voting). You don't want to accept the feedback? Then don't. It's your answer, after all. – StoryTeller - Unslander Monica Mar 14 '18 at 18:17
-
I accept it but it's harder from someone who answered `double correct_answer = 0.5;` and who talks to me about helping the OP... – Benjamin Barrois Mar 14 '18 at 18:38
-
Answered? I didn't "answer" anything, I joked with the OP. And when there was a hint of offence on their part, I apologized. Given your entire attitude here, and that blatant ad-hominem, I'm really wasting my keystrokes. – StoryTeller - Unslander Monica Mar 14 '18 at 21:33
-
Sure. You blame me for not "conserving my effort for decent question" while you spend it joking on a non-humouristic website and blaming the ones just trying to help. So yes, wasting your keystrokes seems to be quite a habit... – Benjamin Barrois Mar 14 '18 at 21:59