Sunday, April 23, 2017

The End of an Era

This week in Ruby, many old and respected methods "retired" from use.  They will always be remembered.

It turns out that trying to convert numbers from bases to bases without going through base 10, which already proved itself troublesome with decimal inputs, is nearly impossible with decimal bases.  As such, although the program works, it is without the dramatic flair which it would otherwise have had.

Anyways, let us begin.



Here, you can see two translation lists, an accuracy, and a rather unusual input.  


A moment of silence for the fallen.  Their functionality has been replaced, their usefulness is no longer extant, and now their watch has ended.




This method takes any base 10 number and converts it to any other base.  Note the $accuracy, which gives the number of decimal places it is accurate to.  The higher the accuracy, the higher the...well...accuracy.  However, there is the danger that you'll get an infinity error if the number gets too small, so some care must be had.


This method is mostly unchanged from the previous version and entirely unchanged in effect.


This method is exactly like convertDecimalsTo10, but with the order reversed (so as to increment going to the right), the exponents multiplied (because they're treated as positive), the for loop going from zero instead of one, and the printed statement saying it's dealing with ints.



This is a safety net, which makes sure that if a decimal point is in the inputed alphabet, the inputed number is treated as an integer.  The outputted one, however, can be a non-int.



And so the most powerful base calculator ever made ends, with very few changes for precision.



The main problem this week, however, was not the methods.  The main problem was the fact that, for example, (.25 - .2 ) returned .04999999999.  This, remarkably, isn't good.  

Luckily, after talking with Dr. Manilich, the professor who will teach Ruby on Rails next semester, I found a solution in the way of these lines:


BigDecimal is a Ruby gem, designed to convert numbers to "big decimals" (to_d) and so to be able to calculate with extreme precision.  I downloaded it, required it, and so solved the problem and finished the program.

Wednesday, April 12, 2017

Base Calculator Mach Two

The changes here are small but meaningful.  Essentially, I added the ability to input and output non-integer numbers and more digit lists for fun and profit.

I added two methods, convertDecimalsFrom10:


And convertDecimalsTo10:

Both are fairly straightforward, and the only complex bit is the first for loop in convertDecimalsFrom10 -- which just tests smaller and smaller exponents until either the remaining float is less than .0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, which is a pretty small number.  As such, there is quite a bit of accuracy.


These are the current four digit lists.  The first is the standard one, which supports 2-36, the second is the extended one, which supports 2-63 (note the space), the third is the letter scale for base 26 shenanigans, and the fourth is ASCII and supports 2-94.  If you're using the ASCII one, it is recommended to not have the number contain a decimal point as you could end up with more than one.


The major issue this time was trying to avoid converting it to 10 and back.  I succeeded in doing that for integers, but unfortunately, due to the sometimes interminable nature of decimals, I could not find any bug-free way to do that here.  

Anyways, if I can solve the puzzle of changing the second float while translating the first, I'll be a happy man indeed.

Saturday, April 1, 2017

Base Calculator Mach One

At the moment, here are the capabilities of my code:

It can transform any base from 2-36, inclusive, into every other.

It can take any size input that will not crash your computer.

It can work even if the number input involves digits not seen in the input base (for example, HELLO can be an input for base 10)

And it will print out each individual addition which takes place, for those who care about such things.

Here's my code, which is commented such you can see what's going on at any given point:







It doesn't take inputs yet, but the output looks like this: