Befunge 98 - 11 7 8
~:'-`!j,
Explained:
~ read character input
: duplicate
'- fetch - and push it
` compare duplicated value with it, 1 if first is greater, otherwise 0
! if the number on the stack is 0, set it to 1, otherwise 0
j jump the number of characters on the top of the stack, which is 1 if the input character is -
, print the character on top of the stack
If I understand this correctly, then this works for any input, but only if constant looping is allowed. It simply prints everything but the minus sign. If infinite looping is not allowed, then this should work (13 10 chars (Thanks FireFly)):
~:'-`!j,#@
Old version (Befunge 93) - 11
&:0`2*1-*.@
It works like this:
& push input
: push a duplicate
0 push a zero
` pop duplicate and 0, if duplicate greater than 0, push 1 else 0
2 push a two
* pop the 1 or 0 and 2 , multiply the 1 or 0 by the 2
1 push a one
- pop last two values and subtract the second value on the stack by the first (ie a-1)
* pop last two values and multiply last two values on stack (ie + or - 1 * input)
. print
@ end program
Note: Only integers are valid numbers in Befunge.
If I need to support floating point input, then it is 18 chars:
&&\:0`2*1-*.".",.@
It is 17 chars in Befunge 98:
&&\:0`2*1-*.'.,.@
Note: these print a space before the decimal point
"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\$