Re: round

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

On 2000-06-06 EUPHORIA at LISTSERV.MUOHIO.EDU said:
 EU>Hello ©koda,
 EU>>Could someone give me better round function, that rounds number.
 EU>>1.4 to 1, 1.6 to 2.
 EU>>-1.7 to -2 etc
 EU>>I  have written this one, but can't remeber how to do it better,
 EU>simpler.
 EU>I think this will do what you want if I'm understanding your
 EU>question correctly:

 EU>function round (object x)
 EU>return floor (x + 0.5)
 EU>end function


  you may also wish to consider the sign of x , as this affects the value
 returned.
---------------------------------------------------------------------------

  function abs(object x)
  --                            absolute value of atom/integer
  --                            easily extended to a sequence.
  --                            sqrt(x*x) is almost equivalent.
  --                            or use a for loop.

   if x<0 then x=-x end if

   return x

  end function

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

  function sgn(object x)
  --                             sign of  atom/integer
  --                         for loop required for a sequence.
   integer s
        s=0
     if x<0 then
        s=-1
     elsif x>0
        s=1
     else
        s=0
     end if

     return s

   end function

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

   function n_round(object x)
   --                           new rounding function.
   --               requires a little work to extend to a sequence.

    return sgn(x)*floor(abs(x)+0.5)

   end function

------------------------------------------------------------------------
Net-Tamer V 1.11 - Test Drive

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

Search



Quick Links

User menu

Not signed in.

Misc Menu