# Why does Nearest not support Sqrt?

Bug introduced in 10.0.2 and fixed in 10.3.0

Simple examples with two points

nf1=Nearest[{{0, 0}, {Sqrt[2], Sqrt[2]}}]

nf1[{0, 0}, {All, 2.1}]


gives

{{0, 0}}


This is incorrect. If we test bigger radius, we can find that Mathematica takes 4 as critical radius. Because nf1[{0, 0}, {All, 3.999}] gives {{0,0}} and nf1[{0, 0}, {All, 4}] gives {{0, 0}, {Sqrt[2], Sqrt[2]}}. Why?

While if we use N

nf2 = Nearest[N@{{0, 0}, {Sqrt[2], Sqrt[2]}}]
nf2[{0, 0}, {All, 2.1}]


{{0., 0.}, {1.41421, 1.41421}}


Why Nearest gives wrong answer with irrational number? And If Nearest doesn't support irrational number, is it possible to write another Nearest function that has the same behavior and same efficiency while supporting irrational number directly.

• Oct 9, 2015 at 14:35
• I also tested the workaround I gave in that question on your problem. nf=Nearest[{{0,0},(Sqrt[2],Sqrt[2]}}, DistanceFunction -> (Norm[#1 - #2] &)] generates a NearestFunction which returns the expected points for nf[{0, 0}, {All, 2.1}] (at least in v10.2). Oct 9, 2015 at 14:40
• This bug has been fixed in the development version. Oct 9, 2015 at 14:53
• Development version is 10.2+a decimal to be named later Oct 9, 2015 at 15:09
• Yes, the fix should be available in the next release. Oct 9, 2015 at 15:18

\$Version