home.social

Search

4 results for “simontatham”

  1. #TIL that in #SageMath you can accidentally write a comparison expression that doesn't do a comparison. (Reposted after the previous version was subtly wrong)

    x = QQbar(UniversalCyclotomicField().gen(3))
    if x == (-1 + sqrt(3)*i) / 2:
    print("All is well")
    if x != (-1 + sqrt(3)*i) / 2:
    print("Disaster!")

    The canonical primitive cube root of unity really is (−1+i√3)/2. So we expect "All is well" and not "Disaster". But in fact we get both:

    $ sage z.sage
    All is well
    Disaster!

    Turns out this is because when you compare a QQbar value with some more general algebraic expression, it doesn't do a comparison. It constructs a symbolic expression object that just represents the _notion_ of equality or inequality:

    sage: print(x != (-1 + sqrt(3)*i) / 2)
    (-0.500000000000000? + 0.866025403784439?*I) != 1/2*I*sqrt(3) - 1/2

    And those expression objects can evaluate true in a boolean context, simply because they aren't None or False. But they don't _always_. The == comparison seems to work OK, but the != evaluates true unconditionally. #wat

    (The workaround is to coerce the RHS of the expression to QQbar too: 'if x != QQbar(...)'. Then you're comparing elements of the same field and Sage is happy.)

  2. that in you can accidentally write a comparison expression that doesn't do a comparison. (Reposted after the previous version was subtly wrong)

    x = QQbar(UniversalCyclotomicField().gen(3))
    if x == (-1 + sqrt(3)*i) / 2:
    print("All is well")
    if x != (-1 + sqrt(3)*i) / 2:
    print("Disaster!")

    The canonical primitive cube root of unity really is (−1+i√3)/2. So we expect "All is well" and not "Disaster". But in fact we get both:

    $ sage z.sage
    All is well
    Disaster!

    Turns out this is because when you compare a QQbar value with some more general algebraic expression, it doesn't do a comparison. It constructs a symbolic expression object that just represents the _notion_ of equality or inequality:

    sage: print(x != (-1 + sqrt(3)*i) / 2)
    (-0.500000000000000? + 0.866025403784439?*I) != 1/2*I*sqrt(3) - 1/2

    And those expression objects can evaluate true in a boolean context, simply because they aren't None or False. But they don't _always_. The == comparison seems to work OK, but the != evaluates true unconditionally.

    (The workaround is to coerce the RHS of the expression to QQbar too: 'if x != QQbar(...)'. Then you're comparing elements of the same field and Sage is happy.)

  3. #TIL that in #SageMath you can accidentally write a comparison expression that doesn't do a comparison. (Reposted after the previous version was subtly wrong)

    x = QQbar(UniversalCyclotomicField().gen(3))
    if x == (-1 + sqrt(3)*i) / 2:
    print("All is well")
    if x != (-1 + sqrt(3)*i) / 2:
    print("Disaster!")

    The canonical primitive cube root of unity really is (−1+i√3)/2. So we expect "All is well" and not "Disaster". But in fact we get both:

    $ sage z.sage
    All is well
    Disaster!

    Turns out this is because when you compare a QQbar value with some more general algebraic expression, it doesn't do a comparison. It constructs a symbolic expression object that just represents the _notion_ of equality or inequality:

    sage: print(x != (-1 + sqrt(3)*i) / 2)
    (-0.500000000000000? + 0.866025403784439?*I) != 1/2*I*sqrt(3) - 1/2

    And those expression objects can evaluate true in a boolean context, simply because they aren't None or False. But they don't _always_. The == comparison seems to work OK, but the != evaluates true unconditionally. #wat

    (The workaround is to coerce the RHS of the expression to QQbar too: 'if x != QQbar(...)'. Then you're comparing elements of the same field and Sage is happy.)

  4. #TIL that in #SageMath you can accidentally write a comparison expression that doesn't do a comparison. (Reposted after the previous version was subtly wrong)

    x = QQbar(UniversalCyclotomicField().gen(3))
    if x == (-1 + sqrt(3)*i) / 2:
    print("All is well")
    if x != (-1 + sqrt(3)*i) / 2:
    print("Disaster!")

    The canonical primitive cube root of unity really is (−1+i√3)/2. So we expect "All is well" and not "Disaster". But in fact we get both:

    $ sage z.sage
    All is well
    Disaster!

    Turns out this is because when you compare a QQbar value with some more general algebraic expression, it doesn't do a comparison. It constructs a symbolic expression object that just represents the _notion_ of equality or inequality:

    sage: print(x != (-1 + sqrt(3)*i) / 2)
    (-0.500000000000000? + 0.866025403784439?*I) != 1/2*I*sqrt(3) - 1/2

    And those expression objects can evaluate true in a boolean context, simply because they aren't None or False. But they don't _always_. The == comparison seems to work OK, but the != evaluates true unconditionally. #wat

    (The workaround is to coerce the RHS of the expression to QQbar too: 'if x != QQbar(...)'. Then you're comparing elements of the same field and Sage is happy.)