/* DEC long ConvDecimalToFraction( pn, x, d); * * ARGUMENT * DEC *x; input DEC * long i; integer part * int *pn; numerator * int d; denominator * * DESCRIPTION * Rounds the DEC x to a mixed fraction with denominator d. * The integer part is stored returned, while the numberator, * between 0 and d-1, is stored in *pn. * * SIDE EFFECTS * x is indeterminate on error. * * RETURNS * The integer part if the conversion is successful, and 0L otherwise. * * POSSIBLE ERROR CODES * GM_NULLPOINTER * GM_DIV0 * GM_OVERFLOW * * AUTHOR * Jared Levy Oct. 10, 1989 * Copyright (C) 1989-1990 Greenleaf Software Inc. All rights reserved. * * MODIFICATIONS * */ #include #include "gmsystem.h" long ConvDecimalToFraction( pn, x, d) int *pn; DEC *x; int d; { long i; DECP x0, xf, d0; _MacStart(GM_DTOFR); _MacInVar(x, 0L); if (!pn) { _MacErr(GM_NULLPOINTER); _MacRet(0L); } /* check for 0 denominator */ if (!d) { _MacErr(GM_DIV0); _MacRet(0L); } /* find integer part */ _TruncateDec80Bit(x0, x, 0); i = x0->ls.lsl[0]; if (x0->ls.lsl[1] || (i & 0x80000000L)) { _MacErr(GM_OVERFLOW); _MacRet(0L); } /* numerator = (x - integer part) * denom. */ _SubDec80Bit(xf, x, x0); ConvLongToDecimal(d0, (long) d); _MulDec80Bit(xf, xf, d0); _ScaleDec80Bit(xf, xf, 0); *pn = xf->dc.sl[0]; /* if numer. = den., then increase i by 1 and set num = 0 */ if (*pn == d) { *pn=0; i++; } /* make int. part negative if number is negative */ if (_MacIsDecN(x)) i = -i; _MacRet(i); }