# Find the absolute value of a number without built-in functions [closed]

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

• @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? – musefan Dec 9 '13 at 14:08
• @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. – Timtech Dec 9 '13 at 15:50
• @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 – Doorknob Dec 9 '13 at 17:57
• -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. – Gareth Dec 11 '13 at 20:15
• 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 – Peter Taylor Dec 14 '15 at 11:34

# 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
}

• It is important to note that the language was invented after the challenge and therefore it technically is not a competitive answer. – TanMath Dec 2 '15 at 5:55

## PowerShell: 38

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

if(($x=+(read-host))-lt0){-$x}else{$x}  # R 17 characters max(n<-scan(),-n)  Or 13 characters by adapting David Carraher Mathematica answer: sqrt(scan()^2)  • Max isn't allowed; I'd recommend switching to your 13 character solution. – Timtech Dec 24 '13 at 14:45 ## 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. • Square root is a function. – Gareth Dec 11 '13 at 20:19 • @Gareth It's dealing with mathematics. – Timtech Dec 11 '13 at 21:38 ## PowerShell, 22 (read-host)-replace'-'  Or, if the returned value has to typed as numeric, 23: +(read-host)-replace'-'  ## F#, 30 let a x=if x<0. then -x else x  # Python (20) lambda x:x*2*(x>0)-x  # Python - 27 chars >>> f=lambda x:-x if x<0 else x >>> f(-19.768) 19.768  • -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. – Justin Dec 14 '13 at 3:24 • oh duhh... fixed it – KGo Dec 14 '13 at 3:35 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  No one use sed? similar as perl s/-//  ## PHP, 40 <?$n=$argv[1];if($n<0) $n*=-1;echo$n;


# SQL - 43 chars

SELECT CASE WHEN @n<0 THEN -@n ELSE @n END;

• 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;. – manatwork Jan 7 '14 at 18:41
• @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. – ypercubeᵀᴹ Jan 7 '14 at 21:36

# 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.

## 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. # 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)} # 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. 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?

# LISP, 38

(define (abs x)
(cond ((< x 0) (- x))
(else x)
))

• Please add also the character count. – ProgramFOX Dec 27 '13 at 9:14

## Ti-Basic 84, 10 bytes

Ans(2(Ans>0)-1

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