# exact factorting Anonymous

How do you get sage to factor into exact values. For instance, I want it to factor x^2-2 and return (x-sqrt(2))*(x+sqrt(2))

However, when I input

realpoly.<x> = PolynomialRing(CC) factor(x^2-2,x)

Sage returns

(x - 1.41421356237310) * (x + 1.41421356237310)

Any ideas?

edit retag close merge delete

Sort by » oldest newest most voted

The notation CC (as well for RR) is misleading: CC does not corresponds to genuine complex numbers (unlike for NN, ZZ, QQ, AA, QQbar), but only refers to floating-point approximation of complex numbers.

In your case, if you want exact computations, you should work on the algebraic field QQbar instead of CC:

sage: QQbar
Algebraic Field
sage: realpoly.<x> = PolynomialRing(QQbar)
sage: factor(x^2-2,x)
(x - 1.414213562373095?) * (x + 1.414213562373095?)


This looks the same, but you should notice the question mark after 1.414213562373095, which means that 1.414213562373095? is only a representation of some algebraic number. You can check that it is really sqrt(2) as follows:

sage: s2 = x-factor(x^2-2,x)
sage: s2
1.414213562373095?
sage: s2 == sqrt(QQbar(2))
True

sage: QQbar(s2).as_number_field_element()
(Number Field in a with defining polynomial y^2 - 2,
a,
Ring morphism:
From: Number Field in a with defining polynomial y^2 - 2
To:   Algebraic Real Field
Defn: a |--> 1.414213562373095?)

more