Re: New proposal for math.e

new topic     » goto parent     » topic index » view thread      » older message » newer message

Colin Taylor wrote:

> Juergen Luethje wrote:
> 
> > This is my new proposal for a "math.e" standard include file,
> > according to the recent discussion on this forum.
> > 
> ...
> > global function radians_to_degrees (object x)
> >    -- in : (sequence of) angle(s) in radians
> >    -- out: (sequence of) angle(s) in degrees
> > 
> >    return x/PI * 180.0
> > end function
> > 
> > global function degrees_to_radians (object x)
> >    -- in : (sequence of) angle(s) in degrees
> >    -- out: (sequence of) angle(s) in radians
> > 
> >    return x/180.0 * PI
> > end function
> > 
> ...
> > Regards,
> >    Juergen
> 
> Very nice.  First I would suggest adding a constant RADIANS =
> 57.29577951308232158
> and modifying radians_to_degrees() and degrees_to_radians() as follows:
> 
> 
> global function radians_to_degrees (object x)
>    return x * RADIANS
> end function
> 
> global function degrees_to_radians (object x)
>    return x / RADIANS
> end function
> 
> 
> This will save a multiplication in each function.

I think that's a good idea. And Derek recently wrote that
multiplication is faster than division. So maybe we should have
two constants here, and do a multiplication in both functions.

>  I would also like to add a couple of functions:

Very nice!
How about adding a user-defined type here, that your functions use for
parameter checking?
(Below I also swapped the function names, according to your other post.)

type point (object x)
   if sequence(x) and (length(x) = 2) then
      return atom(x[1]) and atom(x[2])
   end if
   return 0
end type

> global function polar_to_rect(sequence xy)

global function rect_to_polar(point xy)
                              ^^^^^

> -- convert a rectangular coordinates to polar coordinates
>     atom angle, distance, x, y
>     x = xy[1]
>     y = xy[2]
>     distance = sqrt(power(x, 2)+power(y, 2))

I'm pretty sure the following will be faster:
      distance = sqrt(x*x + y*y)

>     if x > 0 then
>           angle = arctan(y/x)
>     elsif x < 0 then
>           if y < 0 then
>               angle = arctan(y/x)-PI
>           else
>               angle = arctan(y/x)+PI
>           end if

Are you sure?
According to one of my math textbooks, it should simply be:
     elsif x < 0 then
           angle = arctan(y/x)+PI


>     else
>           if y < 0 then
>               angle = -HALF_PI
>           else
>               angle = HALF_PI
>           end if
>     end if

According to the same math textbook, it should be:
     else                        -- x = 0 here
           if y < 0 then
               angle = -HALF_PI
           elsif y > 0 then
               angle = HALF_PI
           else
               angle = UNDEFINRD
           end if
     end if

>     return {distance, angle}
> end function
> 
> global function rect_to_polar(sequence pol)

global function polar_to_rect(point pol)
                              ^^^^^

> -- convert a polar coordinates to rectangular coordinates
>     atom distance, angle, x, y
>     distance = pol[1]
>     angle = pol[2]
>     x = distance*cos(angle)
>     y = distance*sin(angle)
>     return {x, y}
> end function
> 
>  
> In the above functions, angle is expressed in radians, increasing in a
> clockwise
> direction from east.
> 
> Regards,  Colin   (now returning to deep lurk mode)

Regards,
   Juergen

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu