1. RE: fraclib.e

Don't forget mine in General Functions...
----- Original Message ----- 
From: Pete Lomax <petelomax at blueyonder.co.uk>
Subject: Re: fraclib.e



On Tue, 21 Jan 2003 10:28:04 +0100, Juergen Luethje <eu.lue at gmx.de>
wrote:

>global function gcd2 (biginteger p, biginteger q)

Similar to mine, but yours is neater, better commented, and works with
negative numbers tongue 

>   -- The number of steps needed to arrive at the greatest common
>   -- divisor for two numbers is <= 5 times the number of digits in
>   -- the number with the smaller absolute value, i.e.:
>   --    n = min(abs(p), abs(q))
>   --    steps <= 5*(floor(log10(n))+1)
Quite fast then.
>   -- The worst case occurs when the algorithm is applied to two
>   -- consecutive Fibonacci numbers.
>   -- [ by JuLu,
>   --   after http://mathworld.wolfram.com/EuclideanAlgorithm.html ]
Hairy stuff indeed.

Pete

==^^===============================================================
This email was sent to: rforno at tutopia.com


TOPICA - Start your own email discussion group. FREE!

new topic     » topic index » view message » categorize

2. RE: fraclib.e

Have you tried the free ABC programming language from the Netherlands? They
do this kind of thing apparently with infinite precision. I don't know how
they do that. Maybe you could contact them to learn the basis of their
algorithm.
Regards.
----- Original Message -----
From: <jbrown1050 at hotpop.com>
Subject: fraclib.e


>
> I'm wrote this very simple lib, which gives infinite precision for
rational
> numbers by storing numbers as fractions, i.e. "1/3 * 3" (actually,
> "f_mul({1,3}, {3,1})" using my lib) returns 1, rather than 0.9999999999
>
> It stores fractions as a 2 element biginteger sequence. (A biginteger
> is currently an atom thats restricted to whole numbers, I eventually plan
> to add a bignum library to it, so you are not limited by the +/-1e300
> size of an atom.)
>
> If you try out this line, "? atom_to_f(f_to_atom({1,3}))", via my lib,
> you'll find it takes a VERY long time to compute. This is because my
> reduce() function is not very optimized. Any one have any ideas on how it
> could be made faster?
>
> TIA,
> jbrown
>
> My lib is pasted into my signature space at the bottom of this email.
>
> --
> include get.e
> include misc.e
>
> global type biginteger(object a)
> if not atom(a) then
> return 0
> end if
> if floor(a) != a then
> return 0
> end if
> return 1
> end type
>
> global type fraction(object s)
> if not sequence(s) then
> return 0
> end if
> if length(s) != 2 then
> return 0
> end if
> if not(biginteger(s[1]) and biginteger(s[2])) then
> return 0
> end if
> return 1
> end type
>
> function gcd(biginteger num1, biginteger num2)
> biginteger gcd_val
> if num1 > num2 then
> gcd_val = num1
> else
> gcd_val = num2
> end if
> while remainder(num1, gcd_val) != 0
> or remainder(num2, gcd_val) != 0 do
> gcd_val -= 1
> end while
> return gcd_val
> end function
> function reduce(fraction f)
> if f[2] = 0 then
> f[2] /= 0 --trigger an error here.
> end if
> return f / gcd(f[1], f[2])
> end function
>
> global function f_mul(fraction f1, fraction f2)
> return reduce({f1[1]*f2[1], f1[2]*f2[2]})
> end function
>
> global function f_div(fraction f1, fraction f2)
> return f_mul(f1, {f2[2], f2[1]})
> end function
>
> global function f_add(fraction f1, fraction f2)
> biginteger f1_dem, f2_dem
> f1_dem = f1[2]
> f2_dem = f2[2]
> f1 *= f2_dem
> f2 *= f1_dem
> return reduce({f1[1]+f2[1], f1[2]})
> end function
>
> global function f_sub(fraction f1, fraction f2)
> biginteger f1_dem, f2_dem
> f1_dem = f1[2]
> f2_dem = f2[2]
> f1 *= f2_dem
> f2 *= f1_dem
> return reduce({f1[1]-f2[1], f1[2]})
> end function
>
> global function f_to_atom(fraction f)
> return f[1]/f[2]
> end function
>
> global function f_to_string(fraction f)
> return sprintf("%d/%d", f)
> end function
>
> global function string_to_f(sequence s)
<snip>

>
>

new topic     » goto parent     » topic index » view message » categorize

3. RE: fraclib.e

> From: jbrown1050 at hotpop.com [mailto:jbrown1050 at hotpop.com]

> P.S. I know Pi is not irrational, but if its several trillion 
> digits long,
> I doubt that many people would want to compute that much in a home PC.

In fact, pi *is* irrational.  There are no integers a and b such that pi = a
/ b.  Beyond that, pi is transcendental (as opposed to algegbraic).  This
means that pi cannot be exactly described by any polynomial (i.e., sqrt(2)
can be described by x^2 - 2 = 0 -- it is *irrational*, but *not*
transcendental).

Matt Lewis

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu