c++ 中浮點數取余


運算符% a一般用於整形之間使用,而對於浮點數需要使用函數

fmodf用於float型變量操作

fmod用於double型變量操作

modl用於long double型變量操作

 

這里使用fmod(), 下面示例從鍵盤獲得一個整形范圍的數並且需要輸入 整形數據

#include <iostream>                                                              
#include <climits>
#include <cmath>
using namespace std;

bool is_int(double x) 
{
    if (x <= INT_MAX && x >= INT_MIN && fmod(x,1.0) == 0) 
        return true;
    else return false;
}

int main()
{
    double num;
    cout << "Yo , dude ! Enter as integer value: ";
    cin >> num;
    while (!is_int(num))
    {  
        cout << "Out of range -- please try again: ";
        cin >> num;
    }  

    int val = int (num);
    cout << "you've entered the integer " << val << "\nBye\n";

    return 0; 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM