-2
\$\begingroup\$

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

\$\endgroup\$
8
  • \$\begingroup\$ So basically are you asking to codegolf the abs function, without using it ? \$\endgroup\$ Commented May 30, 2018 at 9:25
  • \$\begingroup\$ 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. \$\endgroup\$ Commented May 30, 2018 at 9:26
  • \$\begingroup\$ basically, yes. I was just wondering what other languages have to offer when it comes to making this as small as possible \$\endgroup\$ Commented May 30, 2018 at 9:27
  • \$\begingroup\$ @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? \$\endgroup\$ Commented May 30, 2018 at 9:28
  • \$\begingroup\$ What i meant was: create the smallest possible version of abs, without using abs or i<0?-i:i \$\endgroup\$ Commented May 30, 2018 at 9:30

11 Answers 11

4
\$\begingroup\$

Retina, 2 bytes

-

Try it online!

Just remove the minus sign...

\$\endgroup\$
1
  • \$\begingroup\$ Works in QuadR too: Try it online! \$\endgroup\$
    – Adám
    Commented May 30, 2018 at 9:43
2
\$\begingroup\$

TI-Basic, 3 bytes

√(AnsAns

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

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ (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. \$\endgroup\$
    – Giuseppe
    Commented May 30, 2018 at 9:46
  • \$\begingroup\$ Although of course abs in R returns the magnitude of complex inputs, so none of these quite works the same as the R version. \$\endgroup\$
    – Giuseppe
    Commented May 30, 2018 at 9:49
1
\$\begingroup\$

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

Old 48 bytes answer:

[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
  Jump to function PRINT
else
  Jump to function PRINT

function PRINT:
  Print i as integer to STDOUT
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 2 bytes

nt

Try it online.

Explanation:

n     # Square
      #  i.e. 5 → 25
      #  i.e. -5 → 25
 t    # Square-root
      #  25 → 5
\$\endgroup\$
0
\$\begingroup\$

sed, 5 bytes

s/-//

Strip the negative sign

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

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

\$\endgroup\$
2
  • \$\begingroup\$ I don't think there is any restriction for -n \$\endgroup\$ Commented May 30, 2018 at 9:47
  • \$\begingroup\$ @Therandomguy There is, but it's in the comments instead of the challenge.. \$\endgroup\$ Commented May 30, 2018 at 9:57
0
\$\begingroup\$

J, 2 bytes

Anonymous tacit prefix function.

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

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

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.

\$\endgroup\$
1
  • \$\begingroup\$ Also ⊢∧× and ⊢∨- \$\endgroup\$
    – H.PWiz
    Commented May 30, 2018 at 10:08
0
\$\begingroup\$

Python 2, 18 bytes

lambda x:max(-x,x)

Try it online!

Previous (20 bytes)

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

Try it online!

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

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

\$\endgroup\$

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