C++中的取余函数remainder与fmod详解

 更新时间:2023年05月25日 11:12:33   作者:gsgbgxp  
这篇文章主要为大家详细介绍了C++中的取余函数remainder、fmod的具体使用以及自编的remainder及fmod,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习学习

1.整数取余

%

2.remainder函数

用法:

double remainder (double numer , double denom);
float remainder (float numer , float denom);
long double remainder (long double numer, long double denom);
double remainder (Type1 numer , Type2 denom); // additional overloads

Returns the floating-point remainder of numer/denom (rounded to nearest):

remainder = numer - rquot * denom

Where rquot is the result of: numer/denom, rounded toward the nearest integral value (with halfway cases rounded toward the even number).

A similar function, fmod, returns the same but with the quotient truncated (rounded towards zero) instead.

The function remquo has a behavior identical to this function, but it additionally provides access to the intermediate quotient value used.

Additional overloads are provided in this header () for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).

Parameters

numer: Value of the quotient numerator.
denom: Value of the quotient denominator.

Return Value

The remainder of dividing the arguments.
If this remainder is zero, its sign shall be that of numer.
If denom is zero, the function may either return zero or cause a domain error (depending on the library implementation).

If a domain error occurs:

And math_errhandling has MATH_ERRNO set: the global variable errno is set to EDOM.
And math_errhandling has MATH_ERREXCEPT set: FE_INVALID is raised.

示例:

/* remainder example */
#include <stdio.h>      /* printf */
#include <math.h>       /* remainder */
int main ()
{
  printf ( "remainder of 5.3 / 2 is %f\n", remainder (5.3,2) );
  printf ( "remainder of 18.5 / 4.2 is %f\n", remainder (18.5,4.2) );
  return 0;
}

输出:

remainder of 5.3 / 2 is -0.700000
remainder of 18.5 / 4.2 is 1.700000

3.fmod函数

https://cplusplus.com/reference/cmath/fmod/?kw=fmod

用法:

double fmod (double numer , double denom); float fmod (float numer , float denom);long double fmod (long double numer, long double denom); double fmod (Type1 numer , Type2 denom); // additional overloads

Compute remainder of division

Returns the floating-point remainder of numer/denom (rounded towards zero):

fmod = numer - tquot * denom

Where tquot is the truncated (i.e., rounded towards zero) result of: numer/denom.

A similar function, remainder, returns the same but with the quotient rounded to the nearest integer (instead of truncated).

Additional overloads are provided in this header () for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).

Parameters

numer Value of the quotient numerator. denom Value of the quotient
denominator.

Return Value

The remainder of dividing the arguments.
If denom is zero, the function may either return zero or cause a domain error (depending on the library implementation).

C90 (C++98)C99 (C+11)

If a domain error occurs:

  • And math_errhandling has MATH_ERRNO set: the global variable errno is set to EDOM.
  • And math_errhandling has MATH_ERREXCEPT set: FE_INVALID is raised.

示例:

/* fmod example */
#include <stdio.h>      /* printf */
#include <math.h>       /* fmod */
int main ()
{
  printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3,2) );
  printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5,4.2) );
  return 0;
}

输出:

fmod of 5.3 / 2 is 1.300000
fmod of 18.5 / 4.2 is 1.700000

