5
\$\begingroup\$

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

\$\endgroup\$
20
  • 3
    \$\begingroup\$ @Timtech: Can you clarify "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\$
    – musefan
    Dec 9, 2013 at 14:08
  • 2
    \$\begingroup\$ @musefan You may not use any built in functions (math operators are not included, they are not functions). The GolfScript answer did violate the rule; that's why it's not accepted. \$\endgroup\$
    – Timtech
    Dec 9, 2013 at 15:50
  • 4
    \$\begingroup\$ @tim Why don't operators count as functions? In C and friends you can override operators and use them just like normal functions. This seems to be a very vague rule \$\endgroup\$
    – Doorknob
    Dec 9, 2013 at 17:57
  • 9
    \$\begingroup\$ -1. Question is vague and the definition of "built-in function" has only appeared in the comments 3 days after the question was posed. It seems like you're just looking for the shortest way to say printf(x*(x<0?-1:1)) in a number of languages. \$\endgroup\$
    – Gareth
    Dec 11, 2013 at 20:15
  • 5
    \$\begingroup\$ Apart from the fact that the question as currently written seems to permit a whitelist of operators but only if they're not built in to the language, this is a classic example of why trying to whitelist permitted operations is a disaster. Consider >: in some languages it returns 0 or 1; in other languages it returns true or false 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 as 0 or 1 under the "or their equivalents" grant? It's extremely fuzzy \$\endgroup\$ Dec 14, 2015 at 11:34

49 Answers 49

1
2
1
\$\begingroup\$

Swift, 35 bytes (Not Competitive)

This is just for fun, and because Swift was developed after this question was posted, is not a competitive answer.

func a(v:Int)->Int{return v<0?-v:v}

un-golfed version

func absolute(value: Int) -> Int{
   return value < 0 ? -value : value
}
\$\endgroup\$
1
  • \$\begingroup\$ It is important to note that the language was invented after the challenge and therefore it technically is not a competitive answer. \$\endgroup\$
    – TanMath
    Dec 2, 2015 at 5:55
0
\$\begingroup\$

PowerShell: 38

Lazy if/else. Maybe I'll find a shorter way later.

if(($x=+(read-host))-lt0){-$x}else{$x}
\$\endgroup\$
0
\$\begingroup\$

R 17 characters

max(n<-scan(),-n)

Or 13 characters by adapting David Carraher Mathematica answer:

sqrt(scan()^2)
\$\endgroup\$
1
  • \$\begingroup\$ Max isn't allowed; I'd recommend switching to your 13 character solution. \$\endgroup\$
    – Timtech
    Dec 24, 2013 at 14:45
0
\$\begingroup\$

Ti-84 Table, 12 5 characters

The text in brackets are the square root character and the squared character, respectively.

Y=[square root]X[^2]

The old code:

