I think consistency must be maintained in this case.
Rational(5.2) must be equal to Rational( '5.2' )
I made a patch for it. I've attached it to this mail.
Output after code changes:
In [1]: from sympy import *
In [2]: Rational('0.2')
Out[2]: 1/5
In [3]: Rational(0.2)
Out[3]: 1/5
I ran the tests. Got this error message:
_________________________________________________________________ sympy.core
.numbers.Rational_________________________________________________________________
File "sympy/core/numbers.py", line 970, in sympy.core.numbers.Rational
Failed example:
Rational(.2)
Expected:
3602879701896397/18014398509481984
Got:
1/5
============================================= tests finished: 2643 passed,
1failed
, 3 skipped, in 178.48 seconds =============================================
*This error is expected. Every other test is passed. *
Since, mathematically, 0.2 != 3602879701896397/18014398509481984
Please give me your feedback on this.
On Tuesday, February 4, 2014 1:26:56 PM UTC+5:30, F. B. wrote:
>
>
>
> On Tuesday, February 4, 2014 8:38:57 AM UTC+1, Rajath Shashidhara wrote:
>>
>>
>>
>> On Monday, February 3, 2014 11:05:50 PM UTC+5:30, mario wrote:
>>>
>>> 5.2 is a Python floating point number (a double) represented as
>>> 5854679515581645*2**(-50);
>>> using Rational( '5.2' ) it is recognized as exactly 52/10
>>>
>>>
>> Isn't it better to have Rational(5.2) also store 26/5 , just like
>> Rational( '5.2' ) does ?
>> Is there a reason why it is left the way it is?
>>
>>
> It's Python that stores floating points that way. Moreover, the handling
> of the internal representation of 5.2 is handled by the CPU. It's the
> correct behavior as Python was not meant to be a CAS, just an ordinary
> programming language.
>
> I can make a patch for this, if more people feel this way.
>>
>
> You can? How? The only possibility I see is by modifying IPython to
> capture 5.2 before it gets evaluated by Python. Anyways, I don't see any
> reason for it, just use *Rational(26, 5)*
>
--
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.
For more options, visit https://groups.google.com/groups/opt_out.
>From de4a95a0fa23d2bf2de2a4968a8bb9ef6a9aeafd Mon Sep 17 00:00:00 2001
From: rajaths589 <[email protected]>
Date: Tue, 4 Feb 2014 18:29:19 +0530
Subject: [PATCH] Fix Rational-Float Issue by typecasting float to str
---
sympy/core/numbers.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/sympy/core/numbers.py b/sympy/core/numbers.py
index 8dc8e37..69d0bda 100644
--- a/sympy/core/numbers.py
+++ b/sympy/core/numbers.py
@@ -1077,7 +1077,9 @@ def __new__(cls, p, q=None):
pass # error will raise below
if isinstance(p, (float, Float)):
- return Rational(*_as_integer_ratio(p))
+ #Error Corrected by typecasting float to str
+ return Rational(str(p))
+ #return Rational(*_as_integer_ratio(p))
if not isinstance(p, SYMPY_INTS + (Rational,)):
raise TypeError('invalid input: %s' % p)
--
1.8.1.2