First time here? Check out the FAQ!

Ask Your Question
1

how to get output in a mixed fraction?

asked 14 years ago

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?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
4

answered 14 years ago

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'
Preview: (hide)
link

Comments

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

Jason Grout gravatar imageJason Grout ( 14 years ago )
2

answered 14 years ago

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)

Preview: (hide)
link

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 ( 14 years ago )

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: 14 years ago

Seen: 1,669 times

Last updated: Aug 21 '10