The challenge is to take any real number as input and output or return the absolute value. You may not use any built-in functions other than those dealing with input and output/returning. This is code golf, so the shortest code wins. This is my first question here, so bear with me if I've left something obvious out of the challenge.
As per Quincunx's suggestion, I am limiting input to anywhere between -9E99 and 9E99.
Also, the only functions/operators you can use are input
, output
, return
, +
, -
, *
, /
, %
, ^
+=
, -=
, *=
, /=
, >
, <
, ==
, >=
, <=
, !=
, square
, and square root
or their equivalents
"You may not use any built-in functions"
? For example, does the GolfScript answer violate this rule when it uses the built-in split functions? Or did you just mean built-in functions that are specifically designed to calculate the absolute value? \$\endgroup\$printf(x*(x<0?-1:1))
in a number of languages. \$\endgroup\$>
: in some languages it returns0
or1
; in other languages it returnstrue
orfalse
and Booleans can't be cast to integers. Should languages in the second category be permitted to use?:
in contexts which could be algebraically rewritten in terms of the condition as0
or1
under the "or their equivalents" grant? It's extremely fuzzy \$\endgroup\$