Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

# What is isfinite() in C? Kainat Asif

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The isfinite() function in C identifies if a number is finite. The declaration of isfinite() is shown below:

int isfinite(float num);


OR:

int isfinite(double num);


OR:

int isfinite(long double num);


## Parameter

• num: the number checked to see if it is finite

## Return value

The isfinite() function returns 1 if the number passed as a parameter is finite, and returns 0 otherwise.

## Library

To use the isfinite() function, include the following library:

#include <math.h>


## Example

Consider the code snippet below, which demonstrates the use of the isfinite() function:

#include<stdio.h>#include<math.h>int main() {  float num1 = 1.0;  float num2 = 0.0;  int num3 = isfinite(num1/num2);  if(num3==1){    printf("%f / %f is finite \n", num1, num2);  }else if(num3==0){    printf("%f / %f is infinite \n", num1, num2);  }  int num4 = isfinite(num2/num1);  if(num4==1){    printf("%f / %f is finite \n", num2, num1);  }else if(num4==0){    printf("%f / %f is infinite \n", num2, num1);  }  int num5 = isfinite(INFINITY);  if(num5==1){    printf("INFINITY is finite \n");  }else if(num5==0){    printf("INFINITY is infinite \n");  }  return 0;}

## Explanation

• The isfinite() function is used in line 8 to show that a division by zero is not finite.
• The isfinite() function is used in line 15 and a simple finite division is performed. The isfinite() function returns 0, as the division is finite.
• The isfinite() function is used in line 22 to show that infinity is not finite.

RELATED TAGS

c

CONTRIBUTOR Kainat Asif 