Multi-Precision Expressions

Check-in [8b57721245]
Login

Check-in [8b57721245]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Change qround() to round to even on ties.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 8b577212453d0356f554549fd92e82c4ba377b37
User & Date: dgp 2013-05-09 14:17:12.000
Context
2013-05-09
14:51
Bump for a release of version 1.2. check-in: 5d0b7b082a user: dgp tags: trunk
14:17
Change qround() to round to even on ties. check-in: 8b57721245 user: dgp tags: trunk
12:57
Added a test to check that the mpexpr precision variable is handled on a per-interpreter basis (with the old version this was not the case) check-in: bb6410d6f3 user: arjenmarkus tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to generic/qfunc.c.
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736






737
738
739
740
741
742
743
 */
NUMBER *
qround(q, places)
	NUMBER *q;		/* number to be rounded */
	long places;		/* number of decimal places to round to */
{
	NUMBER *r;
	ZVALUE tenpow, roundval, tmp1, tmp2;

	if (places < 0)
		math_error("Negative places for qround");
	if (qisint(q))
		return qlink(q);
	/*
	 * Calculate one half of the denominator, ignoring fractional results.
	 * This is the value we will add in order to cause rounding.
	 */
	zshift(q->den, -1L, &roundval);
	roundval.sign = q->num.sign;
	/*
	 * Ok, now do the actual work to produce the result.
	 */
	r = qalloc();
	ztenpow(places, &tenpow);
	zmul(q->num, tenpow, &tmp2);
	zadd(tmp2, roundval, &tmp1);
	zfree(tmp2);
	zfree(roundval);
	zquo(tmp1, q->den, &tmp2);
	zfree(tmp1);






	if (ziszero(tmp2)) {
		zfree(tmp2);
		return qlink(&_qzero_);
	}
	/*
	 * Now reduce the result to the lowest common denominator.
	 */







|




















|

>
>
>
>
>
>







707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
 */
NUMBER *
qround(q, places)
	NUMBER *q;		/* number to be rounded */
	long places;		/* number of decimal places to round to */
{
	NUMBER *r;
	ZVALUE tenpow, roundval, tmp1, tmp2, rem;

	if (places < 0)
		math_error("Negative places for qround");
	if (qisint(q))
		return qlink(q);
	/*
	 * Calculate one half of the denominator, ignoring fractional results.
	 * This is the value we will add in order to cause rounding.
	 */
	zshift(q->den, -1L, &roundval);
	roundval.sign = q->num.sign;
	/*
	 * Ok, now do the actual work to produce the result.
	 */
	r = qalloc();
	ztenpow(places, &tenpow);
	zmul(q->num, tenpow, &tmp2);
	zadd(tmp2, roundval, &tmp1);
	zfree(tmp2);
	zfree(roundval);
	zdiv(tmp1, q->den, &tmp2, &rem);
	zfree(tmp1);
	if (ziszero(rem) && zisodd(tmp2) && ziseven(q->den)) {
		zsub(tmp2, _one_, &tmp1);
		zfree(tmp2);
		tmp2 = tmp1;
	}
	zfree(rem);
	if (ziszero(tmp2)) {
		zfree(tmp2);
		return qlink(&_qzero_);
	}
	/*
	 * Now reduce the result to the lowest common denominator.
	 */