[pure-lang-svn] SF.net SVN: pure-lang: [370] pure/trunk
Status: Beta
Brought to you by:
agraef
From: <ag...@us...> - 2008-07-02 20:46:18
|
Revision: 370 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=370&view=rev Author: agraef Date: 2008-07-02 13:46:27 -0700 (Wed, 02 Jul 2008) Log Message: ----------- Add double->rational conversion. Modified Paths: -------------- pure/trunk/lib/math.pure pure/trunk/runtime.cc pure/trunk/runtime.h Modified: pure/trunk/lib/math.pure =================================================================== --- pure/trunk/lib/math.pure 2008-07-02 19:26:25 UTC (rev 369) +++ pure/trunk/lib/math.pure 2008-07-02 20:46:27 UTC (rev 370) @@ -418,12 +418,16 @@ /* Conversions. */ +extern expr* pure_rational(double); + rational x@(_%_) = x; rational x::int | rational x::bigint = x%1; -// TODO: Need to rationalize doubles here. Currently this is a no-op. -rational x::double = x; +/* The conversion from double doesn't do any rounding, so it is guaranteed + that converting the resulting rational back to double reconstructs the + original value. */ +rational x::double = n%d when n,d = pure_rational x end; rational (x+:y) = rational x +: rational y; rational (x<:y) = rational x <: rational y; Modified: pure/trunk/runtime.cc =================================================================== --- pure/trunk/runtime.cc 2008-07-02 19:26:25 UTC (rev 369) +++ pure/trunk/runtime.cc 2008-07-02 20:46:27 UTC (rev 370) @@ -1786,6 +1786,22 @@ } extern "C" +pure_expr *pure_rational(double d) +{ + pure_expr *u = pure_bigint(0, 0); + pure_expr *v = pure_bigint(0, 0); + mpz_t& x = u->data.z; + mpz_t& y = v->data.z; + mpq_t q; + mpq_init(q); + mpq_set_d(q, d); + mpq_get_num(x, q); + mpq_get_den(y, q); + mpq_clear(q); + return pure_tuplel(2, u, v); +} + +extern "C" pure_expr *bigint_neg(mpz_t x) { pure_expr *u = pure_bigint(0, 0); Modified: pure/trunk/runtime.h =================================================================== --- pure/trunk/runtime.h 2008-07-02 19:26:25 UTC (rev 369) +++ pure/trunk/runtime.h 2008-07-02 20:46:27 UTC (rev 370) @@ -415,6 +415,11 @@ pure_expr *pure_bigintval(pure_expr *x); pure_expr *pure_pointerval(pure_expr *x); +/* Convert a double to a rational number, without rounding. Returns a pair n,d + of two bigint values, where n is the numerator and d the denominator. */ + +pure_expr *pure_rational(double d); + /* Construct a "byte string" from a string. The result is a raw pointer object pointing to the converted string. The original string is copied (and, in the case of pure_byte_cstring, converted to the system encoding). The This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |