Convert between numeral systems

124 views
Skip to first unread message

Robert Pollak

unread,
Jul 3, 2015, 8:48:04 AM7/3/15
to sy...@googlegroups.com
Hello,

I have got a newbie question: Does SymPy provide a way to deal with numbers in different numeral systems and e.g. to convert them between different systems? Or do I have to tell my students that they have to program this by theirselves :) ?

Best Regards,
Robert

Oscar Benjamin

unread,
Jul 3, 2015, 10:35:10 AM7/3/15
to sympy
Hi Robert,

I guess you mean converting from e.g. hex to decimal or something like
that? Or do you want to use more obscure systems like Roman numerals?

Python's int function can convert from any positional system in bases
2-36. Also Python has the bin, hex and oct functions which you may
find useful:

>>> int('ff', base=16)
255
>>> hex(255)
'0xff'
>>> bin(255)
'0b11111111'
>>> oct(255)
'0o377'
>>> str(255)
'255'

There is no general inverse for int(str, base) provided so you would
have to implement that yourself but it could be a good exercise for
your students.

I don't personally know if sympy has anything to help you here.

--
Oscar

Joachim Durchholz

unread,
Jul 3, 2015, 1:43:44 PM7/3/15
to sy...@googlegroups.com
That's representation, which usually isn't considered a part of symbolic
math.
So no, SymPy does not offer routines for that. A short internet search
showed me that Gmpy should have them, so I suggest looking there. Also,
SymPy will use Gmpy if installed AFAIK, so this should work well for
your use case.

Robert Pollak

unread,
Jul 6, 2015, 5:35:09 AM7/6/15
to sy...@googlegroups.com


On Friday, July 3, 2015 at 7:43:44 PM UTC+2, Joachim Durchholz wrote:
Am 03.07.2015 um 14:48 schrieb Robert Pollak:
> I have got a newbie question: Does SymPy provide a way to deal with numbers
> in different numeral systems and e.g. to convert them between different
> systems? Or do I have to tell my students that they have to program this by
> themselves :) ?

That's representation, which usually isn't considered a part of symbolic
math.
So no, SymPy does not offer routines for that.

I understand.
 
A short internet search
showed me that Gmpy should have them, so I suggest looking there.

Thank you for this hint, which lead me to gmpy2.digits(x, base) for one conversion direction of integers. However, it now came to my mind that our students should also be able to deal with floats, and there might be no 'packaged' solution  for that available yet. (They did this with Mathematica until now.)

Aaron Meurer

unread,
Jul 6, 2015, 2:23:45 PM7/6/15
to sy...@googlegroups.com
I think mpmath has some support, although it may be low-level. Look at
https://github.com/fredrik-johansson/mpmath/blob/master/demo/pidigits.py,
which uses mpmath to compute the digits of pi in any base.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/937afe89-6ce5-4ed1-8468-832e85afe893%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Robert Pollak

unread,
Jul 7, 2015, 4:00:20 PM7/7/15
to sy...@googlegroups.com
Am 2015-07-06 um 20:23 schrieb Aaron Meurer:
> I think mpmath has some support, although it may be low-level. Look at
> https://github.com/fredrik-johansson/mpmath/blob/master/demo/pidigits.py,
> which uses mpmath to compute the digits of pi in any base.

Thank you - that looks promising.

Chris Smith

unread,
Jul 17, 2015, 5:20:15 PM7/17/15
to sy...@googlegroups.com
Is this what you are looking for?

>>> from sympy.ntheory.factor_ import digits
>>> digits(1234,20)
[20, 3, 1, 14]
>>> help(digits)
Help on function digits in module sympy.ntheory.factor_:

digits(n, b=10)
    Return a list of the digits of n in base b. The first element in the list
    is b (or -b if n is negative).

    Examples
    ========

    >>> from sympy.ntheory.factor_ import digits
    >>> digits(35)
    [10, 3, 5]
    >>> digits(27, 2)
    [2, 1, 1, 0, 1, 1]
    >>> digits(65536, 256)
    [256, 1, 0, 0]
    >>> digits(-3958, 27)
    [-27, 5, 11, 16]

Robert Pollak

unread,
Sep 1, 2015, 12:26:48 PM9/1/15
to sympy


On Friday, July 17, 2015 at 11:20:15 PM UTC+2, Chris Smith wrote:
Is this what you are looking for?

>>> from sympy.ntheory.factor_ import digits
>>> digits(1234,20)
[20, 3, 1, 14]

Thank you! So there really was something hidden in SymPy :) Unfortunately, as this is part of ntheory, it also cannot convert floats.

Chris Smith

unread,
Jun 29, 2017, 3:11:19 PM6/29/17
to sympy, robert...@mykolab.com
Most of what you would need to convert strings to fractions in a given base is already part of Python and an example is given here

Robert Pollak

unread,
Jun 29, 2017, 5:38:04 PM6/29/17
to Chris Smith, sy...@googlegroups.com
Am 2017-06-29 um 21:11 schrieb Chris Smith:
> Most of what you would need to convert strings to fractions in a given
> base is already part of Python and an example is given here
> <https://stackoverflow.com/questions/44616903/using-other-base-systems-in-sympy/44833093#44833093>

Thank you, but I am not interested in fractions. I need floating point
representation in different bases.

Since I was already at stackoverflow, I poked around a little, and I
finally found something!:
"How to convert floating point number to base 3 in python",
https://stackoverflow.com/a/5111205/1389680

--Robert
Reply all
Reply to author
Forward
0 new messages