当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ fmod()用法及代码示例


C++ floor() 函数

fmod() 函数是 cmath 头文件的库函数,用于求除法的余数,它接受两个数字(分子和分母)并返回分子/分母的浮点余数,该余数向零四舍五入。

fmod() 函数的语法:

    fmod(n, m);

参数: n,m– 是用于计算除法余数的数字(分子、分母)。

返回值: double– 它返回 double 类型的值,该值是除法的浮点余数。

例:

    Input:
    float n = 5.3;
    float m = 2;
    
    Function call:
    fmod(n,m);
    
    Output:
    1.3

C++代码演示fmod()函数的例子

// C++ code to demonstrate the example of 
// fmod() function

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

// main() section
int main()
{
    float n;
    float m;
    float result;
    
    //input the value of 
    //numerator and denominator
    cout<<"Enter the value of numerator :";
    cin>>n;
    cout<<"Enter the value of denominator:";
    cin>>m;
    
    //finding the remainder
    result = fmod(n,m);
    cout<<"floating-point remainder of "<<n<<"/"<<m<<" is:";
    cout<<result<<endl;

    return 0;
}

输出

Enter the value of numerator:5.3
Enter the value of denominator:2
floating-point remainder of 5.3/2 is:1.3

Second run:
Enter the value of numerator:18.5 
Enter the value of denominator:4.2
floating-point remainder of 18.5/4.2 is:1.7

Third run:
Enter the value of numerator:-36.23 
Enter the value of denominator:24.1 
floating-point remainder of -36.23/24.1 is:-12.13 

Fourth run:
Enter the value of numerator:36.23
Enter the value of denominator:-24.1
floating-point remainder of 36.23/-24.1 is:12.13


相关用法


注:本文由纯净天空筛选整理自 fmod() function with example in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。