4.自编remainder及fmod

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
//VS2010不提供remainder这个函数,自己实现
//http://www.cplusplus.com/reference/cmath/remainder/
//Returns the floating - point remainder of numer / denom(rounded to nearest) :
//remainder = numer - rquot * denom
//Where rquot is the result of : numer / denom, rounded toward the nearest integral value(with halfway cases rounded toward the even number).
//A similar function, fmod, returns the same but with the quotient truncated(rounded towards zero) instead.
#define ROUND(d)  int(d + 0.5)//四舍五入
double myremainder(double numer, double denom)
{
	//如果一正一负,这个函数结果可能不对
	int rquot = ROUND(numer / denom);//remainder和fmod的区别就是要不要四舍五入
	double remainder = numer - rquot * denom;
	return remainder;
}
double myfmod(double x, double y)
{
	return x - (int)(x / y) * y;
}
int main()
{
	double x, y;
	x = 14.3;
	y = 3.0;
	printf("x = %f, y = %f\n", x, y);
	printf("remainder(x, y) = %f\n", remainder(x, y)); // 计算余数,四舍五入到最接近的整数
	printf("myremainder(x, y) = %f\n", myremainder(x, y)); // 计算余数,四舍五入到最接近的整数
	printf("fmod(x, y) = %f\n", fmod(x, y)); // 计算余数,向零取整
	printf("myfmod(x, y) = %f\n", myfmod(x, y)); // 计算余数,向零取整
	x = -14.3;
	y = 3.0;
	printf("x = %f, y = %f\n", x, y);
	printf("remainder(x, y) = %f\n", remainder(x, y)); // 计算余数,四舍五入到最接近的整数
	printf("myremainder(x, y) = %f\n", myremainder(x, y)); // 计算余数,四舍五入到最接近的整数
	printf("fmod(x, y) = %f\n", fmod(x, y)); // 计算余数,向零取整
	printf("myfmod(x, y) = %f\n", myfmod(x, y)); // 计算余数,向零取整
	x = 14.3;
	y = -3.0;
	printf("x = %f, y = %f\n", x, y);
	printf("remainder(x, y) = %f\n", remainder(x, y)); // 计算余数,四舍五入到最接近的整数
	printf("myremainder(x, y) = %f\n", myremainder(x, y)); // 计算余数,四舍五入到最接近的整数
	printf("fmod(x, y) = %f\n", fmod(x, y)); // 计算余数,向零取整
	printf("myfmod(x, y) = %f\n", myfmod(x, y)); // 计算余数,向零取整
	x = -14.3;
	y = -3.0;
	printf("x = %f, y = %f\n", x, y);
	printf("remainder(x, y) = %f\n", remainder(x, y)); // 计算余数,四舍五入到最接近的整数
	printf("myremainder(x, y) = %f\n", myremainder(x, y)); // 计算余数,四舍五入到最接近的整数
	printf("fmod(x, y) = %f\n", fmod(x, y)); // 计算余数,向零取整
	printf("myfmod(x, y) = %f\n", myfmod(x, y)); // 计算余数,向零取整
	system("pause");
}

输出结果

到此这篇关于C++中的取余函数remainder与fmod详解的文章就介绍到这了,更多相关C++取余内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++中Copy-Swap实现拷贝交换

    C++中Copy-Swap实现拷贝交换

    本文主要介绍了C++中Copy-Swap实现拷贝交换,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • Clion-MinGW编译后的exe文件添加ico图标的操作方法

    Clion-MinGW编译后的exe文件添加ico图标的操作方法

    这篇文章主要介绍了Clion-MinGW编译后的exe文件添加ico图标的操作方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • C++之list容器介绍及使用方式

    C++之list容器介绍及使用方式

    这篇文章主要介绍了C++之list容器介绍及使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • C++解析ini文件的实现方法

    C++解析ini文件的实现方法

    在C++编程中,有时我们需要处理配置文件来存储应用程序的设置和参数,而INI文件是一种常见的选择,这篇文章主要给大家介绍了关于C++解析ini文件的实现方法,需要的朋友可以参考下
    2024-08-08
  • 定义vim配置文件vimrc用于c/c++编程

    定义vim配置文件vimrc用于c/c++编程

    vim作为Linux下广受赞誉的代码编辑器,其独特的纯命令行操作模式可以很大程度上方便编程工作,通过自定义vim配置文件可以实现对vim功能的个性化设置。这篇文章主要介绍了定义vim配置文件vimrc,用于c/c++编程 ,需要的朋友可以参考下
    2018-10-10
  • C++ JSON库nlohmann指南

    C++ JSON库nlohmann指南

    本文详细介绍了C++中流行的JSON库nlohmann的功能与使用方法,包括声明与构造JSON对象、数组,解析与序列化JSON数据,以及如何进行元素的获取、修改、删除等常见操作,感兴趣的朋友跟随小编一起看看吧
    2025-10-10
  • C语言 解压华为固件的实例代码

    C语言 解压华为固件的实例代码

    这是解压华为固件(update.app)的C语言。。其实这也是我翻115翻出来的。。。
    2013-08-08
  • C++实现LeetCode(91.解码方法)

    C++实现LeetCode(91.解码方法)

    这篇文章主要介绍了C++实现LeetCode(91.解码方法),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C语言中使用fopen()打开和操作文件的详细方法指南

    C语言中使用fopen()打开和操作文件的详细方法指南

    fopen是C语言库函数,open是系统调用,mmap是将大文件映射到内存中使用,这篇文章主要给大家介绍了关于C语言中使用fopen()打开和操作文件的详细方法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-09-09
  • 基于C++编写一个键盘提示音程序

    基于C++编写一个键盘提示音程序

    首先讲一下思路,这次制作的小黑子相当于键盘提示音,输入J,N,T,M,会发出“鸡你太美”的声音,连续按下JNTM则会发出“你干嘛啊,哎呦”的声音,感兴趣的可以了解一下
    2023-03-03

最新评论