LearnFrenzy
Sign in Take a free test

Floating Point Issues · Predict the Output · Question 7

Arithmetic with infinity – output Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

What is the output of the following C program?

main.c
#include <math.h>
#include <stdio.h>

int main(void) {
    double inf = INFINITY;
    printf("%f %f %d\n", inf + 1, -inf * 2, isnan(inf - inf) != 0);
    return 0;
}
  • A inf -inf 1 Correct
  • B inf -inf 0
  • C inf inf 1
  • D nan nan 1
Correct Answer: Option A

Infinity absorbs finite values and keeps its sign through multiplication. But ∞ − ∞ has no meaningful answer, so it gives NaN.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links