ldiv.c

来自「标准c库代码,可以应用于各个系统提供了大量的基本函数」· C语言 代码 · 共 99 行

C
99
字号
/* * ldiv.c * Original Author:	G. Haley * * Computes the quotient and remainder of the division of the numerator n by * the denominator d. If the division is inexact, the sign of the quotient is * that of the mathematical quotient, and the magnitude of the quotient is the * largest integer less than the magnitude of the mathematical quotient. If the * result cannot be represented, the behaviour is undefined. Returns the * results in a structure of type ldiv_t, comprising the following members: * *	long quot; *	long rem; *//*FUNCTION<<ldiv>>---divide two long integersINDEX	ldivANSI_SYNOPSIS	#include <stdlib.h>	ldiv_t ldiv(long <[n]>, long <[d]>);TRAD_SYNOPSIS	#include <stdlib.h>	ldiv_t ldiv(<[n]>, <[d]>)	long <[n]>, <[d]>;DESCRIPTIONDivide@tex$n/d$,@end tex@ifinfo<[n]>/<[d]>,@end ifinforeturning quotient and remainder as two integers in a structure <<ldiv_t>>.RETURNSThe result is represented with the structure. typedef struct. {.  long quot;.  long rem;. } ldiv_t;where the <<quot>> field represents the quotient, and <<rem>> theremainder.  For nonzero <[d]>, if `<<<[r]> = ldiv(<[n]>,<[d]>);>>' then<[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'.When <[d]> is zero, the <<quot>> member of the result has the samesign as <[n]> and the largest representable magnitude.To divide <<int>> rather than <<long>> values, use the similarfunction <<div>>.PORTABILITY<<ldiv>> is ANSI, but the behavior for zero <[d]> is not specified bythe standard.No supporting OS subroutines are required.*/#include <limits.h>#include <stdlib.h>ldiv_t ldiv (n, d)     long n, d;{  ldiv_t res;  if (d)    {      res.quot = labs (n) / labs (d);      res.rem = labs (n) % labs (d);      if ((n < 0 && d > 0) || (n >= 0 && d < 0))	res.quot = -res.quot;      if (n < 0)	res.rem = -res.rem;    }  else    {      if (n < 0)	res.quot = LONG_MIN;      else	res.quot = LONG_MAX;      res.rem = 0;    }  return res;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?