Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: BlakPilar on September 12, 2011, 07:42:37 pm

Title: Convert any base algorithm help
Post by: BlakPilar 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? ???
Title: Re: Convert any base algorithm help
Post by: Yeong 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 (http://ourl.ca/9972)
Title: Re: Convert any base algorithm help
Post by: Builderboy 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
Title: Re: Convert any base algorithm help
Post by: BlakPilar on September 12, 2011, 08:00:27 pm
Ah-hah! Thanks Builderboy!
Title: Re: Convert any base algorithm help
Post by: Builderboy 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
Title: Re: Convert any base algorithm help
Post by: BlakPilar 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
Title: Re: Convert any base algorithm help
Post by: meishe91 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.
Title: Re: Convert any base algorithm help
Post by: BlakPilar 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).
Title: Re: Convert any base algorithm help
Post by: Builderboy 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.
Title: Re: Convert any base algorithm help
Post by: meishe91 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?
Title: Re: Convert any base algorithm help
Post by: Builderboy 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
Title: Re: Convert any base algorithm help
Post by: meishe91 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
Title: Re: Convert any base algorithm help
Post by: BlakPilar on September 12, 2011, 08:48:13 pm
@BlakPilar
Listen to Builder :P

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