How to check whether a floating-point variable is an integer (has no fractional part) in C

5 Answers

0 votes
#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool is_integer(double x) {
    double intpart;
    
    return modf(x, &intpart) == 0.0;
}

int main() {
    double d1 = 982317.0;
    double d2 = 345.98;
     
    if (is_integer(d1)) {
        puts("yes");
    } else {
        puts("no");
    }
     
     if (is_integer(d2)) {
        puts("yes");
    } else {
        puts("no");
    }
 
    return 0;
}
 
 
/*
run:
 
yes
no
 
*/

 



answered Jan 29, 2024 by avibootz
edited Sep 10 by avibootz
0 votes
#include <stdio.h>
#include <stdbool.h>

bool is_integer(double x) {
    return x == (long long)x;
}

int main() {
    double x = 982317.0;
 
    if (is_integer(x)) {
        puts("yes");
    } else {
        puts("no");
    }
 
    return 0;
}
 
 
 
/*
run:
 
yes
 
*/

 



answered Sep 10 by avibootz
edited Sep 10 by avibootz
0 votes
#include <stdio.h>
#include <math.h>

int main() {
    double x = 982317.0;

    if (floor(x) == x) {
        puts("yes");
    } else {
        puts("no");
    }

    return 0;
}


/*
run:

yes

*/

 



answered Sep 10 by avibootz
0 votes
#include <stdio.h>
#include <math.h>

int main() {
    double x = 982317.0;

    if (ceil(x) == x) {
        puts("yes");
    } else {
        puts("no");
    }

    return 0;
}


/*
run:

yes

*/

 



answered Sep 10 by avibootz
0 votes
#include <stdio.h>
#include <math.h>   // floor, fabs

// -----------------------------------------------------------------------------
// Determines whether a double contains an integer (no fractional part).
// Uses a small tolerance to safely handle floating‑point precision issues.
// -----------------------------------------------------------------------------
int is_integer(double value)
{
    const double epsilon = 1e-12;

    // floor(value) gives the largest integer <= value
    double floored = floor(value);

    // If the difference is extremely small, treat the number as an integer
    return fabs(value - floored) < epsilon;
}

// -----------------------------------------------------------------------------
// Prints a descriptive message for the given double.
// -----------------------------------------------------------------------------
void print_check(double value)
{
    printf("Checking value: %.15f\n", value);

    if (is_integer(value))
        printf("Result: The value contains an integer.\n\n");
    else
        printf("Result: The value contains a floating‑point number.\n\n");
}

// -----------------------------------------------------------------------------
// Entry point demonstrating several example values.
// -----------------------------------------------------------------------------
int main(void)
{
    // Example input values
    double a = 42.0;                 // integer-like
    double b = 42.00000000001;       // floating-point due to tiny fraction
    double c = 89.0000000000001;     // integer-like
    double d = -7.0;                 // negative integer-like
    double e = 3.14;                 // floating-point

    // Run checks
    print_check(a);
    print_check(b);
    print_check(c);
    print_check(d);
    print_check(e);

    return 0;
}


/*
run:

Checking value: 42.000000000000000
Result: The value contains an integer.

Checking value: 42.000000000009997
Result: The value contains a floating‑point number.

Checking value: 89.000000000000099
Result: The value contains an integer.

Checking value: -7.000000000000000
Result: The value contains an integer.

Checking value: 3.140000000000000
Result: The value contains a floating‑point number.

*/

 



answered Sep 10 by avibootz

Related questions

...