campo-sirio/gfm/frtod.c

68 lines
1.0 KiB
C
Raw Normal View History

/* DEC *ConvFractionToDecimal( x, i, n, d)
*
* ARGUMENT
* DEC *x; output
* long i; integer part
* int n; numerator
* int d; denominator
*
* DESCRIPTION
* Sets x equal to the fraction i + n/d.
*
* SIDE EFFECTS
* x is indeterminate on error.
*
* RETURNS
* The DEC if the conversion is successful, and GM_NULL otherwise.
*
* POSSIBLE ERROR CODES
* GM_NULLPOINTER
* GM_DIV0
*
* AUTHOR
* Jared Levy Oct. 10, 1989
* Copyright (C) 1989-1990 Greenleaf Software Inc. All rights reserved.
*
* MODIFICATIONS
*
*/
#include <stdio.h>
#include "gmsystem.h"
DEC *ConvFractionToDecimal( x, i, n, d)
DEC *x;
long i;
int n;
int d;
{
int isneg=0;
_MacStart(GM_FRTOD);
_MacOutVarD(x);
/* check for 0 denominator */
if (!d) {
_MacErr(GM_DIV0);
_MacRet(GM_NULL);
}
/* handle negative numbers */
if (i<0) {
isneg = 1;
i = -i;
n = (n<0) ? -n : n;
d = (d<0) ? -d : d;
}
ConvLongToDecimal(x, (long) n);
DivideDecimalByLong(x, x, (long) d);
AddLongToDecimal(x, x, i);
DeleteTrailingZeroes(x, x);
x->dc.attr = isneg;
_MacRet(x);
}