fmod(n,d)函数详解

首页 / C++入门教程 / fmod(n,d)函数详解

该函数找到分子/分母的浮点余数并四舍五入为零。

fmod - 公式:

fmod= numerator - t*denominator

其中" t"是分子/分母的截断值。

fmod - 语法

考虑一个分子“ n”和分母“ d”。语法为:

double fmod(double n,double d);

fmod - 参数

n :分子的值。

d :分母的值

fmod - 返回值

它返回n/d的浮点余数。

Note: 如果分母的值为零,则fmod()函数将返回NAN(Not a Number)。

fmod - 例子1

让我们看一个具有相同类型参数的简单示例。

#include <iostream>  
#include<math.h>  
using namespace std;  
int main()  
{  
    double n=4.2;  
    double d=7.8;  
    std::cout << "The values of numerator and denominator are :" <<n<<" , "<< d<<     std::endl;  
    std::cout << "fmod of these values is :"<<fmod(n,d) <<std::endl;  
    return 0;  
}  

输出:

The values of numerator and denominator are :4.2 , 7.8
fmod of these values is :4.2

fmod - 例子2

让我们看一下具有不同类型参数的简单示例。

#include <iostream>  
#include<math.h>  
using namespace std;  
int main()  
{  
    float n=7.8;  
    int d=9;  
    std::cout << "The values of numerator and denominator are :" <<n<<" , "<< d<< std::endl;  
    std::cout << "fmod of these values is :"<<fmod(n,d) <<std::endl;  
    return 0;  
}  

输出:

The values of numerator and denominator are :7.8 , 9
fmod of these values is :7.8

fmod - 例子3

让我们看一个简单的例子,当分母的值为零时。

#include <iostream>  
#include<math.h>  
using namespace std;  
int main()  
{  
    float n=16.7;  
    int d=0;  
    std::cout << "The values of numerator and denominator are :" <<n<<" , "<< d<< std::endl;  
    std::cout << "fmod of these values is :"<<fmod(n,d) <<std::endl;  
    return 0;  
}  

输出:

The values of numerator and denominator are :16.7 , 0
fmod of these values is :-nan

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学微服务 -〔胡忠想〕

小马哥讲Spring核心编程思想 -〔小马哥〕

分布式协议与算法实战 -〔韩健〕

手机摄影 -〔@随你们去〕

跟着高手学复盘 -〔张鹏〕

讲好故事 -〔涵柏〕

大数据经典论文解读 -〔徐文浩〕

搞定音频技术 -〔冯建元 〕

Kubernetes入门实战课 -〔罗剑锋〕

好记忆不如烂笔头。留下您的足迹吧 :)