Ask Your Question
1

how to get output in a mixed fraction?

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

umren gravatar image

regular rational sage style is like

sage:22/10

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

sage:2+2/10

is this possible?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
4

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

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())
    else:
        return '0'
edit flag offensive delete link more

Comments

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 +0100 )edit
2

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

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

Comments

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 +0100 )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

Stats

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

Seen: 1,598 times

Last updated: Aug 21 '10