-4

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.

Eric Wong
  • 3
  • 6

1 Answers1

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
  • 2
    You 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
  • 4
    Then 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