[Question]
  [
Given two integers, A and B, output A if A-B (A minus B) is in A-B (A to B), otherwise output B.
"A minus B" is standard subtraction.
"A to B" is the range of integers starting at A and ending at B, including both A and B. For example:
```
1 to 4: 1, 2, 3, 4
-2 to 5: -2, -1, 0, 1, 2, 3, 4, 5
3 to -1: 3, 2, 1, 0, -1
7 to 7: 7
```
**The shortest code in bytes wins.**
## Test Cases
```
A B Output
1 4 4
-2 5 5
3 -1 -1
7 7 7
90 30 90
90 -30 -30
-90 30 30
-90 -30 -90
-2 -2 -2
-2 -1 -2
-2 0 -2
-2 1 1
-2 2 2
-1 -2 -2
-1 -1 -1
-1 0 -1
-1 1 1
-1 2 2
0 -2 -2
0 -1 -1
0 0 0
0 1 1
0 2 2
1 -2 -2
1 -1 -1
1 0 1
1 1 1
1 2 2
2 -2 -2
2 -1 -1
2 0 2
2 1 2
2 2 2
```
[Original Chat-Mini-Challenge](https://chat.stackexchange.com/transcript/message/35909025#35909025)
  
[Answer]
# [Python](https://docs.python.org/2/), 27 bytes
```
lambda a,b:[a,b][2*b*b>a*b]
```
[Try it online!](https://tio.run/nexus/python2#VU/RaoQwEHyuXzEVBLWxJNpSTrC/0A@wPiSecsJVD6Mt9/U2MeaMsBtmdndms23xvVz5jzhzcCLyUj1VmcYiFp88FtXSjOMwShSgXjuMeobU6HoVt3kKo9zDvWhDVY48dC3q543l8J5uY9dP8L/mSc1Cy8NAkkBG@OMSCkJehvl6xoX/NhBN06viqx9oA3IndaQ8tv0vBZi3@QUSpuoj2BCWsmQEbyoqgjJJCd5VaJwRJEynJh8EOjQ8UYJM5YlalmTmWR1s26Gmb@b1BpMPxlxGXaI6zGIdK2YHA@b@UiPqkocB2w2oq6eunK5qukGrpbv0sPqw2Sy2kO3QSg9np67UHG3rbIertPoH "Python 2 – TIO Nexus")
An arithmetic formula. Why is the negation of `2*b*b>a*b` equivalent to the problem condition `a-b in symrange(a,b)`?
Note that `x in symrange(a,b)` is equivalent to `0 in symrange(a-x,b-x)`. Applying this to `x=a-b` gives `0 in symrange(b,2*b-a)`. The value `0` is included in the interval unless it stretches between two positive values or two negative values. This can be stated arithmetically as "their product `b*(2*b-a)` is not positive.
Finally, take `b*(2*b-a)<=0`, and rewrite to `2*b*b<=a*b`. A byte is saved by flipping `<=` to `>` and switching the cases.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
_erị,
```
[Try it online!](https://tio.run/nexus/jelly#TY6xDcJQEENXQal94u4HFEUZgCGiKBULMAItg9BkA0omSRb56OfbKJXtuyfLeb4/1s8LeWi@7/P2XG45j4HLhNNoCdeiLSyKduiK9I7Waaw6@9/scEywJBM0@ihXIESGyCAZUN4BJ@jkvGIOpv2pNpWxi1Vs0jht4zQuK8N@ "Jelly – TIO Nexus")
### How it works
```
_erị,  Main link. Arguments: a, b
_      Subtraction; Yield a-b.
  r    Range; yield [a, ..., b].
 e     Exists; yield 1 if a-b belongs to [a, ..., b], 0 if not.
    ,  Pair; yield [a, b].
   ị   At-index; yield the element of [a, b] at index 1 (a) or index 0 (b).
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 7 bytes
Code:
```
DŸ¹Æå_è
```
Uses the **CP-1252** encoding. [Try it online!](https://tio.run/nexus/05ab1e#@@9ydMehnYfbDi@NP7zi//9oSwMdBWODWAA "05AB1E – TIO Nexus")
Explanation:
```
D         # Duplicate the input.
 Ÿ        # Inclusive range.
  ¹Æ      # Push the input [a, b] and compute a - b.
    å     # Check if the number exists in the range.
     _    # Boolean negate
      è   # Index at the first input
```
[Answer]
# PHP, 58 Bytes
```
<?=in_array(($a=$argv[1])-$b=$argv[2],range($a,$b))?$a:$b;
```
[Answer]
# JavaScript (ES6), 24 bytes
```
a=>b=>[a,b][2*b*b>a*b|0]
```
## Test cases
```
let f = a=>b=>[a,b][2*b*b>a*b|0]
let tests = [[1,4,4],[-2,5,5],[3,-1,-1],[7,7,7],[90,30,90],[90,-30,-30],[-90,30,30],[-90,-30,-90],[-2,-2,-2],[-2,-1,-2],[-2,0,-2],[-2,1,1],[-2,2,2],[-1,-2,-2],[-1,-1,-1],[-1,0,-1],[-1,1,1],[-1,2,2],[0,-2,-2],[0,-1,-1],[0,0,0],[0,1,1],[0,2,2],[1,-2,-2],[1,-1,-1],[1,0,1],[1,1,1],[1,2,2],[2,-2,-2],[2,-1,-1],[2,0,2],[2,1,2],[2,2,2]]
tests.map(test => `f(${test[0]})(${test[1]}) == ${test[2]}`).forEach(test => console.log(test, eval(test)))
```
[Answer]
# Python 2, 37 bytes
```
lambda*s:s[min(s)<=s[1]-s[0]<=max(s)]
```
Invoke as `f(B, A)`.
[Answer]
# Python2, ~~55~~ ~~52~~ 51 bytes
```
lambda A,B:[B,A][A-B in range(min(A,B),max(A,B)+1)]
```
[Try it online!](https://tio.run/nexus/python2#VZDBboMwDIbP5Ck8LhAtTElpNbVSDnDYK@zAOKQU2mhAK@iBt2e2F6RNDr9/nM8mZO1s74bzxUGhylNVqqKuiqwEP8LkxmubDn5McUuqwS1sXo2s19nGcWxgD3uR7eAAB5FDZnCJd8AQRw25hqMmk@X8iOy3GBxXEcB2XmxMMDpkA4YShuA9LpvwJVQdMnOGOR0wHSiNkEYlRDOxDdrm0BhSw0rEdqRdIOhA9GZYMfDnRXefoLnRRc1v86P3zzT5GhN5EpFTZ9XYyo/PdJFA3EJYc9s4SGQtIt9BlyIrwVposC96TNQTf96n7/byArECHiVF1PZz@4f4cL7/R6w/)
Handles every test case OP has mentioned (at the time of posting this), as the TIO suggests.
[Answer]
# JavaScript ES6, 40 37 bytes
```
a=>b=>a-b<(a<b?a:b)|a-b>(a<b?b:a)?b:a
```
Explained:
```
a=>b=>                                   take a and b as inputs
      a-b<(a<b?a:b)                      if a-b is less than the lowest of a and b
                   |a-b>(a<b?b:a)        or a-b is greater than the largest of a and b
                                 ?b      return b
                                   :a    else return a
```
Saved 3 bytes thanks to Arnauld.
```
f=a=>b=>a-b<(a<b?a:b)|a-b>(a<b?b:a)?b:a
function t(){
    var tests = [[1,4,4],[-2,5,5],[3,-1,-1],[7,7,7],[90,30,90],[90,-30,-30],[-90,30,30],[-90,-30,-90],[-2,-2,-2],[-2,-1,-2],[-2,0,-2],[-2,1,1],[-2,2,2],[-1,-2,-2],[-1,-1,-1],[-1,0,-1],[-1,1,1],[-1,2,2],[0,-2,-2],[0,-1,-1],[0,0,0],[0,1,1],[0,2,2],[1,-2,-2],[1,-1,-1],[1,0,1],[1,1,1],[1,2,2],[2,-2,-2],[2,-1,-1],[2,0,2],[2,1,2],[2,2,2]];
    for (var test of tests) {
        console.log(`f(${test[0]},${test[1]}) == ${test[2]}`, f(test[0])(test[1])==test[2]);
    }
}
t();
```
[Answer]
# Mathematica, 16 bytes
```
If[2#^2>1##,##]&
```
Pure function taking two arguments in the opposite order as the OP (for example, `If[2#^2>1##,##]&[B,A]`). A port of [xnor's Python answer](https://codegolf.stackexchange.com/a/112334/56178).
[Answer]
## R, ~~49~~ ~~30~~ 28 bytes
```
pryr::f("if"(2*b*b>a*b,b,a))
```
Uses @xnor's logic for determining whether a-b is in a:b. 
[Answer]
# Clojure, ~~71~~ 41 bytes
-30 bytes by using `<=` and `min`/`max` instead of `range`s.
```
#(if(<=(min % %2)(- % %2)(max % %2))% %2)
```
Checks if `(a - b)` is in the range from `a` to `b`, dispatching a return accordingly.
```
(defn eh [a b]
  ; <= accepts any number of argments, and ensures all fall within the range
    (if (<= (min a b) (- a b) (max a b))
      a
      b))
```
[Answer]
# PHP (7.1), 55 bytes
uses the [new](http://php.net/manual/migration71.new-features.php) array destructuring syntax:
```
[,$a,$b]=$argv;echo in_array($a-$b,range($a,$b))?$a:$b;
```
Run with `-r`, provide numbers as command line arguments.
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), ~~37~~ ~~35~~ 32 bytes
```
($a,$b=$args)[$a-$b-notin$a..$b]
```
[Try it online!](https://tio.run/nexus/powershell#@6@hkqijkmSrkliUXqwZrZKoq5Kkm5dfkpmnkqinp5IU@///f12j/4YA "PowerShell – TIO Nexus")
Literal translation of the problem into PowerShell using the `-notin` operator. Saved three bytes by using multiple assignment and encapsulation. This works because `-` has a [higher operator precedence](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_operator_precedence) than `-notin`, and the `( )` portion of the code is executed first and returned as an array `@($a,$b)`. However, since it's `$a,$b` rather than `$b,$a`, we need to use `-notin` to flip/flop the output result.
[Answer]
# Nim, 60 bytes
```
proc f(a,b:int):int=
  if a-b in min(a,b)..max(a,b):a else:b
```
[Try it online!](https://tio.run/nexus/nim#XZLdioMwEIXvfYpDryrEkuguS4VA38P1IhZlhfUHW4pvbzOJ7iYDM@F8M8kxhtnmZbqjOxvRlP34TGnRCdB3MFmDfsTQj9RNL5fBrE6VBu3voy2b7WUWzGYxw0PjVtljt0oJfNiohaMsF/i0sWMhkCnKnb8EKHa6SoHC5lUGhazwy2F4bIorftffQfqsz7CgWEEytn0VIMWBivsp9icEknHopyI/yewkc5POTP5TYCUjJ34xfi9/rYBURIETf7KcOfkHC7oqIueU1Ek32ZGgufFzUdq@nRK7AkZjrmTtdENaeb2Szkm3958JRuD0/TwJNIdw45lCa6zbGw "Nim – TIO Nexus")
Pretty standard as far as answers go, no big tricks in this one.
[Answer]
## Batch, 107 bytes
```
@set/aa=%1,r=b=%2,x=a-b
@if %a% gtr %b% set/aa=b,b=%1
@if %a% leq %x% if %x% leq %b% set/ar=%1
@echo %r%
```
[Answer]
# Pyth - 9 bytes
Was hoping to use more implicit input... :(
```
@Q!}-FQ}F
```
[Try it online here](http://pyth.herokuapp.com/?code=%40Q%21%7D-FQ%7DF&test_suite=1&test_suite_input=1%2C+4%0A-2%2C+5&debug=0).
[Answer]
# [Röda](https://github.com/fergusq/roda), 30 bytes
```
f a,b{[b]if[2*b*b>a*b]else[a]}
```
[Try it online!](https://tio.run/nexus/roda#RY7RboMwDEWfl6@wEA/AQEqg1dRN7H3fQFGbsGRD6igiTKrU8e3M9oImJ9fOzYmd1YHOzb0xbe@aMjOZedWZae3F20a3y/ql@wHu4mH89p/JbP186rS3PoUf8OOlnxNvxzo6Hv1jRN6oJ2/fhtl@2AlOaLjrRAPyDt6vcKtdgof0BcapH2Y4uyTWeWxSqCG@kXRn5AYrFiFw6P88eK4hUrCDnShK2MNeVFAoXOIJMMRBQiXhIKkoKt6i@DNDxS4C@JwXFyoUMmQFihKG4Du2VZiEKkNmTjEnAyYDJRGSqIRIJrZGWx9qQ6pYidi@VAaCPkQnxYoRiWX9BQ "Röda – TIO Nexus")
It uses the formula used in xnor's answer.
Another solution (37 bytes):
```
f a,b{[a]if[a-b in[seq(a,b)]]else[b]}
```
[Try it online!](https://tio.run/nexus/roda#RY5hboMwDIV/L6d4QvygGkhJaTV1EwfYGShqAwsbUkcZYVKljrMz2wuanDw7L1/sLC1sWt9LW3VtabMaXV9695WQuakqd/GurKt5@bRdj7t6GL79RzI5P50a653f4Ad@uHRT4t1QRMejf4zYG@zo3Ws/uXc34kRGex15Ttrg7Ypb0Ur/Fwxj1084t0ls07jeoEB8Y2nOxPVOzUrR0P95eC4QGeywU9kWe@xVjszQUk@gUAeNXOOguchy2Sr7M0MlLgH0XJYUJhQ6ZAPDiULJndgmTCLVIQtnhNMB04HSBGlSRrQQa6O1D7dhNaJMrF/aBoI/xCcjShGpefkF "Röda – TIO Nexus")
[Answer]
# [><>](https://esolangs.org/wiki/Fish), 21 bytes
Makes use of @xnor's [trick](https://codegolf.stackexchange.com/a/112334/63647). We use `-v B A` to prepopulate the stack. (`-v A B` is +1 byte).
```
:01pr:11p::2**r*)1gn;
```
[Try it online!](https://tio.run/nexus/fish#@29lYFhQZGVoWGBlZaSlVaSlaZieZ/3//3/dMgUTEGEIAA "><> – TIO Nexus")
## Explanation
```
                        Input: [B, A] on stack.
:01pr:11p::2**r*)1gn;
:                       Duplicate.           [B, A, A]
 01p                    Push A to [0,1].     [B, A]
    r                   Reverse              [A, B]
     :                  Duplicate.           [A, B, B]
      11p               Push B to [1,1].     [A, B]
         ::             Duplicate x 2.       [A, B, B, B]
           2            Push 2.              [A, B, B, B, 2]
           2**          Compute 2*B*B.       [A, B, 2*B*B]
              r         Reverse.             [2*B*B, B, A]
               *        Compute A*B.         [2*B*B, A*B]
                )       >                    [2*B*B > A*B]
                 1      Push 1.              [2*B*B > A*B, 1]
                  g     If 2*B*B > A*B
                         get B, else get A.  [2*B*B > A*B ? B : A]
                   n    Output as number.
                    ;   Terminate.
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~27~~ 22 bytes
```
->a,b{(b*a<2*b*b)?b:a}
```
[Try it online!](https://tio.run/nexus/ruby#@59mq2uXqJNUrZGklWhjpJWklaRpn2SVWMvFVaCQFm1poGNsEAtj6sLYukjiulCJ//8B "Ruby – TIO Nexus")
Nothing innovative here. The simple math behind it:
```
(A<=A-B<=B or B<=A-B<=A)
```
can be written as
```
(B>=0 and A>=2B) or (B<=0 and A<=2B)
```
that is: if A-2B has the same sign as B, we are in the range.
[Answer]
# SpecBAS - 38 btes
```
1 INPUT a,b: ?IIF(a-b IN [a TO b],a,b)
```
`IIF` is an inline-IF-THEN-ELSE, to print the correct value.
[Answer]
# Haskell, 21 bytes
```
a!b|b*a<2*b*b=b|0<1=a
```
[Try it online!](https://tio.run/nexus/haskell#@5@omFSTpJVoY6SVpJVkm1RjYGNom/g/NzEzT8FWISWfSwEICooy80oUVBQMFRQVTFBENHSNNIGCpiiClgZAIWMDTDENXWMDzf8A)
## Readable
```
func :: Int -> Int -> Int
func a b
    | b*a < 2*b*b = b
    | otherwise = a
```
## Explanation
Uses @xnor's formula to check whether a-b is in range. Nothing special besides that.
[Answer]
## Haskell, 58 bytes
Just recently I fell in love with arrows again. Unfortunately they require us to work with tuples instead of binary functions. And of course Haskell does not have a symmetrical `range` function.
```
import Control.Arrow
u=uncurry
app<<<elem.u(-)&&&(u enumFromTo<<<u min&&&u max)
```
[Answer]
# PHP 7 - 45 Bytes
```
echo(in_array($a-$b,range($a,$b))?$a:$b)==$c;
```
[Answer]
# Octave, 55bytes
```
@(a,b)(c=b*~[find((a:1-2*(b<a):b)==(a-b)) 0](1))+(a*~c)
```
This could probably be optimised further. I'll add an explanation later.
[Answer]
## [Swift](https://swift.org/about/) - ~~38~~ ~~30~~ 22 bytes
Saved 8 bytes thanks to @Matt
```
print(a...b~=a-b ?a:b)
```
[Try it at IBM Swift Sandbox online!](http://swift.sandbox.bluemix.net/#/repl/58c18fe9f8ba897c77f93546)
---
## Or 21 bytes:
(thanks to **@xnor**'s formula) and saved 8 bytes thanks to @Matt
```
print(2*b*b>a*b ?a:b)
```
---
Swift is not the best language for golfing (it's very rigid), so if you see any other golfing opportunity, I'll totally edit the answer.
[Answer]
# Java 7, ~~84~~ ~~60~~ 58 bytes
```
int c(int a,int b){return(a<b?a:b)>a-b|(a<b?b:a)<a-b?b:a;}
```
# Java 8, 37 bytes
```
a->b->(a<b?a:b)>a-b|(a<b?b:a)<a-b?b:a
```
**Explanation:**
```
int c(int a, int b){          // method with two integer parameters and integer return-type
  return (a<b ? a : b) > a-b  //  if smallest of the input is larger than a-b
       | (a<b ? b : a) < a-b  //    or if the largest of the input is smaller than a-b
    ? b                       //   return b
    :                         //  else
      a                       //   return a
}                             // end of method
```
**Test code:** [Try it here.](https://ideone.com/mYsUsN)
[Answer]
# Ti-Basic (TI-84 Plus CE), ~~26 24~~ 23 bytes
```
Prompt A,B
A-B≥A and A-B≤B
AAns+Bnot(Ans
```
TI-Basic is a tokenized language; all tokens used are [one-byte tokens](http://tibasicdev.wikidot.com/one-byte-tokens).
`Prompt` prompts you for the two numbers.
`A-B≥A and A-B≤B` checks if A-B is between A and B (inclusive); this returns a 1 if true and a zero if false, which is stored to `Ans`.
Since we are returning A if A-B is between A and B, we multiply A by Ans, which will be A if we are supposed to return A, and 0 otherwise.
Next, we add `Bnot(Ans` to it. If Ans was 1 (truthy), we `not(` it and get 0, thus our sum is A. If Ans was 0 (falsy), we `not(` it to get 1, which we multiply by B and add to 0 to get B.
The last evaluation in TI-Basic is implicitly returned.
-2 bytes thanks to Scott Milner
[Answer]
# [Pyt](https://github.com/mudkip201/pyt), 32 bytes
```
←Đ←Đ3Ș⇹Đ3ȘĐ4Ș3Ș-3Ș⇹Ř∈Đ3Ș⇹¢*3Ș⇹*+
```
Takes A and B from stdin as two separate inputs
Explanation:
AABB -> ABBA -> ABAB -> ABABB -> ABBBA -> ABBBAA -> ABAABB -> ABABBA -> ABABC -> ABCBA -> ABCAB -> ABC[A,...,B] -> ABD -> ABDD -> ADDB -> ADBD -> AD{B\*(1-D)} -> {B\*(1-D)}AD -> {B\*(1-D)}+{A\*D}
where: C=B-A and D=C∈[A,...,B] (1 if true, 0 if false)
[Answer]
# [Ohm](https://github.com/MiningPotatoes/Ohm), 10 bytes (CP437)
There's probably a golfier way to do this, but the strictly-typed nature of Ruby makes this difficult.
```
G┼┘-îε?┼¿┘
```
[Answer]
# [Perl 6](https://perl6.org), ~~31 29~~ 24 bytes
```
{$^a-$^b==any($a...$b)??$a!!$b}
```
[Try it](https://tio.run/nexus/perl6#NY5NbsIwEIX3PsUDRSURcbAJtEIopLDrnmWElB9XRYIENaEqinKy7nqnrunY2Br7zeT5m8lcW4Wv56hcs/MNT2VTKST33jvk3DsUSZLXN9/LoyjyiiBNvXw08orhfqWmvWo70/TaUdUiwelYq9YPfn@isjkX/mzCJ2lWTWfaeasJvpzyGtMHv2bvzafr5Rsf2TZEtqOrvi@q7FSFoGfAsYVeyt@GuyCEewsx7skZkGzQO3MYs@EuscCC8TmWWLIYXNJhL6BgK4FYYCV0wWNzGX@YtjIuAdRujimkLYTNElInCmbejC3tn0iFzYaThhMWE5YSBAlSjQhDuEFujh6jVRrVhFtpbgm9kP6SRin@6oaXefmh/gE "Perl 6 – TIO Nexus")
```
{$^a-$^b∈($a...$b)??$a!!$b}
```
[Try it](https://tio.run/nexus/perl6#NY5NbsIwEIX3PsUDRSURcbAJtEIopLDrnmWElB9XRYIENaFqFWXPnbrrUXqHrtOxcTT2m8nzN5O51gofj0G@ZucvPORVoRD1rXNIuXPIfm8310mDIHAyL46ddDRysq6/Uste1Y1peW6oqhHhdCxV7Xo/30FenTN3NuGTOCmmM@28lARfTmmJ6Z1fs9fqfejlGxfJ1keyo6s@LypvVAGvZcCxhl7J3fo7z8fw5mPcktMh2qAdzG7Mul5igQXjcyyxZCG4pMOeQMFWAqHASuiCh@YyfjdtZVwCqN0cU0hbCJslpE4UzLwZW9o/kQqbDScNJywmLCUIEqQaEYYYBg1z9Bit0qgmhpXmltAL6S9plOKvrHie5m/qHw "Perl 6 – TIO Nexus")
```
->\a,\b{2*b*b>a*b??b!!a}
->\a,\b{2*b²>a*b??b!!a}
```
[Try it](https://tio.run/nexus/perl6#NY5NbsIwEIX3PsWDRUlCDHYCrRBKUth132U2@XFVJEhQE6pWUU7WXe/UdTo2tsZ@M37@PJ5bp/D5uKr27PKNh6qtFZKJp3kR5uUQBWVQpkVQZlk5mxXjdCP6VXW9oZ97qjokOJ8a1Xn@78@qai@lt17wRZbXy7V2XhqCr@eiwfLO79lb@@He8tRDfgiRH2mrr6uqelXDHxhw6qCn8Q7h0Q/h7kLMB3JGJCkGZ45zNk4SG2wYj7DFlsXgkhZ7AgXbCcQCO6ELHpvN@N20lXEJoOdmmULaQtgsIXWiYObO2NL@RCpsNpw0nLCYsJQgSJBqRBjCNXJ9dBut0qgm3EiRJfRA@iSNUvw1La@K6l39Aw "Perl 6 – TIO Nexus")
]