|
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
|