You could use the attributes of a Triangle object directly (of which there 
are many, including area):

>>> p=Point
>>> A = p(0, 0)
>>> B = p(3, 0)
>>> C = p(3, 3)
>>> Triangle(A,B,C).area
9/2

or simplify your "manual" result:

>>> simplify(sqrt(-3*sqrt(2)/2 + 3)*sqrt(27*sqrt(2)/4 + S(27)/2))
9/2

/c

On Sunday, May 17, 2015 at 3:07:48 PM UTC-5, ravas wrote:
>
> My goal is to input the coordinates of the vertices of a triangle and have 
> significant properties printed for a human to read.
> I'm aware of the limitation of floating point arithmetic, and I was able 
> to produce an accurate area with the decimal module.
>
> return (sp*(sp-a)*(sp-b)*(sp-c)).sqrt().quantize(dec('1.0000000000'))
>
> However, a version using sympy is also desirable. Do I need to install 
> mpmath / gmpy to compensate for the floating point errors?
> Here is the test code:
>
> import sympy.functions as sym
> import sympy.geometry as geo
>
> sqrt = sym.sqrt
>
> def distance(A, B):
>  """
>  A & B are objects with x and y attributes
>  :return: the distance between A and B
>  """
>  dx = B.x - A.x
>  dy = B.y - A.y
>  return sqrt(dx**2 + dy**2)
>
> def sides(A, B, C):
>  """
>  A, B & C are objects with x and y attributes
>  :return: sorted side lengths (smallest to largest)
>  """
>  a = distance(B, C)
>  b = distance(A, C)
>  c = distance(A, B)
>  return sorted((a, b, c,), key=float)
>
> def area(a, b, c):
>  """
>  a, b & c are the side lengths of the trigon
>  :return: area of the trigon
>  """
>  p = a + b + c # perimeter
>  sp = p / 2 # semi-perimeter
>  return sqrt(sp*(sp-a)*(sp-b)*(sp-c))
>
> if __name__ == '__main__':
>  p = geo.Point
>  A = p(0, 0)
>  B = p(3, 0)
>  C = p(3, 3)
>  sides = sides(A,B,C)
>  print(sides)
>  print(area(*sides))
>
> Output: 
>
> [3, 3, 3*sqrt(2)]
> sqrt(-3*sqrt(2)/2 + 3)*sqrt(27*sqrt(2)/4 + 27/2)
>
> Obviously I want 9/2 as the result for the area... but it is giving me a 
> representation of 4.4999999999...
> What can I do about this?
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/9a861bc0-74d1-4b66-808b-09a97e1eb46d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to