欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中的取余函數(shù)remainder與fmod詳解

 更新時間:2023年05月25日 11:12:33   作者:gsgbgxp  
這篇文章主要為大家詳細介紹了C++中的取余函數(shù)remainder、fmod的具體使用以及自編的remainder及fmod,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)

1.整數(shù)取余

%

2.remainder函數(shù)

用法:

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函數(shù)

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這個函數(shù),自己實現(xiàn)
//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)
{
	//如果一正一負(fù),這個函數(shù)結(jié)果可能不對
	int rquot = ROUND(numer / denom);//remainder和fmod的區(qū)別就是要不要四舍五入
	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)); // 計算余數(shù),四舍五入到最接近的整數(shù)
	printf("myremainder(x, y) = %f\n", myremainder(x, y)); // 計算余數(shù),四舍五入到最接近的整數(shù)
	printf("fmod(x, y) = %f\n", fmod(x, y)); // 計算余數(shù),向零取整
	printf("myfmod(x, y) = %f\n", myfmod(x, y)); // 計算余數(shù),向零取整
	x = -14.3;
	y = 3.0;
	printf("x = %f, y = %f\n", x, y);
	printf("remainder(x, y) = %f\n", remainder(x, y)); // 計算余數(shù),四舍五入到最接近的整數(shù)
	printf("myremainder(x, y) = %f\n", myremainder(x, y)); // 計算余數(shù),四舍五入到最接近的整數(shù)
	printf("fmod(x, y) = %f\n", fmod(x, y)); // 計算余數(shù),向零取整
	printf("myfmod(x, y) = %f\n", myfmod(x, y)); // 計算余數(shù),向零取整
	x = 14.3;
	y = -3.0;
	printf("x = %f, y = %f\n", x, y);
	printf("remainder(x, y) = %f\n", remainder(x, y)); // 計算余數(shù),四舍五入到最接近的整數(shù)
	printf("myremainder(x, y) = %f\n", myremainder(x, y)); // 計算余數(shù),四舍五入到最接近的整數(shù)
	printf("fmod(x, y) = %f\n", fmod(x, y)); // 計算余數(shù),向零取整
	printf("myfmod(x, y) = %f\n", myfmod(x, y)); // 計算余數(shù),向零取整
	x = -14.3;
	y = -3.0;
	printf("x = %f, y = %f\n", x, y);
	printf("remainder(x, y) = %f\n", remainder(x, y)); // 計算余數(shù),四舍五入到最接近的整數(shù)
	printf("myremainder(x, y) = %f\n", myremainder(x, y)); // 計算余數(shù),四舍五入到最接近的整數(shù)
	printf("fmod(x, y) = %f\n", fmod(x, y)); // 計算余數(shù),向零取整
	printf("myfmod(x, y) = %f\n", myfmod(x, y)); // 計算余數(shù),向零取整
	system("pause");
}

輸出結(jié)果

到此這篇關(guān)于C++中的取余函數(shù)remainder與fmod詳解的文章就介紹到這了,更多相關(guān)C++取余內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 帶你粗略了解C++回文鏈表

    帶你粗略了解C++回文鏈表

    這篇文章主要介紹了Python實現(xiàn)的判斷回文鏈表算法,結(jié)合實例形式分析了Python針對鏈表是否為回文鏈表進行判斷的相關(guān)算法實現(xiàn)技巧,需要的朋友可以參考下
    2021-08-08
  • C++ 空指針解引用的解決方法

    C++ 空指針解引用的解決方法

    空指針解引用是一種常見且嚴(yán)重的錯誤,它通常由于指針未初始化、被設(shè)置為nullptr或指向無效地址引起,本文主要介紹了C++ 空指針解引用的解決方法,感興趣的可以了解一下
    2024-08-08
  • C語言實現(xiàn)簡易通訊錄(靜態(tài)版本)的代碼分享

    C語言實現(xiàn)簡易通訊錄(靜態(tài)版本)的代碼分享

    這篇文章主要為大家詳細介紹了如何錄音C語言實現(xiàn)一個簡易的通訊錄(靜態(tài)版本),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-10-10
  • C語言?超詳細講解算法的時間復(fù)雜度和空間復(fù)雜度

    C語言?超詳細講解算法的時間復(fù)雜度和空間復(fù)雜度

    算法復(fù)雜度分為時間復(fù)雜度和空間復(fù)雜度。其作用:?時間復(fù)雜度是度量算法執(zhí)行的時間長短;而空間復(fù)雜度是度量算法所需存儲空間的大小
    2022-03-03
  • 一篇文章帶你了解C語言指針進階

    一篇文章帶你了解C語言指針進階

    這篇文章主要介紹了C語言指針詳解及用法示例,介紹了其相關(guān)概念,然后分享了幾種用法,具有一定參考價值。需要的朋友可以了解下
    2021-09-09
  • C++實現(xiàn)圖形界面時鐘表盤代碼

    C++實現(xiàn)圖形界面時鐘表盤代碼

    這篇文章主要介紹了C++實現(xiàn)圖形界面時鐘表盤代碼,涉及坐標(biāo)函數(shù)的應(yīng)用及圖形界面程序設(shè)計,需要的朋友可以參考下
    2014-10-10
  • OpenCV實現(xiàn)車牌定位(C++)

    OpenCV實現(xiàn)車牌定位(C++)

    這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)車牌定位,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • C語言入門篇--變量的左值和右值

    C語言入門篇--變量的左值和右值

    本篇文章是基礎(chǔ)篇,適合c語言剛?cè)腴T的朋友,本文對初識c語言的指針和指針變量做了簡單的分析,幫助大家快速入門c語言的世界,更好的理解c語言
    2021-08-08
  • C++ TensorflowLite模型驗證的過程詳解

    C++ TensorflowLite模型驗證的過程詳解

    這篇文章給大家介紹了C++ TensorflowLite模型驗證的過程,測試代碼,主要是RunInference()和read_file(),詳細操作過程跟隨小編一起看看吧
    2021-08-08
  • QT使用udp實現(xiàn)發(fā)送與接收圖片

    QT使用udp實現(xiàn)發(fā)送與接收圖片

    這篇文章主要為大家詳細介紹了QT如何使用udp協(xié)議實現(xiàn)發(fā)送與接收圖片功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12

最新評論