Ask Your Question
1

How to output 'decimal' numbers in different radix.

asked 2012-12-29 00:09:31 +0100

mlv gravatar image

updated 2012-12-29 01:04:28 +0100

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.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2012-12-29 01:03:46 +0100

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'
edit flag offensive delete link more

Comments

Thanks for the solution!

mlv gravatar imagemlv ( 2012-12-29 01:23:16 +0100 )edit
0

answered 2020-08-03 09:37:29 +0100

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))
edit flag offensive delete link more

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: 2012-12-29 00:09:31 +0100

Seen: 1,207 times

Last updated: Dec 29 '12