Ask Your Question

Revision history [back]

Nevermind, I found the answer in a question I had myself asked but apparently forgotten its lesson, about 5 years ago: "Incorrect result for comparison: precision issues?" (question 32371; I can't post links as this is a separate account and I can't merge the accounts) — the solution is to avoid the "<" operator for comparison (what a gotcha!), and define one's own smaller function:

def smaller(a,b):
    prec = 53
    while True:
        R = RealIntervalField(prec)
        try:
            R(a).intersection(R(b))
        except ValueError:
            return R(a) < R(b)
        else:
            print '{} bits of precision is not enough'.format(prec)
            prec += 1

Then it works:

sage: bool(smaller(1/47749, -5564456128*e + 15125759978))
53 bits of precision is not enough
54 bits of precision is not enough
55 bits of precision is not enough
56 bits of precision is not enough
57 bits of precision is not enough
58 bits of precision is not enough
59 bits of precision is not enough
60 bits of precision is not enough
61 bits of precision is not enough
62 bits of precision is not enough
63 bits of precision is not enough
64 bits of precision is not enough
65 bits of precision is not enough
True

You can turn off the print statement when not debugging.

Nevermind, I found the answer in a question I had myself asked but apparently forgotten its lesson, about 5 years ago: "Incorrect result for comparison: precision issues?" (question 32371; (https://ask.sagemath.org/question/32371/incorrect-result-for-comparison-precision-issues/; I can't post links as this is a separate account and I can't merge the accounts) — the solution is to avoid the "<" operator for comparison (what a gotcha!), and define one's own smaller function:

def smaller(a,b):
    prec = 53
    while True:
        R = RealIntervalField(prec)
        try:
            R(a).intersection(R(b))
        except ValueError:
            return R(a) < R(b)
        else:
            print '{} bits of precision is not enough'.format(prec)
            prec += 1

Then it works:

sage: bool(smaller(1/47749, -5564456128*e + 15125759978))
53 bits of precision is not enough
54 bits of precision is not enough
55 bits of precision is not enough
56 bits of precision is not enough
57 bits of precision is not enough
58 bits of precision is not enough
59 bits of precision is not enough
60 bits of precision is not enough
61 bits of precision is not enough
62 bits of precision is not enough
63 bits of precision is not enough
64 bits of precision is not enough
65 bits of precision is not enough
True

You can turn off the print statement when not debugging.

Nevermind, I found the answer in a question I had myself asked but apparently forgotten its lesson, about 5 years ago: "Incorrect result for comparison: precision issues?" (https://ask.sagemath.org/question/32371/incorrect-result-for-comparison-precision-issues/; I can't post links as this is a separate account and I can't merge the accounts) comparison (precision issues?)" at ask.sagemath.org/question/32371 — the solution is to avoid the "<" operator for comparison (what a gotcha!), and define one's own smaller function:

def smaller(a,b):
    prec = 53
    while True:
        R = RealIntervalField(prec)
        try:
            R(a).intersection(R(b))
        except ValueError:
            return R(a) < R(b)
        else:
            print '{} bits of precision is not enough'.format(prec)
            prec += 1

Then it works:

sage: bool(smaller(1/47749, -5564456128*e + 15125759978))
53 bits of precision is not enough
54 bits of precision is not enough
55 bits of precision is not enough
56 bits of precision is not enough
57 bits of precision is not enough
58 bits of precision is not enough
59 bits of precision is not enough
60 bits of precision is not enough
61 bits of precision is not enough
62 bits of precision is not enough
63 bits of precision is not enough
64 bits of precision is not enough
65 bits of precision is not enough
True

You can turn off the print statement when not debugging.

Nevermind, I found the answer in a question I had myself asked but apparently forgotten its lesson, about 5 years ago: "Incorrect result for comparison (precision issues?)" at ask.sagemath.org/question/32371 — the solution is to avoid the "<" operator for comparison (what a gotcha!), and define one's own smaller function:

def smaller(a,b):
    '''Returns True if a < b (and False if b < a). Assumes that a != b.'''
    prec = 53
    while True:
prec < 1000000:
        R = RealIntervalField(prec)
        try:
            R(a).intersection(R(b))
        except ValueError:
            return R(a) < R(b)
        else:
            print #print '{} bits of precision is not enough'.format(prec)
            prec += 1
prec // 10
    raise Exception('A million bits of precision was not enough for: ', a, b)

Then it works:works correctly:

sage: bool(smaller(1/47749, -5564456128*e + 15125759978))
53 bits of precision is not enough
54 bits of precision is not enough
55 bits of precision is not enough
56 bits of precision is not enough
57 bits of precision is not enough
58 bits of precision is not enough
59 bits of precision is not enough
60 bits of precision is not enough
61 bits of precision is not enough
62 bits of precision is not enough
63 bits of precision is not enough
64 bits of precision is not enough
65 bits of precision is not enough
True

You can turn off the print statement when not debugging.

Nevermind, I found the answer a work-around in a question I had myself asked but about 5 years ago (but apparently forgotten its lesson, about 5 years ago: lesson): "Incorrect result for comparison (precision issues?)" at ask.sagemath.org/question/32371 — the solution is to avoid the "<" operator for comparison (what a gotcha!), and define one's own smaller function:

def smaller(a,b):
    '''Returns True if a < b (and False if b < a). Assumes that a != b.'''
    prec = 53
    while prec < 1000000:
        R = RealIntervalField(prec)
        try:
            R(a).intersection(R(b))
        except ValueError:
            return R(a) < R(b)
        else:
            #print '{} bits of precision is not enough'.format(prec)
            prec += prec // 10
    raise Exception('A million bits of precision was not enough for: ', a, b)

Then it works correctly:

sage: bool(smaller(1/47749, -5564456128*e + 15125759978))
True

I found a work-around in a question I had myself asked about 5 years ago (but apparently forgotten its lesson): "Incorrect result for comparison (precision issues?)" at ask.sagemath.org/question/32371 — the solution is to avoid the "<" operator for comparison (what a gotcha!), and define one's own smaller function:

def smaller(a,b):
smaller(a, b):
    '''Returns True if a < b (and False if b < a). Assumes that a != b.'''
    prec = 53
    while prec < 1000000:
100000:
        R = RealIntervalField(prec)
        try:
            R(a).intersection(R(b))
        except ValueError:
            return R(a) < R(b)
        else:
            #print '{} bits of precision is not enough'.format(prec)
            prec += prec // 10
    raise Exception('A million Exception('100000 bits of precision was not enough for: for comparing: ', a, b)
b, ' -- Possibly they are equal')

Then it works correctly:

sage: bool(smaller(1/47749, -5564456128*e + 15125759978))
True

Note that this function assumes that a ≠ b:

sage: smaller(pi/4, 4*arctan(1/5) - arctan(1/239))
...
Exception: ('100000 bits of precision was not enough for comparing: ', 1/4*pi, 4*arctan(1/5) - arctan(1/239), ' -- Possibly they are equal')