RE: abs()

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

FD(censored) wrote:
> 
> 
> hey i remember ages ago someone posted asking the quickest way to code 
> an abs() function. The results were good, but a few who posted at the 
> end of the thread posted very very efficient ways to do this in 
> euphoria, which i cannot remember. I'm looking for speed here. Any help 
> would be appreciated
> 
> 

I dunno about the fastest, but this beats most attempts..

global function abs(object x)
  if atom(x) then
    if x < 0 then return -x else return x end if
  else
    for i = 1 to length(x) do
      if x[i] < 0 then x[i] = -x[i] end if
    end for
  end if
  return x
end function


if you don't have to deal with sequences (which you likely do not), you 
can optimize for atoms..

global function abs(atom a)
  if a < 0 then return -a else return a end if
end function



Chris Bensler
Code is Alchemy

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

Search



Quick Links

User menu

Not signed in.

Misc Menu