#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.
*/