# The Crow vs The Taxicab

Imagine travelling to a point lying A miles away horizontally and B miles away vertically from your current position. Or in other words, travelling from (0, 0) to point (a, b). How far would you need to end up travelling? This seems like a straightforward question, but the answer depends on who you ask. If you're a crow, and you can travel as the crow flies, the distance travelled is just the Euclidean distance to (a, b). This is

sqrt(a^2 + b^2)

But if you're just a boring human, you don't really want to walk that far, so you'll need to take a taxi. Most taxis wont drive in a straight line towards your destination because they generally try to stay on the roads. So the real distance you'll end up travelling is the sum of the vertical distance and the horizontal distance. Or the formula is:

abs(a) + abs(b)

This is called the Taxicab Distance. This picture nicely demonstrates the difference between the two:

To travel to (6, 6), a crow can just fly on the green line, and this gives a distance of 6 * sqrt(2) or roughly 8.49. A taxicab can take the red, blue or yellow paths, but they will all take 12.

This leads to the real question I'm asking. If a crow and a taxicab both leave from point (0, 0), and travel to point (a, b), how much longer is the taxicab's path? Or, in more math jargon,

Given a two dimensional vector, determine the difference between the norm2 of the vector, and the norm1 of the vector.

You must write the shortest possible program or function to answer this question. You may choose to take 'a' and 'b' as two separate inputs, or as a two item tuple. You can take input and output in any reasonable format. If the difference is a non integer, you must be accurate to at least two decimal places.

You can always assume that 'a' and 'b' will be integers, and that they won't both be 0. (Though it is possible that either one of them will be zero)

As usual, standard loopholes apply and try to make your program as short as possible, counted in bytes.

I will upvote any answer that posts an explanation of how the code works, and demonstrates any cool tricks used to save bytes.

Here are some examples for you to test your code on:

#input    #output
3, 4      2
-3, 4     2
-3, -4    2
6, 6      3.51
42, 0     0
10, 10    5.86
3, 3      1.76

Have fun golfing! :)

• can we take input as complex number? Commented Jul 8, 2017 at 20:45
• I think the testcase for 10,10 needs to be 5.86, since it comes out to 5.85786... and you rounded the one below it. Commented Jul 9, 2017 at 5:31
• I first read the title as The Cow vs The Taxicab and was hoping to find something involving collision physics... Commented Jul 10, 2017 at 6:02
• May we give negative results?
Commented Jul 10, 2017 at 22:11
• @Adám No. (Since conceptually, you're returning a distance, which is always positive) Commented Jul 10, 2017 at 22:13

# Japt, 7 bytes

Takes input as an array.

x²¬nUxa

Try it (includes all test cases)

x²¬nUxa     :Implicit input of array U
x           :Sum of
²          :  Squares
¬         :Square root
n        :Subtract from
Uxa     :  Sum of absolute values of U

# Husk, 7 bytes

-√ṁ□¹ṁa

Try it online!

-√ṁ□¹ṁa
ṁa     # sum of absolute values
-           # minus
√ṁ□¹       # square root of sum of squares

# Pari/GP, 25 bytes

x->n=normlp;n(x,1)-n(x,2)

Try it online!

# Factor, 27 bytes

[ dup l1-norm swap norm - ]

Try it online!

# Thunno 2, 6 bytes

ÆH¤AS_

Try it online!

#### Explanation

ÆH¤AS_  # Implicit input
¤AS   # abs(a) + abs(b)
ÆH   _  #                 - hypot(a, b)
# Implicit output

# Vyxal, 6 bytes

₌∆/ȧ∑ε

Try it Online!

#### Explanation

₌∆/ȧ∑ε  # Implicit input
₌       # Parallel apply:
∆/     #  Hypotenuse
ȧ    #  Absolute value
∑   # Sum the absolute values
ε  # Absolute difference
# Implicit output

# Desmos, 33 bytes

f(a,b)=abs(a)+abs(b)-\sqrt{aa+bb}

Try it on Desmos!

# Casio-Basic, 27 bytes

abs(a)+abs(b)-norm([a,b]

Turns out you can just leave off that last bracket and it'll work fine. Having norm as a built-in makes it even shorter!

24 bytes for the code, 3 bytes to add a,b as parameters.

# QBIC, 27 bytes

?abs(:)+abs(:)-sqr(a^2+b^2)

# Scala 8, 48 bytes

a=>b=>Math.abs(a)+Math.abs(b)-Math.sqrt(a*a+b*b)

Explained:

a=>b=>                   A curried function with two parameters, 'a' and 'b'.
Math.abs(a)              The absolute value of 'a'.
+Math.abs(b)             Plus the absolute value of 'b'.
-Math.sqrt(a*a+b*b)      Minus the squareroot of 'a' squared plus 'b' squared.

I'm not very experienced with Scala for there is probably a few improvements that could be made.

# ExtraC, 82 bytes

import math
lambda dbl f,x,y,abs(x)plus abs(y)minus sqrt(x times x plus y times y)

Try it online!

Presenting my new language, ExtraC! It's supposed to look like python, but it's really just C. Oh, and it has almost no symbols available to it.

Simply put, this imports the math library (for sqrt), and defines a function f which takes two integers and returns a double.

Example usage:

import math
lambda dbl f,x,y,abs(x)plus abs(y)minus sqrt(x times x plus y times y)
// end submission
disp(f(x, y))

Which, for input 3 3 would output: 1.75736 Try it online!

# PHP 7, 109 bytes (insane variant)

$a[0]=($s=array_sum)($a=($m=array_map)(function($n){return$n*$n;},$argv));
$a=$m(sqrt,$a); echo$s([-$a[0]]+$a);

linebreaks are for reading convenience only. Run with -nr.

# Perl 5, 41 + 2 (-pa) = 43 bytes

$_=abs($F[1])-sqrt($F[0]**2+$F[1]**2)+abs

Try it online!

# Java 7, 92 73 bytes

double c(int a,int b){return Math.abs(a)+Math.abs(b)-Math.sqrt(a*a+b*b);}

Ungolfed, I think it is self explanatory:

double c(int a,int b){
return Math.abs(a)+Math.abs(b)-Math.sqrt(a*a+b*b);
}
• Suggest return(a<0?-a:a)+(b<0?-b:b)-Math.hypot(a,b); instead of return Math.abs(a)+Math.abs(b)-Math.sqrt(a*a+b*b); Commented Nov 22, 2019 at 20:58