Re: New proposal for math.e
- Posted by jacques deschĂȘnes <desja at g?obetrotter?net> Aug 02, 2007
- 532 views
Hi Juergen, As I mentionned it in previous unoticed post, a math.e library should only deal with numbers. So why find_least() use compare() to compare elements of sequence? Those elements should be atoms and the relational operator '<' should be used. You may argue that list may contain sub-sequence of atom, but can you give exemples were this would be usefull? What i propose is this:
function find_least(sequence list,integer start) -- return the smallest element of list where list is sequence of atoms. -- list must have length(list)>=2 -- but what should we do if length(list)<2, crash!!! ? atom temp ineger ret temp = s[start] ret = start for i = start+1 to length(list) do if s[i]<temp then temp = s[i] ret = i end if end for return ret end function
same thing for find_greatest() regards, Jacques DeschĂȘnes Juergen Luethje wrote: > > This is my new proposal for a "math.e" standard include file, > according to the recent discussion on this forum. > > Changes compared to the previous version: > - All constant values are now truncated after the 17th digit > (according to Matt's explanation about precision). > - Double-checked all constant values by using a third-party > calculator (--> no changes). > - No user-defined type checking of the last parameter anymore in > functions find_least(), find_greatest(), least(), greatest() > ( with an invalid parameter, the functions will crash anyway -- > with or without type-checking ). > Removed the concerning user-defined type. > - Simplified function log10() by using sequence operations. > - Changed the names deg() and rad() to radians_to_degrees() and > degrees_to_radians(), respectively. > > }}} <eucode> > global constant > LN2 = 0.6931471805599453, > LN10 = 2.3025850929940456, > E = 2.7182818284590452, > SQRT2 = 1.4142135623730950, > HALF_SQRT2 = 0.7071067811865475, > PI = 3.1415926535897932, > HALF_PI = 1.5707963267948966, > QUARTER_PI = 0.7853981633974483, > TWO_PI = 6.2831853071795864 > > > global function log2 (object x) > -- logarithm base 2 > -- in : (sequence of) real number(s) > 0 > -- out: (sequence of) real number(s) > -- Note: This function returns _exact_ results for all integral > -- powers of 2 in the half-closed interval ]0,#FFFFFFFF] > > if atom(x) then > if x = #20000000 then > return 29 > elsif x = #80000000 then > return 31 > else > return log(x)/LN2 > end if > end if > > for i = 1 to length(x) do > x[i] = log2(x[i]) > end for > return x > end function > > global function log10 (object x) > -- logarithm base 10 > -- in : (sequence of) real number(s) > 0 > -- out: (sequence of) real number(s) > > return log(x)/LN10 > end function > > global function exp (object x) > return power(E, x) > end function > > global function sinh (object x) > return (exp(x) - exp(-x)) / 2 > end function > > global function cosh (object x) > return (exp(x) + exp(-x)) / 2 > end function > > global function tanh (object x) > return sinh(x) / cosh(x) > end function > > global function arcsinh (object x) > return log(x + sqrt(x*x+1)) > end function > > type not_below_1 (object x) > if atom(x) then > return x >= 1.0 > end if > > for i = 1 to length(x) do > if not not_below_1(x[i]) then > return 0 > end if > end for > return 1 > end type > > global function arccosh (not_below_1 x) > return log(x + sqrt(x*x-1)) > end function > > type abs_below_1 (object x) > if atom(x) then > return x > -1.0 and x < 1.0 > end if > > for i = 1 to length(x) do > if not abs_below_1(x[i]) then > return 0 > end if > end for > return 1 > end type > > global function arctanh (abs_below_1 x) > return log((1+x)/(1-x)) / 2 > end function > > global function abs (object x) > -- return the absolute value of (all elements of) x > > if atom(x) then > if x < 0 then > return -x > else > return x > end if > end if > > for i = 1 to length(x) do > x[i] = abs(x[i]) > end for > return x > end function > > global function sign (object x) > -- x < 0 ==> -1 > -- x = 0 ==> 0 > -- x > 0 ==> +1 > > if atom(x) then > return compare(x, 0) > end if > > for i = 1 to length(x) do > x[i] = sign(x[i]) > end for > return x > end function > > global function ceil (object x) > -- the opposite of floor() > -- Examples: ? ceil(3.2) --> 4 > -- ? ceil({-3.2,7,1.6}) --> {-3,7,2} > > return -floor(-x) > end function > > global function sum (sequence list) > -- Return the sum of all elements in 'list'. > -- Note: This does not do a recursive sum of sub-sequences. > atom ret > > ret = 0 > for i = 1 to length(list) do > ret += list[i] > end for > return ret > end function > > global function find_least (sequence list, integer start) > -- Search for the lowest value in 'list', beginning at index 'start'. > -- Return the index of that element. > -- Notes: This does not do a recursive compare on sub-sequences. > -- An empty sequence will cause a runtime error. > object temp > integer ret > > temp = list[start] > ret = start > for i = start+1 to length(list) do > if compare(temp, list[i]) = 1 then > temp = list[i] > ret = i > end if > end for > return ret > end function > > global function find_greatest (sequence list, integer start) > -- Search for the greatest value in 'list', beginning at index 'start'. > -- Return the index of that element. > -- Notes: This does not do a recursive compare on sub-sequences. > -- An empty sequence will cause a runtime error. > object temp > integer ret > > temp = list[start] > ret = start > for i = start+1 to length(list) do > if compare(temp, list[i]) = -1 then > temp = list[i] <snip>