Jump to content
  • Advertisement
Sign in to follow this  
Endar

converting numbers with ML

This topic is 4395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently working on an assignment in SML, or Standard ML, specifically, the version "from New Jersey." Don't quite know what that means myself, but I assume that it was developed at a uni somewhere in New Jersey. Anyway, because this is an assignment, I'm obviously not looking for code, and since the work is in ML, it's more about the implementation in ML and learning the functional programming paradigm, not working out how to do every little thing ourselves. After that, let me tell you what I'm asking for: I currently need to figure out if there is a way to convert a number from one base to another, digit by digit. If you think you can help, then it would be appreciated. If you can't because it's an assignment, that's okay, I won't hold it against you. [smile]

Share this post


Link to post
Share on other sites
Advertisement
A number in base x can be represented using the formula:
d0*x^0 + d1*x^1 + ... + dn*x^n

Where the dx's are the digits (from least significant to most) and ^ is to the power of. This can be exploited to convert a string of a variable base to a number. To convert a number of base x to a string, simply remember that every digit can only go up to x, and that each successive digit increases in relation to x as you go up in significance.

Share this post


Link to post
Share on other sites
I think what the previous poster was getting at is: just divide by the base and the remainder is the next digit: ie base 10 to base 4:

100/4 = 25 r 0
25/4 = 6 r 1
6/4 = 1 r 2
1/4 = 0 r 1

so 1210 base 4 is 100 base 10. I'm almost postive sml has an atoi() function. Floating point will be a bit more involved, but there you just multiply the fractional part by the new base.

Share this post


Link to post
Share on other sites
Thanks guys, but the thing is (which I probably should have mentioned in more detail) the reason I wanted it digit by digit is because the assignment is to write a bunch of functions for infinite integers that can use many different bases internally for computation. I'm using an integer list (since I'm an absolute beginner - been using ML for less than a week) to hold the digit for each place in the number (eg. 215 in base 10 would be [2,1,5]). The internal base for computation could be anywhere from 2 to 215-1.

So it's quite possible that an integer would not be able to hold the whole value.

Edit:: so, when loading a value, we need to convert it from whatever base it is provided in, into the internal base.

[Edited by - Endar on July 29, 2006 6:06:46 PM]

Share this post


Link to post
Share on other sites
I see. So what you really want are routines to do addition, multiplication, etc. given a number list of any base, correct? In that case, just think about how you do those things by hand and it should be fairly easy.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!