Author Topic: Convert any base algorithm help  (Read 5881 times)

0 Members and 1 Guest are viewing this topic.

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Convert any base algorithm help
« on: September 12, 2011, 07:42:37 pm »
Ok, first off, PLEASE don't give me another routine (I am aware that Weregoose (I believe) has a super-optimized base converter algorithm), I just want to see if the one I'm trying will work.

Now, in theory (or at least through my way of thinking), this routine should work, but I get an error for anything other than hexadecimal. Here's all of the code for it:
Code: [Select]
"0123456789ABCDEFG→Str2
length(Str2→L

Input "DEC: ",D
"_→Str1

If 0>D or not(D
"0→Str1

While D>0
sub(Str2,L(fPart(D/L))+1,1)+Str1→Str1
iPart(D/L→D
End

Disp Str1
However, I keep getting errors during the first run-through on the sub command. I've manually checked all the variables' values (using 50 as my test value) and the very first letter should be G, but I keep getting a domain error with the cursor scrolling to the closing parenthesis of the sub command.

Any ideas? ???
« Last Edit: September 12, 2011, 07:45:11 pm by BlakPilar »

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Convert any base algorithm help
« Reply #1 on: September 12, 2011, 07:47:08 pm »
It looks like the algorithm that I used last time.
You can use my program as a reference.
you can get it at here:
http://ourl.ca/9972
Sig wipe!

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Convert any base algorithm help
« Reply #2 on: September 12, 2011, 07:55:15 pm »
try substituting L(fPart(D/L))+1 with round(L(fPart(D/L))+1,0), sometimes the floating point values can look like whole numbers, but are now quite because of precision errors, and this makes commands like Sub() complain.  Rounding it to the nearest whole number should fix the problem

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Convert any base algorithm help
« Reply #3 on: September 12, 2011, 08:00:27 pm »
Ah-hah! Thanks Builderboy!

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Convert any base algorithm help
« Reply #4 on: September 12, 2011, 08:08:11 pm »
No problem :D I swear the first time this happened to me I thought TiBasic was broken x.x

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Convert any base algorithm help
« Reply #5 on: September 12, 2011, 08:11:17 pm »
Yeah, that was me in class, working on this. I constantly went over it and over it and over it and it wouldn't so I figured I'd bring it here to avoid further frustration lol

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Convert any base algorithm help
« Reply #6 on: September 12, 2011, 08:17:45 pm »
Oh sub()...you picky little meanie. Oh well, glad it got fixed. Wouldn't iPart() be a better choice though since you just want to truncate the decimal section? I know like nothing about base conversion though so I'm not sure, just a guess from making a binary converter or two.
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Convert any base algorithm help
« Reply #7 on: September 12, 2011, 08:22:47 pm »
I do lol, that's how I prevent an infinite loop. After using the sub, I cut off the decimal part of the original number divided by the length of the base-string. (Read Builderboy's response lol) All my knowledge of base conversion comes from TIBD, which I'm glad I took the time to read (that article, that is).
« Last Edit: September 12, 2011, 08:25:39 pm by BlakPilar »

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Convert any base algorithm help
« Reply #8 on: September 12, 2011, 08:24:01 pm »
You could use iPart, but sometimes the number you want is 3, and instead you get 2.9999999999999999999, which iPart truncates into 2, whereas round() gets you to 3.

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Convert any base algorithm help
« Reply #9 on: September 12, 2011, 08:29:08 pm »
Well I was talking about instead of using round(,0). Since you should be getting an integer to begin with it shouldn't make a difference which is used and using iPart() will save you a couple bytes of memory. Also, you can save a couple more bytes by taking out a couple of those parentheses.

Edit:
But shouldn't using Base(fPart(Num/Base)) give you a number above the desired number? Or is the algorithm for that stuff just that flawed?
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Convert any base algorithm help
« Reply #10 on: September 12, 2011, 08:31:18 pm »
what do you mean you should be getting an integer to begin with?  The whole problem is that the floating point arithmatic returns a non integer number

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Convert any base algorithm help
« Reply #11 on: September 12, 2011, 08:36:00 pm »
Well I meant like the math returns the integer part of the number with that line of math. But you're right. I just tested it out and ya, I forgot about cases dealing with thirds and such. My bad.

@BlakPilar
Listen to Builder :P
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Convert any base algorithm help
« Reply #12 on: September 12, 2011, 08:48:13 pm »
@BlakPilar
Listen to Builder :P

I understood what he meant when he first said it :P