Ruby/Ruby Arithmetic

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
** Writing Programs in Ruby
** Ruby and the Text Editors
** Escape Character Sequence
** Ruby Arithmetic
** Comments in Ruby
** Ruby Documentation
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application
Ruby Arithmetic/Mathematics

Ruby supports both the types of numbers, integers and floating point numbers.

The Integers are the instances of Class Fixnum and Bignum.Floating point numbers are the instances of class Float. Ruby also performs all the arithmetic operations as any other programming language does. It will give integer output if two integers are added and will give floating point numbers when a floating point number is divided by an integer value.

As we had discussed earlier in the section methods that most of the operations in Ruby are intact in methods and the various arithmetic operators are also methods in reality but give us the feel of having them act as operators only. Here we will have a look on NArray, BigFloat and Polynomial.

NArray as the term illustrates is a package that functions to handle the arrays. It takes care of the arrays of large size. The Ruby Libraries of Ruby like Vector nad mtarxi are very strong and efficient in performance as they are the extensions of C. It also takes care of the basic statistical operations also.

BigFloat takes care of floating point numbers which are very large in number. It is developed by Shigeo Kobayashi. BigFloat needs the representation as 0.0 if not given like this it accepts the number in this format by default.

Polynomial, which was founded and developed by K. Kodama, takes the responsibility of managing the prime factorizations and polynomials. In this both monomials and multi-variate polynomials are supported by Ruby. Image:Number1.jpg

[edit] How to use Ruby as a Basic Calculator

If we have Ruby installed on Windows platform, we will get a command line as shown below to work on Ruby Calculator:

C:\>irb --simple-prompt
>> 

Now we are all set to work on our Ruby Calculator and get the expected result of our computations.

>>1+5
=> 6
>>6/2
=>3
>>3/2
=>1

Here we get 1 as the output, the reason is very simple, here the numbers are considered as Integers and not as Float.

Let's have a basic understanding of what are Integers and what are Floating Point Numbers.

1. Integers Integers:Integers are the whole numbers in mathematics,for example 3, -8,6,9,11. and whenever we do any computation on the Integers we always get the result also as Integers or whole numbers like

when we divided 3 by 2 we got the output as 1 as it was treated as integer in this case, although 3 divided by 2 should ideally give is 1.5, but since it was treated as Integer we got 1 as the result.


>> 3/2
=>1 //Treated as Integer

2. Floats Floats:Float as in any other programming language represents the decimal numbers for example 1.5,34.98,1000.89 etc. Whenever we do any computation on floats in Ruby we always get the output as Float numbers only.

For example:

>>3/2
=>1.5 

Here since it was treated as float we got the output as 1.5 instead of 1 as in case of Integers.

2. Basic Symbols used in Ruby Arithmetic

+  Denotes addition  
-  Denotes subtraction  
*  Denotes multiplication 
/  Denotes division 
** Denotes exponent  
%  Denotes remainder 

Examples for each are mentioned below in the section:

1. +(Addition)

>>3+4
=>7

2. -(Subtraction)

>>33-11

=>22

3. *(multiplication)

>>3*4
=>12

4. /(division)

>>6/2
=>3

5. **(exponent)

>>3**3
=>27

6. %(remainder)

>>15%4
=>3

[edit] Conversion of Classes in Ruby Arithmetic

In Ruby there are several classes for different methods in Arithmetic too.For example the basic difference we saw in out section above was between Integers and Floats.

Here Division (/) and Addition (+) behave separately with the Floats and the Integers and so we need to traet them differently. due to this we need to have few methods which can interchange then from one to another as per the user's requirement.

We will have a small list of few of these methods below:

String#to_i :  This method converts String to Integer. 
String#to_f :  This method converts String to Float.  
Float#to_i  :  This method converts Float  to Integer. 
Float#to_s  :  This method converts Float  to String.
Integer#to_f:  This method converts Integer to Float . 
Integer#to_s:  This method converts Integer to String . 


Next
Personal tools