Ticket #3938 (closed defect: fixed)

Opened 2 years ago

Last modified 19 months ago

[with patch, with positive review] coercion framework converts built-in types to Sage types when it should not

Reported by: cwitty Owned by: robertwb
Priority: major Milestone: sage-3.3
Component: coercion Keywords:
Cc: cwitty Author(s):
Report Upstream: Reviewer(s):
Merged in: Work issues:

Description (last modified by mabshoff) (diff)

This came up while reviewing #2898, which adds a conversion from float to ZZ (for integral values). After applying that patch, you get:

sage: 1.0r/8
1/8

That's because of this code in coerce.pyx, which does a conversion rather than a coercion:

        elif PY_IS_NUMERIC(x):
            try:
                x = yp(x)
                if PY_TYPE_CHECK(yp, type): return x,y

I tried to fix this, but every time I fixed something it broke something else. I'm going to attach my latest non-working patch, which may or may not be a useful place to start.

[Once this ticket has been reviewed and merged #2898 should be closed]

Attachments

trac3938-coercion-converts-native.patch Download (4.6 KB) - added by cwitty 2 years ago.
3938-type-coercion-2.patch Download (13.7 KB) - added by robertwb 2 years ago.
3938-type-coercion-3.patch Download (7.3 KB) - added by robertwb 2 years ago.
3938.patch Download (21.4 KB) - added by roed 20 months ago.
Merged the three patches, added a few fixes to precision.
3938-type-coercion-final.patch Download (20.1 KB) - added by robertwb 20 months ago.
apply only this patch

Change History

Changed 2 years ago by cwitty

Changed 2 years ago by robertwb

I've been playing around with this a bit, simplified your patch some, but one consequence is that

sage: parent(RealField(100)(1.5) + float(1.5)) # good?
<type 'float'>
sage: RealField(100)(2^4000) == float('inf')   # bad?
True

Thoughts?

Changed 2 years ago by cwitty

Both of these changes make float act more like RDF. I've sometimes wished that comparison operators would coerce to more-precise types instead of less-precise, but that would be a huge change across big parts of Sage; unless such a policy change is made, I think that both consequences are actually good.

Changed 2 years ago by robertwb

Changed 2 years ago by robertwb

Changed 2 years ago by robertwb

  • summary changed from coercion framework converts built-in types to Sage types when it should not to [with patch, needs review] coercion framework converts built-in types to Sage types when it should not

I feel your pain...what a nasty patch to try and write! Well, I finally feel like I've got a correct, working solution. Apply all three patches.

Changed 2 years ago by mabshoff

  • cc cwitty added
  • description modified (diff)

Carl,

any chance you could review those three patches?

Cheers,

Michael

Changed 2 years ago by anakha

I get

sage: parent(RealField(10)(1) * float(1))
Real Field with 10 bits of precision

with the patches applied against 3.1.2.alpha4.

Changed 2 years ago by robertwb

Is this not the desired behavior?

Changed 2 years ago by anakha

  • summary changed from [with patch, needs review] coercion framework converts built-in types to Sage types when it should not to [with patch, positive review] coercion framework converts built-in types to Sage types when it should not

I thought the goal was to convert to the type with most precision. It seems I was mistaken and this is trying to convert to the type with less precision.

In that case it works and passes a good chunk of the test suite. (I can't test the rest because of the interfaces/lisp.py failure)

It also passes its own tests and the code looks good.

Changed 2 years ago by mabshoff

This patch causes a Heisenbug:

mabshoff@sage:/scratch/mabshoff/release-cycle/sage-3.1.3.alpha0$ ./sage -t -long devel/sage/sage/modular/modsym/ambient.py
sage -t -long devel/sage/sage/modular/modsym/ambient.py     

------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occured in SAGE.
This probably occured because a *compiled* component
of SAGE has a bug in it (typically accessing invalid memory)
or is not properly wrapped with _sig_on, _sig_off.
You might want to run SAGE under gdb with 'sage -gdb' to debug this.
SAGE will now terminate (sorry).
------------------------------------------------------------


A mysterious error (perphaps a memory error?) occurred, which may have crashed doctest.
         [4.6 s]
exit code: 768

It does not happen without "-long" and when running "-long -verbose" it also seems to pass. I guess it is time to valgrind :)

Cheers,

Michael

Changed 2 years ago by mabshoff

  • summary changed from [with patch, positive review] coercion framework converts built-in types to Sage types when it should not to [with patch, needs work] coercion framework converts built-in types to Sage types when it should not

Changed 20 months ago by roed

I didn't find the Heisenbug that Michael mentioned. I rebased the patches against 3.2.3.

Changed 20 months ago by roed

  • summary changed from [with patch, needs work] coercion framework converts built-in types to Sage types when it should not to [with patch, needs review] coercion framework converts built-in types to Sage types when it should not

Changed 20 months ago by robertwb

Thanks for rebasing this. Since you're not the one who originally wrote it, do you want to give it a review?

Changed 20 months ago by roed

Merged the three patches, added a few fixes to precision.

Changed 20 months ago by craigcitro

  • summary changed from [with patch, needs review] coercion framework converts built-in types to Sage types when it should not to [with patch, with positive review pending minor fixes] coercion framework converts built-in types to Sage types when it should not

Patch looks good. Thankfully, David folded everything into one patch.

I have two minor issues, and after these are fixed, I'm happy to give this a positive review.

  • There are two long blocks (an EXAMPLES and a TESTS) that are not indented correctly.
  • There are three functions that are moved and one that is new which need doctests. (The moved functions don't necessarily have to have them added, but since it's three functions, it seems worth just adding doctests.)

Once these are done, I'm happy to give this a positive review.

Changed 20 months ago by robertwb

OK, I'll go ahead and add those doctests.

Changed 20 months ago by robertwb

apply only this patch

Changed 20 months ago by robertwb

OK, I added the doctests and fixed the indentation.

Changed 20 months ago by craigcitro

  • summary changed from [with patch, with positive review pending minor fixes] coercion framework converts built-in types to Sage types when it should not to [with patch, with positive review] coercion framework converts built-in types to Sage types when it should not

Looks good.

Changed 19 months ago by mabshoff

One fix:

    TypeError: no cannonical coercion from Real Field with 53 bits of precision to Real Field with 100 bits of precision

needs to become

    TypeError: no canonical coercion from Real Field with 53 bits of precision to Real Field with 100 bits of precision

I am fixing that in the patch I am applying.

Cheers,

Michael

Changed 19 months ago by mabshoff

  • status changed from new to closed
  • resolution set to fixed
  • milestone changed from sage-3.4.1 to sage-3.3

Merged 3938-type-coercion-final.patch with spelling fix in Sage 3.3.alpha3.

Cheers,

Michael

Note: See TracTickets for help on using tickets.