10
$\begingroup$

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}]

gives correct answer

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

$\endgroup$
10
  • 1
    $\begingroup$ Pssoibly related. $\endgroup$
    – IPoiler
    Oct 9, 2015 at 14:35
  • 2
    $\begingroup$ 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). $\endgroup$
    – IPoiler
    Oct 9, 2015 at 14:40
  • 11
    $\begingroup$ This bug has been fixed in the development version. $\endgroup$
    – ilian
    Oct 9, 2015 at 14:53
  • 5
    $\begingroup$ Development version is 10.2+a decimal to be named later $\endgroup$ Oct 9, 2015 at 15:09
  • 6
    $\begingroup$ Yes, the fix should be available in the next release. $\endgroup$
    – ilian
    Oct 9, 2015 at 15:18

1 Answer 1

3
$\begingroup$

As mentioned in the comments, this bug has been fixed as of Mathematica 10.3.

$Version

(* "10.3.0 for Microsoft Windows (64-bit) (October 9, 2015)" *)

nf1 = Nearest[{{0, 0}, {Sqrt[2], Sqrt[2]}}];
nf1[{0, 0}, {All, 2.1}]

(* {{0, 0}, {Sqrt[2], Sqrt[2]}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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