1. RE: abs()
- Posted by Chris Bensler <bensler at nt.net> Oct 25, 2004
- 494 views
- Last edited Oct 26, 2004
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
2. RE: abs()
- Posted by Chris Bensler <bensler at nt.net> Oct 25, 2004
- 479 views
- Last edited Oct 26, 2004
Chris Bensler wrote: > > > 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.. > > }}} <eucode> > 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 > </eucode> {{{ > > if you don't have to deal with sequences (which you likely do not), you > can optimize for atoms.. > > }}} <eucode> > global function abs(atom a) > if a < 0 then return -a else return a end if > end function > </eucode> {{{ > > > Chris Bensler > Code is Alchemy > Sorry, I forgot the Post Script. PS. If anybody can provide you with the most efficient algorithm, it will be Jiri Babor. I suggest you draw his attention :) Or even query him personally. Chris Bensler Code is Alchemy
3. RE: abs()
- Posted by "Ricardo M. Forno" <rforno at uyuyuy.com> Oct 26, 2004
- 502 views
----- Original Message ----- From: FD(censored) <francis at gmx.co.uk> To: <EUforum at topica.com> Sent: Monday, October 25, 2004 5:31 PM Subject: abs() > > > 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 have two commented versions of this routine in my "General Functions" (GENFUNC.E) package. Regards.