1. Hi, All Euphoria people

Hi, All Euphoria People.

I started learning Euphoria on 2/27/1998, the day after Euphoria 2.0 beta
released. I was using Pascal and C++ in old days. Euphoria's really cool!
Now, I have turned to euphoria, and I am gonna deltree my BC and BP!.. smile

I wrote some programs yesterday (Or this morning? I did it for a whole
night with *euphoria*!)  (-O...

One is Pi calculation, much faster than that two on the web. I have a
faster one which takes only 50% time of this one, but that one can't
solve more than 2000 digits, not really good EUPHORIA program.
EUPHORIA means, no limit ! smile

Another is a floating point library. It's a good idea to make a lib that
handles floating numbers without LIMIT, But it's hard to do. Now, I've
made a lib with only plus, sub, mul, div.. and "float to string"...
Try it! If you have some suggestions or a better lib, Email me please!

My name is Li, Peng. I'm a HS student in China, So please don't care my
mistakes in English ... smile


*****************************************
-- Pi.ex

-- Fast Pi Calculation,  Li Peng

-- 2/28/1998 ( I started learning Euphoria on 2/27/1998 ! smile

without type_check
include get.e
-----------------------------------------------------------------------------
function Calc_PI( integer Digits )
    sequence sum,b,zero
    integer n,m,div
        zero = repeat(0,Digits)
        b    = 3 & repeat(0,Digits-1)
        sum  = b
        n    = 1
        while compare(zero,b) != 0 do
            b   = b*n
            div = 4*(n+1)
            m   = 0
            for i = 1 to Digits do
                m = m * 10 + b[i]
                b[i] = floor(m/div)
                m = remainder(m,div)
            end for
            b = b*n
            div = n+2
            m   = 0
            for i = 1 to Digits do
                m = m * 10 + b[i]
                b[i] = floor(m/div)
                m = remainder(m,div)
            end for
            sum = sum + b
            n = n + 2
        end while
        m = 0
        for i = Digits to 1 by -1 do
            m = sum[i] + m
            sum[i] = remainder(m,10)
            m = floor(m/10)
        end for
    return sum
end function
-----------------------------------------------------------------------------
object pi, inp, timecost, digits
    puts(2, "\nHow many digits would you like to calculate Pi to ? ")
    inp = get(0)
    digits = inp[2] + 3
    timecost = time()
    pi = Calc_PI(digits)
    timecost = time() - timecost
    printf(1, "\n\nPi, to %d digits, is approximately:\n\n3.", digits-3)
    for i = 2 to digits - 3 do
        puts(1,'0'+pi[i])
    end for
    printf(2, "\n\nIt took %g seconds to calculate Pi\n", timecost )
-----------------------------------------------------------------------------



***************************************************************************

FloatLib.e

--------------------------------------------------------------------------
--  Floating point library
--  Author : Li Peng
--  Date   : 2/28/1998
--------------------------------------------------------------------------

global constant
    FL_DIGITS   = 1,
    FL_SIGN     = 2,
    FL_EXP      = 3,
    FL_DATA     = 4

--------------------------------------------------------------------------

global type float_number( sequence x )
    return integer(x[FL_DIGITS]) and integer(x[FL_EXP])
           and integer(x[FL_SIGN]) and sequence(x[FL_DATA])
end type

--------------------------------------------------------------------------

global function Float_New( integer Digits )
    return { Digits, 1, 0, repeat(0, Digits ) }
end function

---------------------------------------------------------------------------

global function Float_NewInt( integer Digits, atom content )
    sequence temp, int
        temp = Float_New( Digits )
        int = {}
        if content < 0 then
            temp[FL_SIGN] = -1
            content = - content
        end if
        while content > 0 do
            int = prepend( int, remainder( content, 10 ) )
            content = floor( content / 10 )
        end while
        temp[FL_DATA][1..length(int)] = int
        temp[FL_EXP] = length(int) - 1
    return temp
end function

----------------------------------------------------------------------------

global function Float_ToString( float_number x )
    sequence temp
        temp = {}
        if x[FL_SIGN] < 0 then
            temp = {'-'}
        end if
        for i = 1 to x[FL_DIGITS] do
            temp = temp & '0'+x[FL_DATA][i]
            if i = 1 then
                temp = temp & '.'
            end if
        end for
        if x[FL_EXP] > 0 then
            temp = temp & sprintf("E+%d", x[FL_EXP])
        else
            temp = temp & sprintf("E%d", x[FL_EXP])
        end if
    return temp
end function

-----------------------------------------------------------------------------

global function Float_Negtive( float_number x )
    x[FL_SIGN] = - x[FL_SIGN]
    return x
end function

------------------------------------------------------------------------------

global function Float_Arrange( float_number x )
    integer k,len
        len = x[FL_DIGITS]
        k = 0
        for test = 1 to len do
            if x[FL_DATA][test] > 0 then
                k = test
                exit
            end if
        end for
        if k = 0 then
            x[FL_EXP] = 0
            x[FL_DATA] = repeat(0,len)
        elsif k > 1 then
            x[FL_EXP] = x[FL_EXP] - k + 1
            x[FL_DATA] = x[FL_DATA][k..len] & repeat(0, k-1)
        end if
    return x
end function

------------------------------------------------------------------------------


function AbsCompare( float_number operand1, float_number operand2 )
        if operand1[FL_EXP] != operand2[FL_EXP] then
            return operand1[FL_EXP] - operand2[FL_EXP]
        end if
        for i = 1 to operand1[FL_DIGITS] do
            if operand1[FL_DATA][i] != operand2[FL_DATA][i] then
                return operand1[FL_DATA][i] - operand2[FL_DATA][i]
            end if
        end for
    return 0
end function

function Positive_Add( float_number operand1, float_number operand2 )
    integer len1,pos1, gap,temp, high
        len1 = operand1[FL_DIGITS]
        gap = operand1[FL_EXP] - operand2[FL_EXP]
        high = 0
        temp = 0
        for pos2 = operand2[FL_DIGITS] to 1 by -1 do
            pos1 = pos2 + gap
            if pos1 <= len1 then
                temp = operand2[FL_DATA][pos2]
                while pos1 >= 1 and temp > 0 do
                    temp = operand1[FL_DATA][pos1] + temp
                    operand1[FL_DATA][pos1] = remainder( temp, 10 )
                    temp = floor (temp / 10)
                    pos1 = pos1 - 1
                end while
            end if
            if temp != 0 then
                high = high + temp
                temp = 0
            end if
        end for
        if high > 0 then
            operand1[FL_EXP] = operand1[FL_EXP] + 1
            operand1[FL_DATA] = high & operand1[FL_DATA][1..len1-1]
        end if
    return operand1
end function

function Positive_Sub( float_number operand1, float_number operand2 )
    integer len1,pos1, gap,temp
        len1 = operand1[FL_DIGITS]
        gap = operand1[FL_EXP] - operand2[FL_EXP]
        temp = 0
        for pos2 = operand2[FL_DIGITS] to 1 by -1 do
            pos1 = pos2 + gap
            if pos1 <= len1 then
                temp = operand2[FL_DATA][pos2]
                while pos1 >= 1 and temp > 0 do
                    temp = operand1[FL_DATA][pos1] - temp
                    operand1[FL_DATA][pos1] = remainder( temp+10, 10 )
                    if temp < 0 then
                        temp = 1
                    else
                        temp = 0
                    end if
                    pos1 = pos1 - 1
                end while
            end if
        end for
    return Float_Arrange(operand1)
end function


global function Float_Add( float_number operand1, float_number operand2 )
    float_number result
    integer sign
        if AbsCompare(operand1, operand2) < 0 then
            result = operand1
            operand1 = operand2
            operand2 = result
        end if
        sign = operand1[FL_SIGN]
        if sign < 0 then
            operand1[FL_SIGN] = 1
            operand2 = Float_Negtive( operand2 )
        end if
        if operand2[FL_SIGN] > 0 then
            result = Positive_Add( operand1, operand2 )
        else
            result = Positive_Sub( operand1, operand2 )
        end if
        if sign < 0 then
            result = Float_Negtive( result )
        end if
    return result
end function

---------------------------------------------------------------------------

global function Float_Sub( float_number operand1, float_number operand2 )
    return Float_Add( operand1, Float_Negtive( operand2 ) )
end function

----------------------------------------------------------------------------

global function Float_Mul( float_number operand1, float_number operand2 )
    sequence temp
    float_number result
    integer len1, len2, len, pos, k, range1, range2
        len1 = operand1[FL_DIGITS]
        len2 = operand2[FL_DIGITS]
        len = len1 + len2
        temp = repeat(0,len)
        for pos1 = len1 to 1 by -1 do
            if operand1[FL_DATA][pos1] > 0 then
                range1 = pos1
                exit
            end if
        end for
        for pos2 = len2 to 1 by -1 do
            if operand2[FL_DATA][pos2] > 0 then
                range2 = pos2
                exit
            end if
        end for
        for pos1 = 1 to range1 do
        for pos2 = 1 to range2 do
            pos = pos1 + pos2
            temp[pos] = temp[pos] + operand1[FL_DATA][pos1]
                                  * operand2[FL_DATA][pos2]
        end for
        end for
        k = 0
        for loop = len to 1 by -1 do
            k = temp[loop] + k
            temp[loop] = remainder( k, 10 )
            k = floor( k / 10 )
        end for
        result = {0,0,0,{}}
        result[FL_DIGITS] = len1
        result[FL_SIGN]   = operand1[FL_SIGN] * operand2[FL_SIGN]
        result[FL_EXP]    = operand1[FL_EXP] + operand2[FL_EXP] + 1
        result[FL_DATA]   = temp[1..len1]
    return Float_Arrange(result)
end function


------------------------------------------------------------------------------

global function Float_Div( float_number operand1, float_number operand2 )
    float_number result
    sequence temp, bc, cc, tmpbc
    integer len1, len2, len, cur, wide
        temp = {}
        bc = 0 & operand1[FL_DATA]
        cc = 0 & operand2[FL_DATA]
        len = operand1[FL_DIGITS]
        len1 = len + 1
        if length(cc) > len1 then
            cc = cc[1..len1]
        end if
        len2 = length(cc)
        cur = 0
        wide = 0
        while wide < len1 do
            tmpbc = bc[1..len2] - cc
            for i = len2 to 2 by -1 do
                if tmpbc[i] < 0 then
                    tmpbc[i] = tmpbc[i] + 10
                    tmpbc[i-1] = tmpbc[i-1] - 1
                end if
            end for
            if tmpbc[1] < 0 then
                temp = temp & cur
                cur = 0
                bc = bc[2..len+1] & 0
                wide = wide + 1
            else
                cur = cur + 1
                bc[1..len2] = tmpbc
            end if
        end while
        result = {0,0,0,{}}
        result[FL_DIGITS] = len1
        result[FL_EXP]    = operand1[FL_EXP]  - operand2[FL_EXP]
        result[FL_SIGN]   = operand1[FL_SIGN] * operand2[FL_SIGN]
        result[FL_DATA]   = temp[1..len+1]
        result = Float_Arrange( result )
        result[FL_DIGITS] = len
        result[FL_DATA]   = result[FL_DATA][1..len]
    return result
end function

----------------------


btw, Are you good at Quake II ? . . . :P

new topic     » topic index » view message » categorize

2. Re: Hi, All Euphoria people

At 06:18 PM 3/1/98 +0800, you wrote:
>Hi, All Euphoria People.
>
>I started learning Euphoria on 2/27/1998, the day after Euphoria 2.0 beta
>released. I was using Pascal and C++ in old days. Euphoria's really cool!
>Now, I have turned to euphoria, and I am gonna deltree my BC and BP!.. smile
>
>I wrote some programs yesterday (Or this morning? I did it for a whole
>night with *euphoria*!)  (-O...
>
>One is Pi calculation, much faster than that two on the web. I have a
>faster one which takes only 50% time of this one, but that one can't
>solve more than 2000 digits, not really good EUPHORIA program.
>EUPHORIA means, no limit ! smile
>
>Another is a floating point library. It's a good idea to make a lib that
>handles floating numbers without LIMIT, But it's hard to do. Now, I've
>made a lib with only plus, sub, mul, div.. and "float to string"...
>Try it! If you have some suggestions or a better lib, Email me please!
>
>My name is Li, Peng. I'm a HS student in China, So please don't care my
>mistakes in English ... smile
>
        Congratulations on your sudden mastery of Euphoria. Try to find a
new-to-c++ programmer who can accomplish so much in such a short time. I,
too, was thrilled with the speed of learning and usage for Euphoria.
        Li, Peng, welcome and thanks for the fine collection of math
techniques. I am a retired engineer and I have a lot of fun with Euphoria,
but I have been a little disappointed by the lack of engineers and
mathematics people in this group. It is heavy on games and graphics. It is
also very friendly and supportive of those who need help. Perhaps with your
leadership, we can broaden the scope of Euphoria activities. I tried to do
so with my matrx.e which you can find in the Official home page.
        I would appreciate a note from you with a brief paragraph and
example re the usage of each of your routines (and when they should be used
instead of the built in routines). Many are fairly obvious but others aren't.
        Another of our contributers recently wrote a clever prog for finding
prime numbers. I will hunt up it/him for you if you are interested.
        Thank you, Art Adamson.

Arthur P. Adamson, The Engine Man, euclid at isoc.net

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

Search



Quick Links

User menu

Not signed in.

Misc Menu