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:



Sunday, March 26, 2017

Project Proposal

So, my proposal is this:

I intend to create a program which will ask for an input string, a current base, and a targeted base (and a-z vs 0-p for base 26) anywhere from base 1.0000001 to base 62.999999, using 0-9, then a-z, and then A-Z.

Essentially, it will convert any base to any other, be it large, small, integer, non-integer, etc.  It will also be able to convert non-integer numbers, and have a moderately large number of digits following the decimal point if needed.

This program will be properly sanitized, with the standard returns of errors if the inputs do not fit demand.

Sunday, March 19, 2017

Ruby Vs. Java: Why is Ruby Used So Much In Industry?

Ruby is everywhere in industry, as anyone who went to the Healthcare IT event can tell you.  But why?  It's not a specialized language like PHP or SQL, it's not a derivative of C, and it doesn't have as varied a library as Python.

The answer lies in its versatility, simplicity, and numerous frameworks/enhancements, like the famous Ruby on Rails, RubyForge, RAA, RubyGems, and libraries for website and GUI applications.  As such, Ruby is extremely useful for basic scripting and clear, intuitive design.  An example of such a benefit is evident from this hypothetical scenario.

Suppose that you had to write a program in Java which took an array of literally anything (ints, strings, other lists, nulls, objects, kitchen sinks, black market organs, wookiees, etc-- all jumbled together) and returned it without all the nulls and duplicates.  You could use sets or the like, but that'd be very complex.  You could use an augmented for loop to add values to a new list if they're not nulls and not contained in the list, but that'd be very long.  There are no solutions that are both simple and short.

So what can a more general language such as Ruby do?  Well, this:



Oh snap.

Tuesday, February 28, 2017

Fantastic Arrays and How to Sort Them

Arrays in Ruby are similar to Lists in Python: highly mutable, can contain literally anything, and can do just about anything you might want.  Creating them is quite simple, and can be done in many ways, as this code shows:



Which puts this:



Which makes perfect sense.

Manipulating lists is also quite intuitive, and can be accessed in a myriad of ways.  Here are examples of most common methods:





which puts a very, very long response (with apologies for egregious repetition so it's legible):











And now it's time for a very special little method: uniq.  You saw it earlier, but that's its non-destructive form.  When it's made destructive with an !, then it has a very interesting effect:



recall that monster of an array?  Well, it's now:


Quite nice, isn't it?

So, now, bubble sort time!



This is the code, and this:



is what it does.  With this array:



it produces this result:



And so it goes.

Anyways, that's arrays finished, and a lovely bubble sort to round it all out.

See you on the next post!

Friday, February 17, 2017

Methods, Classes, and Variables

Ruby is an object-oriented language, and thus has very simple class and method support.  In addition, it has a wide range of variables which have different properties.  Here's an example of a rather simple class:



This particular class has instance methods (methods which pertain to one instance of a class), a class method (akin to a static method in Java), an instance variable (which pertains to one instance of a class), and a class variable (akin to a static variable in Java).

In addition, it references two other variables, one global and one local.  Global variables can be accessed anywhere, while local ones are tied to their specific area.  Here, they act in similar fashions, but they are fundamentally different.

When you take this class and add some code like this:



Then you can see that it will print a large amount of statements.  First, it instantiates a RandomClass with a value of one, prints out the variable, prints out a toString type thing, sets the variable to something different, and then prints out that toString substitutes again.

Then, it prints the local variable, sets the local to the return of the static method, and then prints it again, before it creates a temporary instance with value 20, and then prints it.

After this, it sets the global variable to 18, prints out the value of the static method, creates a new class using the global as the value, and then prints the toString.

Finally, it prints the class variable.  The result is this:



This is exactly what you would expect, with the only unique, Ruby-esque twists being the symbols before the variable names to designate their purpose and the "::" instead of the "." most other languages use to call a static (or any kind) of variable.

The three people whose blogs I commented on are:

Julian Kastelic, whose blog is https://perlnecklace.wordpress.com/,
Joseph Pernick, whose blog is http://cs270joepernick.blogspot.com/,
and Caroline Marcus, whose blog is https://swift359.wordpress.com/.