Ask Your Question

how to get output in a mixed fraction?

asked 2010-08-21 14:27:07 +0200

umren gravatar image

regular rational sage style is like


i want make it look like (thru pprint it will just look like a mixed on papper)


is this possible?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2010-08-21 15:13:36 +0200

Mike Hansen gravatar image

If you want all rational numbers to look like this, then there isn't a convenient way to make that happen in Sage. I'm currently working on a framework to allow one to do this, but it's not ready yet. I will update this post when there is a ticket in question

However, if you just want a function that will return a string in the right form, you can use something like the following

def pprint(rational):
    if rational < 0:
        return '%s - %s'%(rational.ceil(), rational.ceil() - rational)
    elif rational > 0:
        return '%s + %s'%(rational.floor(), rational-rational.floor())
        return '0'
edit flag offensive delete link more


Can you say a bit more about this framework? Is it a printing framework?

Jason Grout gravatar imageJason Grout ( 2010-08-24 13:08:44 +0200 )edit

answered 2010-08-21 17:05:32 +0200

umren gravatar image

yeah would be great to deal this without that much complex constructions..

got an easier solution btw

sage: a=22/10 sage: floor(a), a - floor(a) (2, 1/5)

edit flag offensive delete link more


Note that if a is something like -22/10, that will give (-3, -4/5) which is probably not the desired result.

Mike Hansen gravatar imageMike Hansen ( 2010-08-21 19:44:36 +0200 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2010-08-21 14:27:07 +0200

Seen: 1,515 times

Last updated: Aug 21 '10