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

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

技术教程推荐

机器学习40讲 -〔王天一〕

Nginx核心知识150讲 -〔陶辉〕

编辑训练营 -〔总编室〕

Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕

ZooKeeper实战与源码剖析 -〔么敬国〕

Spark核心原理与实战 -〔王磊〕

大厂晋升指南 -〔李运华〕

etcd实战课 -〔唐聪〕

高楼的性能工程实战课 -〔高楼〕

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