# Smallest possible version of abs [duplicate]

The abs() function in most languages does one thing: give the positive version of the input. Most of the times this is done using something similar to

int
abs (int i)
{
return i < 0 ? -i : i;
}


I was wondering if there is any way to do this with less bytes. Note that this doesn't have to be more efficient, just smaller

Edit: i forgot to add that you can't use the abs() function in your language

• So basically are you asking to codegolf the abs function, without using it ? – The random guy May 30 '18 at 9:25
• I'm afraid not. In most languages abs(i) is the shortest, and for those languages that require Math.abs(i) like Java, .NET, JS, etc. i<0?-i:i is the shortest. – Kevin Cruijssen May 30 '18 at 9:26
• basically, yes. I was just wondering what other languages have to offer when it comes to making this as small as possible – Davin Miler May 30 '18 at 9:27
• @DavinMiler Ah wait. You meant this as a challenge without using the builtin abs. Not a tips question to see if there is anything shorter than i<0?-i:i as alternative? – Kevin Cruijssen May 30 '18 at 9:28
• What i meant was: create the smallest possible version of abs, without using abs or i<0?-i:i – Davin Miler May 30 '18 at 9:30

## Retina, 2 bytes

-



Try it online!

Just remove the minus sign...

• Works in QuadR too: Try it online! – Adám May 30 '18 at 9:43

# R, 8 bytes

(x^2)^.5


Try it online!

thanks to @Giuseppe

Former, 1 byte longer version :

# R, 9 bytes

x*sign(x)


Try it online!

• (x^2)^.5 is 8 bytes. Another 9-byter (if we don't care about vectorization) is max(x,-x) or if we do care about vectorization, pmax(x,-x) for 10 bytes. – Giuseppe May 30 '18 at 9:46
• Although of course abs in R returns the magnitude of complex inputs, so none of these quite works the same as the R version. – Giuseppe May 30 '18 at 9:49

# TI-Basic, 3 bytes

√(AnsAns


Square root of input * input. Alternatively, √(Ans² is also 3 bytes.

# Whitespace, 48 47 bytes

[N
S S N
_Create_Label_LOOP][S S S N
_Push_0][S N
S _Duplicate][T N
T   S _Read_STDIN_as_character][T   T   T   _Retrieve][S N
S _Duplicate][S S S T   S T T   S T N
_Push_45_-][T   S S T   _Subtract][N
T   S N
_If_0_jump_to_Label_LOOP][T N
S S _Print_as_character][N
S N
N
_Jump_to_Label_LOOP]


Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Start LOOP:
Character c = STDIN as character
if(c == '-')
Go to next iteration of LOOP
Print c
Go to next iteration of LOOP


[S S S N
_Push_0][S N
S _Duplicate][T N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve][S N
S _Duplicate][N
T   T   S N
_If_negative_jump_to_Label_NEG][N
S N
N
_Jump_to_Label_PRINT][N
S S S N
_Create_Label_NEG][S S T    T   N
_Push_-1][T S S N
_Multiply][N
S S N
_Create_Label_PRINT][T  N
S T _Print_as_integer]


Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Integer i = STDIN as integer
if(i is negative)
i = i * -1
else

function PRINT:
Print i as integer to STDOUT


# 05AB1E, 2 bytes

nt


Try it online.

Explanation:

n     # Square
#  i.e. 5 → 25
#  i.e. -5 → 25
t    # Square-root
#  25 → 5


# sed, 5 bytes

s/-//


Strip the negative sign

# Java 8, 13 bytes

n->n<0?n*-1:n


Try it online.

Boring, but can't be done shorter without Math. or using -n directly (a.f.a.i.k.).

• I don't think there is any restriction for -n – The random guy May 30 '18 at 9:47
• @Therandomguy There is, but it's in the comments instead of the challenge.. – Kevin Cruijssen May 30 '18 at 9:57

# J, 2 bytes

Anonymous tacit prefix function.

%* The argument (implicit) divided by its sign. Try it online! Handles complex numbers as well.

# APL (Dyalog Unicode), 3 bytesSBCS

There are a few interesting approaches to this. All are anonymous tacit prefix functions. H.PWiz added two more.

⊢×× The argument multiplied by its sign. Try it online!

⊢⌈- Max of the argument and its negation. Try it online!

⊢∧× The LCM of the argument and its sign. Try it online!

⊢∨- The GCD of the argument and its negation. Try it online!

⊢÷× The argument divided by its sign. Try it online! Handles complex numbers but needs ⎕DIV (Division method) set to 1.

• Also ⊢∧× and ⊢∨- – H.PWiz May 30 '18 at 10:08

# Python 2, 18 bytes

lambda x:max(-x,x)


Try it online!

Previous (20 bytes)

lambda x:(x,-x)[x<0]


Try it online!

# Japt, 2 bytes

Multiply input by its sign.

*g


Try it

Another square/root solution.

²q


Try it

Slightly less trivial 4 byte solution: convert to string, remove non-digit characters and convert back to integer. Or, if input can be taken as a string, this one can simply be r-.

sr\D


Try it