6
$\begingroup$

Can I specify some tolerance for the new geometric-computation function?

RegionMember[Line[{{0, 0}, {1, 0}}], {.5, 0}]
(* True *)

While:

RegionMember[Line[{{0, 0}, {1, 10^-100}}], {.5, 0}]
(* False *)

RegionMember[Line[{{0, 0}, {1, 10.^-100}}], {.5, 0}]
(* True *)

RegionMember[Line[{{0, 0}, {1, 10.^-7}}], {.5, 0}]
(* False *)
$\endgroup$
4
$\begingroup$

There's Internal`$EqualTolerance. See How to make the computer consider two numbers equal up to a certain precision and its reference.

Block[{Internal`$EqualTolerance = 9.},
 RegionMember[Line[{{0, 0}, {1, 10.^-7}}], {.5, 0}]
 ]
(* True *)

It's a relative tolerance, whereas gpap's & kguler's answers give absolute ones. The setting above, which is roughly equivalent to Internal`$EqualTolerance = $MachinePrecision - 7, says approximate numbers that agree to seven digits (or differ in at most the last nine) are to be considered equal.

Block[{Internal`$EqualTolerance = $MachinePrecision - 7},
 {1. + 1.0000001*^-7 == 1, 1. + 1*^-7 == 1}
 ]
(* {False, True} *)
$\endgroup$
  • $\begingroup$ Yes, this, temporarily overriding the equality criteria, if I understand it correctly. $\endgroup$ – BoLe Oct 17 '14 at 11:18
  • $\begingroup$ Upvote for trick.. $\endgroup$ – yode Apr 5 '17 at 10:33
7
$\begingroup$

This (or an appropriately written variant) works:

myRegionMember[a_, b_] := With[{threshold = 10^-6},
  If[
   RegionDistance[a, b] < threshold,
   True,
   False
   ]
  ]
$\endgroup$
3
$\begingroup$
f1 = RegionDistance[#, #2] < #3]&

f1[Line[{{0, 0}, {1, 10.^-7}}], {.5, 0}, 10^-6]
(* True *)

f2 = Chop[RegionDistance[#, #2] ,#3] == 0&

f2[Line[{{0, 0}, {1, 10.^-7}}], {.5, 0}, 10^-6]
(* True *)
$\endgroup$
1
$\begingroup$

Following gpap's answer:

Options[regionMember] = {Tolerance -> 0};

regionMember[reg_Line, p : {_?NumberQ, _?NumberQ},
  OptionsPattern[]] :=
 With[{e = OptionValue[Tolerance]},
  If[e == 0, RegionMember[reg, p], RegionDistance[reg, p] < e]]

line = Line[{{0, 0}, {1, 0}}];
ps = {.5, #} & /@ {0, 10^-100, 10.^-16, 10.^-5};

regionMember[line, #] & /@ ps
(* {True, False, False, False} *)

regionMember[line, #, Tolerance -> 10.^-6] & /@ ps
(* {True, True, True, False} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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