Other Math/programming question: change of base

Discussion in 'Software' started by Irvine, 30 May 2010.

  1. Irvine

    Irvine What's a Dremel?

    Joined:
    29 Sep 2004
    Posts:
    169
    Likes Received:
    0
    Hey guys. It's been a while since I've been on these forums, but from experience I know you all are bunch of geeks :D

    So here's my question: is there a way to change the base of an exponent WITHOUT using logs, or for that matter, any non-integer number? I'm writing a program in MIPS and I need to convert a 10^x number to a 2^y number without using any of the float operations. Plus it's a fun mind game.

    Thanks!
     
  2. capnPedro

    capnPedro Hacker. Maker. Engineer.

    Joined:
    11 Apr 2007
    Posts:
    4,381
    Likes Received:
    241
    You'll have problems if you can't use any non-integers. For example, when x=2, y=6.646.

    Or do you just mean you don't want to divide by 0.301 instead of log2?
     
  3. bulldogjeff

    bulldogjeff The modding head is firmly back on.

    Joined:
    2 Mar 2010
    Posts:
    8,403
    Likes Received:
    634
    Well I've just read the last 2 post and it's all about as clear as mud....Guess I'm not a geek then!
     
  4. Danny

    Danny What's a Dremel?

    Joined:
    4 May 2010
    Posts:
    114
    Likes Received:
    13
    Second that, bro'
     
  5. Bufo802

    Bufo802 Minimodder

    Joined:
    28 Jul 2008
    Posts:
    335
    Likes Received:
    8
    10 = 2^(1/log2) (log is base 10)

    so 10^x = (2^(1/log2))^x

    = 2^(x/log2)

    therefore in your case y=x/log2 where log2 = 0.30102999....

    Don't do programming so not sure if that's usable but it works from a maths point of view.

    Edit: sorry didn't see the non-integer bit, can't see any way of doing it otherwise atm
     
  6. sb1991

    sb1991 What's a Dremel?

    Joined:
    31 May 2010
    Posts:
    425
    Likes Received:
    31
    I'm not really familiar with the language you're using, but bear in mind that however you display it, the number is actually stored in binary, so given that you want a number as a base 2 exponential it makes sense to start from the binary (or maybe hex) value and convert this. While I think you can avoid logs by using binary, at some point you will have to deal with fractions (if only for displaying the final number), so I don't think you can avoid floats entirely. Is x an integer?
     
  7. Irvine

    Irvine What's a Dremel?

    Joined:
    29 Sep 2004
    Posts:
    169
    Likes Received:
    0
    Thanks for your responses.

    Okay, so for clarity, here's the "entire" problem. ALL of the following variables are signed integers in binary.
    We're given three things to start; I'll call them by their register names. $a0 is the sign bit (that's the easy part, haha), $a1 is the mantissa, and $a2 is the exponent, so that the number is a non-normalized number in scientific notation like so:
    (-1)^($a0) * $a1 * 10^($a2)
    (These numbers are in base-10, for simplicity's sake)

    The final product should be a number that looks like this:
    (-1)^($v0) * 1.($v1) * 2^($v2)
    (These numbers are in base-2)
    where $v1 is essentially concatenated to the "1.", so it represents a fraction of sorts.
    (Note: I know there is no $v2 register, but I'm just using it as an example for clarity)

    To me, this problem boils down to a base conversion issue, but maybe I'm just approaching it at the wrong angle? Any ideas?

    Edit:
    GRRRR, professor just emailed the class and said that the input exponent is only in the range of [-8,8]. That just made the problem super-easy.
    Thanks for the help anyway, guys!
     
    Last edited: 3 Jun 2010
  8. javaman

    javaman May irritate Eyes

    Joined:
    10 May 2009
    Posts:
    3,991
    Likes Received:
    192
    I was looking this up today myself o_O
    What I got was log2 20 = (log10 20)/(log10 2)
     

Share This Page