Why 1/3 == S(1)/3 is True ?

63 views
Skip to first unread message

Christophe Bal

unread,
Oct 2, 2014, 9:02:10 AM10/2/14
to sympy-list
Hello.

For me the fact that  1/3 == S(1)/3  has value  True  sounds like a bug because  1/3  is a float, and  S(1)/3  a rational.

Christophe BAL

=== PYTHON ===

from sympy import *

a = 1/3
b = S(1)/3

print(a)
print(b)

print(a == b)
print(type(a))
print(type(b))

--- OUTPUT ---

0.3333333333333333
1/3
True
<class 'float'>
<class 'sympy.core.numbers.Rational'>

Mateusz Paprocki

unread,
Oct 2, 2014, 9:32:16 AM10/2/14
to sympy
Hi,

On 2 October 2014 15:02, Christophe Bal <proj...@gmail.com> wrote:
> Hello.
>
> For me the fact that 1/3 == S(1)/3 has value True sounds like a bug
> because 1/3 is a float, and S(1)/3 a rational.>

Because 1/3 == S(1)/3 is equivalent to Float(1/3) == S(1)/3 and
apparently __eq__ prefers Float comparison instead of Rational, so you
get True. I think there are some reasons why this works this way, but
indeed it seems wrong.

Mateusz

> Christophe BAL
>
> === PYTHON ===
>
> from sympy import *
>
> a = 1/3
> b = S(1)/3
>
> print(a)
> print(b)
>
> print(a == b)
> print(type(a))
> print(type(b))
>
> --- OUTPUT ---
>
> 0.3333333333333333
> 1/3
> True
> <class 'float'>
> <class 'sympy.core.numbers.Rational'>
>
> --
> 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 sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> 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/CAAb4jGm%3DNV4uO9xdSqLGx1ou5AzETbwc_KdYYdOt2bgaRWKywg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Richard Fateman

unread,
Oct 2, 2014, 12:58:02 PM10/2/14
to sy...@googlegroups.com
Lisp has a variety of equality testing predicates.
EQ  for  " same memory location"
=   for "numeric equality"
There's also EQL, EQUAL, CHAR=, STRING=. ...  
One of the benefits of NOT having infix syntax for relations like "="  is that
it puts these others on a more equal footing, language-wise.

I suppose you might argue that "types" solve this problem, but you
would be wrong.  It is perfectly possible to want to know if two strings
that are string=   are also eq -- that is, in the same memory location.



RJF

Christophe Bal

unread,
Oct 2, 2014, 2:11:14 PM10/2/14
to sympy-list
And what about the following code ?

The user of Sympy must know that types are different and so that the variable are not the same things. A float is not a rational.

Just try the following code using a classical sequence showing a weakness of the floats.

=== PYTHON ===

from sympy import *

a = 1/3
b = S(1)/3

print(a == b)
print(type(a))
print(type(b))

for i in range(31):
    print(a, ";", b)
    a = 4*a - 1
    b = 4*b - 1

== OUTPUT ===

True

<class 'float'>

<class 'sympy.core.numbers.Rational'>

0.3333333333333333 ; 1/3

0.33333333333333326 ; 1/3

0.33333333333333304 ; 1/3

0.33333333333333215 ; 1/3

0.3333333333333286 ; 1/3

0.3333333333333144 ; 1/3

0.33333333333325754 ; 1/3

0.33333333333303017 ; 1/3

0.3333333333321207 ; 1/3

0.3333333333284827 ; 1/3

0.3333333333139308 ; 1/3

0.3333333332557231 ; 1/3

0.3333333330228925 ; 1/3

0.3333333320915699 ; 1/3

0.3333333283662796 ; 1/3

0.3333333134651184 ; 1/3

0.33333325386047363 ; 1/3

0.33333301544189453 ; 1/3

0.3333320617675781 ; 1/3

0.3333282470703125 ; 1/3

0.33331298828125 ; 1/3

0.333251953125 ; 1/3

0.3330078125 ; 1/3

0.33203125 ; 1/3

0.328125 ; 1/3

0.3125 ; 1/3

0.25 ; 1/3

0.0 ; 1/3

-1.0 ; 1/3

-5.0 ; 1/3

-21.0 ; 1/3


--
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 sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

Richard Fateman

unread,
Oct 2, 2014, 11:43:16 PM10/2/14
to sy...@googlegroups.com


On Thursday, October 2, 2014 11:11:14 AM UTC-7, Christophe Bal wrote:
And what about the following code ?

The user of Sympy must know that types are different and so that the variable are not the same things. A float is not a rational.

A float "type"  is a different "type"   from some other numbers, but  in fact every binary float
represents a particular rational number,  of the form  integer X 2^integer.    Just happens that
1/3 as a rational number, is not a one of those binary floats.




Christophe Bal

unread,
Oct 3, 2014, 2:23:27 PM10/3/14
to sympy-list
Yes but the set of floats is not even a sub ring of the set of rational numbers.

--
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 sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

Sergey Kirpichev

unread,
Oct 3, 2014, 3:35:48 PM10/3/14
to sy...@googlegroups.com


On Thursday, October 2, 2014 5:02:10 PM UTC+4, Christophe Bal wrote:
For me the fact that  1/3 == S(1)/3  has value  True  sounds like a bug because  1/3  is a float, and  S(1)/3  a rational.

Looks as a bug, please report one on https://github.com/sympy/sympy/issues (through please search first for similar issues).

Christophe Bal

unread,
Oct 3, 2014, 3:42:23 PM10/3/14
to sympy-list
Just done.

--
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 sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

Richard Fateman

unread,
Oct 4, 2014, 11:29:49 AM10/4/14
to sy...@googlegroups.com


On Friday, October 3, 2014 11:23:27 AM UTC-7, Christophe Bal wrote:
Yes but the set of floats is not even a sub ring of the set of rational numbers.
Nevertheless, floating point computations are widely used.
I suspect that most users do not know what a a sub ring is.


If you wish to incorporate floats into a symbolic computation, you can do so
by talking about them as a subset of the rationals.  Arbitrary precision floats
allow you to approximate arbitrarily closely any rational by a nearby "binary rational".

You can then rework most (maybe all) of the (usual) numerical routines by  adding
a tolerance parameter.   e.g.  instead of  cos(x)  for x a float,  you have
cos(x,err)   for x a binary-rational-arbitrary-precision-"float"   and  err -- a similar
quantity that says how large an error is acceptable in the cosine computation.

Is there a finitely representable computational structure in sympy that is closed under  cosine()?
Reply all
Reply to author
Forward
0 new messages