The remainder function %, remainder, fmod in C++ and the remainder function mod in matlab

C++

1 integer remainder

%

2 remainder function

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 function

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

Self-made remainder and 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");
}

output result
insert image description here

mod function in matlab

When it comes to the problem of positive and negative numbers, it is different from the remainder and fmod in C++, and the principle will be written later.

Guess you like

Origin blog.csdn.net/gsgbgxp/article/details/130789162