Ask Your Question

# Why is sage giving me wrong answers?

Let's say I want to multiply $(\frac{-1 - i \sqrt{3}}{2})(\frac{-1 + i \sqrt{3}}{2})$. Doing it by hand, we easily see that the answer is 1.

However, when I type

n(((-1 + sqrt(-3))/2)*((-1-sqrt(-3))/2))

in sage, I get

1 - 5.55111512312578 x 10^(-17)i

which is (of course) very close to 1...but its not exactly 1. Why is that? How do I fix this??

edit retag close merge delete

## Comments

Note that if you use a slightly different expression (with simply remove parentheses), you get the correct answer. In the following example, I just removed the (useless) parentheses around the fractions, and you get the same answer if you replace the two divisions by $2$ by a division by $4$.

sage: n(((-1-i*sqrt(3))/2*(-1+i*sqrt(3))/2))
1.00000000000000

( 2015-04-11 13:03:00 +0200 )edit

## 2 Answers

Sort by » oldest newest most voted

Default complex numbers use double precision floating points and their rounding errors (which can't be avoided).

Use variable from the right field and arithmetic specific to complex numbers

var('t,v,a,b', domain="complex")
v=sqrt(-3);
a=(-1+v)/2;
b=conjugate(a);
t = a*b;

v;
a;
b;
t.real();
t.imag();


displays

sqrt(-3)
1/2*sqrt(-3) - 1/2
1/2*conjugate(sqrt(-3)) - 1/2
1
0

more

## Comments

@Pierre Thanks a lot. It works now...but I don't really understand what you mean when you say "Do not use floating points". I just wrote the numbers as they were...what do you mean by "floating points"?

( 2015-04-11 01:08:45 +0200 )edit

Sage complex numbers are based on floating points, 1+2i is implemented as 1.00000000 + 2.00000000000 * i.

( 2015-04-11 01:34:50 +0200 )edit

OOOPs it doesn't work

i=CC(-3);

j=CC(-1);

k=CC(2);

t = ((j + sqrt(i))/k)*((j - sqrt(i))/k);

t.real();

t.imag();


The error moved into he imaginary part (approx 0.5 E-16).

I must edit the answer .....

( 2015-04-11 01:37:57 +0200 )edit

Can be written this way too

var('t,v,a,b', domain="complex");
v=sqrt(-3);   a=(-1+v)/2;  b=(-1-v)/2;
t=a*b; t.real(); t.imag();


or this way

var('t,v', domain="complex");
v=sqrt(-3);
t=((-1+v)/2)*((-1-v)/2);
t.expand().factor();

( 2015-04-11 02:08:41 +0200 )edit

There is a similar question in this thread

http://ask.sagemath.org/question/1006...

( 2015-04-11 02:30:20 +0200 )edit

You can directly try to simplify the symbolic expresssion, instead of looking for its numerical approximation (which is the of role the n() function):

sage: a = ((-1 + sqrt(-3))/2)*((-1-sqrt(-3))/2)
sage: a
-1/4*(sqrt(-3) + 1)*(sqrt(-3) - 1)
sage: a.parent()
Symbolic Ring
sage: a.full_simplify()
1


Now, if you follow the link provided by @Pierre last comment, you will see that Sage is not very reliable concerning Symbolic expressions (there are also undecidability results behind this). An alternative is to use the Algebraic Field which is much more reliable:

sage: a = QQbar((-1 + sqrt(-3))/2*(-1-sqrt(-3))/2)
sage: a
1.000000000000000? + 0.?e-19*I
sage: a.parent()
Algebraic Field
sage: a.simplify()
sage: a
1

more

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2015-04-11 00:39:58 +0200

Seen: 453 times

Last updated: Apr 11 '15