First time here? Check out the FAQ!

Ask Your Question
1

How to output 'decimal' numbers in different radix.

asked 12 years ago

mlv gravatar image

updated 12 years ago

DSM gravatar image

For example, how to output the fraction 13/7 as a 'decimal' number in the base 6? Perhaps to 4 'digits' after the 'decimal' point if the representation is not finite.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 12 years ago

DSM gravatar image

You can use the str method:

sage: (13/7)
13/7
sage: (13/7).str(base=6)
'21/11'
sage: (13/7).n()
1.85714285714286
sage: (13/7).n().str(base=6)
'1.505050505050505050511'
Preview: (hide)
link

Comments

Thanks for the solution!

mlv gravatar imagemlv ( 12 years ago )
0

answered 4 years ago

The following function converts integers in any base to any other base:

def base_conversion(number, base_from, base_to):
    if(base_from==10):
        return (number.str(base=base_to))
    else:
        decimal = Integer(str(number), base=base_from)
        return (decimal.str(base=base_to))
Preview: (hide)
link

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

Seen: 1,279 times

Last updated: Dec 29 '12