11
$\begingroup$

If I input

Internal`$SameQTolerance
(* output = 0.30103 *)

which is the approximation of Log[10,2], or Log[2]/Log[10].

I also noticed that

SameQ[1.000000000000000,1.000000000000001] (numbers differ by the 15th and final digit) reads False, while

SameQ[1.0000000000000000,1.0000000000000001] (numbers differ by the 16th and final digit) reads True.

What exactly does a tolerance of Log[2]/Log[10] mean? I read from this post that the tolerance is "Log[2]/Log[10] times the number of least significant bits one wishes to ignore." What does that even mean (I'm confused especially by 'least significant bits'), and why does it not take into effect until the number of digits to the right of the decimal reaches 16+?

$\endgroup$

1 Answer 1

11
$\begingroup$

As the old documentation states:

  • $EqualTolerance gives the number of decimal digits by which two numbers can disagree and still be considered equal according to Equal.

  • The default setting is equal to Log[10, 2^7], corresponding to a tolerance of 7 binary digits.

On my system $MachinePrecision is ~15.9546 which means there are 53 bits:

 Log10[2^53] == $MachinePrecision
True

Let's look at the 53 bits of two different machine numbers, a, and b:

a = .1234567890123456;
b = .1234567890123453;

Subtract @@ RealDigits[{a, b}, 2, 53] // First

Length @ First @ Split @ %
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, -1, 0, 1}

48

So the first 48 bits are the same and the fifth least significant bit is different.

Internal`$EqualTolerance = N @ Log10[2^4];
a == b

Internal`$EqualTolerance = N @ Log10[2^5];
a == b
False

True

$SameQTolerance behaves the same only it applies to SameQ rather than Equal:

Internal`$SameQTolerance = Log10[2`^4];
a === b

Internal`$SameQTolerance = Log10[2`^5];
a === b
False

True

Arbitrary precision

I realized that my chosen example of machine precision numbers might be misinterpreted. The same mechanism also applies to arbitrary precision numbers. Here we have numbers with 84 binary digits, the first 70 of each being identical meaning the fourteenth least significant digit varies.

a = 1234567.8901234567890123456;
b = 1234567.8901234567890111111;

Subtract @@ RealDigits[{a, b}, 2] // First;

Length @ First @ Split @ %

Length @ %%
70

84
Internal`$EqualTolerance = N@Log10[2^13];
a == b

Internal`$EqualTolerance = N@Log10[2^14];
a == b
False

True
$\endgroup$
6
  • $\begingroup$ Thank you so much for posting this. I was confused at first about what you did after defining a and b, but I worked through each individual command, starting with RealDigits[a], then RealDigits[a,2], then RealDigits[a,2,53], etc. From just your 1 post, I was able to learn the effects of each of these functions, as well as how to use shorthand notation to write them succinctly. Thank you very much :) $\endgroup$
    – user155812
    Commented Jun 25, 2015 at 12:47
  • 1
    $\begingroup$ @AisforAmbition You're welcome, and I congratulate you on working through unknown syntax. I feel that effort will pay off. I am almost always happy to explain code I write so if you need don't hesitate to ask. $\endgroup$
    – Mr.Wizard
    Commented Jun 25, 2015 at 12:51
  • $\begingroup$ Near the end of your post, where you configure the Internal`$SameQTolerance, what you're doing is to see if they are the same up to the 4th and 5th least significant digits, respectively, correct? $\endgroup$
    – user155812
    Commented Jun 25, 2015 at 12:54
  • 1
    $\begingroup$ @AisforAmbition Yes, that is correct. I added a section to my answer to demonstrate that the same things apply to arbitrary precision numbers, by the way. $\endgroup$
    – Mr.Wizard
    Commented Jun 25, 2015 at 13:04
  • 1
    $\begingroup$ @AisforAmbition It is my understanding that that is correct. By the way this is a good read: (6169506) $\endgroup$
    – Mr.Wizard
    Commented Jun 25, 2015 at 13:21

Your Answer

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

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