How to check if a double variable contains an integer and not a floating point in C++

1 Answer

0 votes
#include <cmath>
#include <iostream>

int main() {
    double d1 = 45983.0;
    double d2 = 876.109;
    double intpart;

    if (std::modf(d1, &intpart) == 0.0) {
        std::cout << "yes" << std::endl;
    } else {
        std::cout << "no" << std::endl;
    }

    if (std::modf(d2, &intpart) == 0.0) {
        std::cout << "yes" << std::endl;
    } else {
        std::cout << "no" << std::endl;
    }
}



/*
run:

yes
no

*/

 



answered Jan 29, 2024 by avibootz
...