Y=X(2(X>0)-1

Both of these handle rational and irrational numbers. Any real number is valid, with limits of -9.99999999*10^99 and 9.99999999*10^99 for the second one and sqrt(-9.99999999*10^99) and sqrt(9.99999999*10^99) for the first one.

Gets input on X and shows equivalent absolute number as Y.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Square root is a function. \$\endgroup\$
    – Gareth
    Dec 11, 2013 at 20:19
  • 1
    \$\begingroup\$ @Gareth It's dealing with mathematics. \$\endgroup\$
    – Timtech
    Dec 11, 2013 at 21:38
0
\$\begingroup\$

PowerShell, 22

(read-host)-replace'-'

Or, if the returned value has to typed as numeric, 23:

+(read-host)-replace'-'
\$\endgroup\$
0
\$\begingroup\$

F#, 30

let a x=if x<0. then -x else x
\$\endgroup\$
0
\$\begingroup\$

Python (20)

lambda x:x*2*(x>0)-x
\$\endgroup\$
0
\$\begingroup\$

Python - 27 chars

>>> f=lambda x:-x if x<0 else x
>>> f(-19.768)
19.768
\$\endgroup\$
2
  • \$\begingroup\$ -1. This does not comply with the specs. It does not return the absolute value, it simply negates the input (try f(300)). Fix it and I'll revert the vote. \$\endgroup\$
    – Justin
    Dec 14, 2013 at 3:24
  • \$\begingroup\$ oh duhh... fixed it \$\endgroup\$
    – KGo
    Dec 14, 2013 at 3:35
0
\$\begingroup\$

AWK, 15

1,$0*=$1<0?-1:1

Note that, the following code can handle all values except 0

AWK, 13

 $0*=$1<0?-1:1
\$\endgroup\$
0
\$\begingroup\$

No one use sed? similar as perl

s/-// 
\$\endgroup\$
1
0
\$\begingroup\$

PHP, 40

<? $n=$argv[1];if($n<0) $n*=-1;echo $n;
\$\endgroup\$
0
\$\begingroup\$

SQL - 43 chars

SELECT CASE WHEN @n<0 THEN -@n ELSE @n END;
\$\endgroup\$
2
  • \$\begingroup\$ Which SQL dialect? MSSQL lets you skip some spaces and the semicolon (40 chars): select case when @n<0then-@n else @n end, MySQL handles boolean (22 chars): select((@n>0)*2-1)*@n;. \$\endgroup\$
    – manatwork
    Jan 7, 2014 at 18:41
  • \$\begingroup\$ @manatwork, I was aiming for a DBMS agnostic answer. In MySQL, this would save 1 more char: SELECT(@n>0)*2*@n-@n; And the semicolon is not strictly needed in any dialect. \$\endgroup\$ Jan 7, 2014 at 21:36
0
\$\begingroup\$

Arcyóu, 16 bytes

(F(x)(^(^ x 2).5

This is an anonymous function taking one numeric type. It returns a floating point number equal to the number's absolute value. This is just nested exponentiation.

\$\endgroup\$
0
\$\begingroup\$

FP15 (7)

(*)sqrt

The (*) function squares a number. How does it work? In Haskell, (2*) means \x -> 2 * x and (*2) is \x -> x * 2, but (*) isn't \x -> x * x. In FP15, you can do (*) and even things like (*2+1) and (^2 - 5*sqrt). Also, in FP15, function composition is left-to-right.

Examples (~ is negation):

$ ./fp15-repl.py
> 12(*)sqrt
12.0
> 0(*)sqrt
0.0
> ~12(*)sqrt
12.0

If the signum function is allowed:

(*sgn)

If conditionals are allowed:

{(<0):~|_}

If max is allowed:

(~.max)
[,~]max

I created this language and it's still work in progress (no IO functions for now), but the last time I worked on it was early October.

\$\endgroup\$
0
\$\begingroup\$

Go, 62 bytes

Pretty straightforward, I should think. Submitting a noncompilable standalone function feels so... wrong.

func a(){a:=""
Scanln(&a)
b,_:=Atoi(a)
if b<0{b=0-b}
Print(b)}
\$\endgroup\$
0
\$\begingroup\$

Rotor, 2 bytes

sS

Challenge predates this language by two years, so this isn't competitive.

Squares input and then square roots it. Nothing special.

\$\endgroup\$
0
\$\begingroup\$

Note: this answer was written before the rule change mess, and was valid at time of posting.


Mouse-2002, 11 bytes

The DUP word is two bytes too long, and it requires a space after it. :( However, it still costs less than a var. (2 bytes per use)

?&DUP 0<[_]

Expanded:

?      ~ input
&DUP   ~ dup
0 <    ~ cmp
[      ~ if
  _    ~ signflip
]      ~ fi

Or, if leaving the value on the stack is not sufficient, then 12 bytes:

 ?&DUP 0<[_]!

This language needs implicit printing if nothing was.


$ mouse abs.mou
-100e99
100e99
$ mouse abs.mou
-INF
INF
$

Do I get a bonus if my program can compute every real number?

\$\endgroup\$
-1
\$\begingroup\$

LISP, 38

(define (abs x)
 (cond ((< x 0) (- x))
        (else x)
        ))
\$\endgroup\$
1
  • \$\begingroup\$ Please add also the character count. \$\endgroup\$
    – ProgramFOX
    Dec 27, 2013 at 9:14
-2
\$\begingroup\$

Ti-Basic 84, 10 bytes

Ans(2(Ans>0)-1
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Do you not need to close the outer parenthesis? \$\endgroup\$ Dec 6, 2013 at 23:26
  • 1
    \$\begingroup\$ @PeterTaylor No, it is not required. \$\endgroup\$
    – Timtech
    Dec 6, 2013 at 23:30
  • 1
    \$\begingroup\$ This doesn't work. \$\endgroup\$
    – lirtosiast
    Dec 1, 2015 at 17:19
  • \$\begingroup\$ Fixed! Input & output on Ans which is the standard \$\endgroup\$
    – Timtech
    Jan 22, 2016 at 13:30
  • \$\begingroup\$ I won't remove my downvote, because this is essentially your third redundant TI-BASIC answer. \$\endgroup\$
    – lirtosiast
    Jan 22, 2016 at 16:40
1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.