C++中的取余函数%、remainder、fmod以及matlab中的取余函数mod

news/2025/2/19 4:45:47/

C++

1 整数取余

%

2 remainder函数

https://cplusplus.com/reference/cmath/remainder/?kw=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.

Example

/* 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;
}

Output:

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.

Example

/* 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;
}

Output:

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

自编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");
}

输出结果
在这里插入图片描述

matlab中的mod函数

涉及到正负数问题时和C++中的remainder以及fmod都不一样,原理之后再写。


http://www.ppmy.cn/news/76411.html

相关文章

【JavaSE】Java基础语法(一)

文章目录 1. ⛄常量2. ⛄数据类型2.1 &#x1f320;&#x1f320;计算机存储单元2.2 &#x1f320;&#x1f320;Java 中的数据类型 3. ⛄变量的注意事项4. ⛄键盘录入5. ⛄标识符 1. ⛄常量 常量&#xff1a;在程序运行过程中&#xff0c;其值不可以发生改变的量。 Java中的常…

【Linux】初识优雅的Linux编辑器——Vim

❤️前言 大家好&#xff01;今天给大家带来的博客内容是关于Linux操作系统下的一款多模式文本编辑器Vim。本文将和大家一起来了解Vim编辑器的一些基础知识。 正文 Vim是一个多模式的文本编辑器(一共有十二种模式)&#xff0c;其中我们当我们初学Vim时主要了解如下三种工作模式…

__weak类型函数

前言 今天无意之间看到了一个__weak类型函数&#xff0c;之前没有了解过&#xff0c;就查了一下资料。现在分享一下。 __weak是什么 含义解释 &#xff08;1&#xff09;其实这个用白话很好理解&#xff1a;__weak 表示弱定义&#xff0c;表示如果你自己定义了同名的函数就不用…

SCMA基本原理介绍

SCMA: Sparse Code Multiple Access SCMA基本原理 我们考虑一个同步&#xff08;synchronous&#xff09;的SCMA系统&#xff0c; 含1个基站&#xff08;Base Station, BS&#xff09;&#xff1b; J J J个用户&#xff08;so called layers&#xff09;&#xff1b;K个OFDM…

Javaweb概念

什么是javaweb JavaWeb是指&#xff0c;所有通过Java语言编写可以通过浏览器访问的程序的总称&#xff0c;叫JavaWeb。 JavaWeb是基于请求和响应来开发的。 什么是请求 请求是指客户端给服务器发送数据&#xff0c;叫请求Request 什么是响应 响应是指服务器给客户端回传数据&…

Linux Ubuntu配置CPU与GPU版本tensorflow库的方法

本文介绍在Linux操作系统的发行版本Ubuntu中&#xff0c;配置可以用CPU或GPU运行的Python新版本深度学习库tensorflow的方法。 在文章Anaconda配置Python新版本tensorflow库&#xff08;CPU、GPU通用&#xff09;的方法&#xff08;https://blog.csdn.net/zhebushibiaoshifu/ar…

ChatGPT可能马上取代你!ChatGPT能做什么?

文章目录 前言1.客服机器人2.智能助手3.内部沟通4.个性化推荐5.语音交互6.教育培训7.医疗健康8.社交娱乐9.营销推广10.情感分析11.舆情监测12.知识管理13.金融服务14.物联网15.公共服务16.智能家居17.自动化办公18.交通出行19.游戏娱乐20.智慧城市21.决策支持22.人才招聘23.版权…

数据仓库漫谈-前世今生

数据仓库的内容非常多&#xff0c;每一个子模块拎出来都能讲很久。这里没法讲太多细节&#xff0c;大致思考了三个备选议题&#xff1a; 数据仓库的前世今生 数据仓库体系知识介绍 数仓开发者的路在何方&#xff1f; 既然是第一次分享&#xff0c;感觉还是跟大家普及下数仓的…