C++ Math fmod()函数

C++ Math fmod()函数

该函数用于计算numerator/denominator的浮点余数,并向零舍入。

fmod的公式:

fmod= numerator - t*denominator

当 ‘t’ 是分子/分母的截断值时。

语法

假设有一个分子 ‘n’ 和一个分母 ‘d’。语法应为:

double fmod(double n,double d);

参数

n :分子的值。

d :分母的值。

返回值

返还n/d的浮点余数。

注意:如果分母的值为零,那么fmod()函数将返回NAN(不是数字)。

示例1

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

#include 
#include
using namespace std;
int main()
{
    double n=4.2;
    double d=7.8;
std::cout << "The values of numerator and denominator are :" <

输出:

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

示例2

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

#include 
#include
using namespace std;
int main()
{
    float n=7.8;
    int d=9;
std::cout << "The values of numerator and denominator are :" <

输出:

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

示例3

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

#include 
#include
using namespace std;
int main()
{
    float n=16.7;
    int d=0;
std::cout << "The values of numerator and denominator are :" <

输出:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程