1. something like 'abs()' ???

er... me again...

i cant seem to find the equivalent of an 'abs' or 'absolute' function as found
in 'commodore 64' basic etc...

it is a function that returns the positive value of a number regardless of
whether the original number is positive or negative

i have got around this by taking the square root of the square of the number...

but is there a simpler way anyone knows of? (maybe i'm having another brain-cell
free day)

cheerz again - eel

new topic     » topic index » view message » categorize

2. Re: something like 'abs()' ???

eel picton wrote:
> 
> er... me again...
> 
> i cant seem to find the equivalent of an 'abs' or 'absolute' function as found
> in 'commodore
> 64' basic etc...
> 
> it is a function that returns the positive value of a number regardless of
> whether
> the original number is positive or negative
> 
> i have got around this by taking the square root of the square of the
> number...
> 
> but is there a simpler way anyone knows of? (maybe i'm having another
> brain-cell free
> day)

The EuForum archives are a good place to look up such things. One way:

function abs(integer x)
    return x * ((x>0) - (x<0))
end function

Regards,
Irv

new topic     » goto parent     » topic index » view message » categorize

3. Re: something like 'abs()' ???

-- Use the following code to determine which
-- abs() function is faster. Interesting!!!

include get.e
object junk

with warning

function abs(integer x)
	if x > 0 then
		return x
	end if
	return -x
end function

function abs2(integer x)
	if x < 0 then
		return -x
	end if
	return x
end function

integer test_time, tester, counter
atom timer

test_time = 5 -- change this for shorter/longer testing time

counter = 0
puts(1,"Testing abs(1)\t")
timer = time() + test_time
while timer > time() do
	counter += 1
	tester = abs( 1 )
end while
?counter

counter = 0
puts(1,"Testing abs2(1)\t")
timer = time() + test_time
while timer > time() do
	counter += 1
	tester = abs2( 1 )
end while
?counter

counter = 0
puts(1,"Testing abs(-1)\t")
timer = time() + test_time
while timer > time() do
	counter += 1
	tester = abs( -1 )
end while
?counter

counter = 0
puts(1,"Testing abs2(-1)\t")
timer = time() + test_time
while timer > time() do
	counter += 1
	tester = abs2( -1 )
end while
?counter

junk = wait_key()

-- end of test code

-=ck
"Programming in a state of EUPHORIA."

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu