Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - NUMBERS


As we have seen in the Data Types topic, there are five types of Numbers supported by Ruby.


Let us see them below :

  1. Fixnum Data Type



    The Fixnum Data Type supports, a whole number(that can be positive or negative).

    Example :



    x = 5
    y = -3
    puts x.class
    puts y.class
    


    Output :



      Fixnum
      Fixnum


    So, if you see the above code, the variable x is holding a positive number (i.e. 5),

    x = 5


    And the variable y is holding a negative number (i.e. -3)

    y = -3


    Now, if you see the Data Type of all the variables,

    puts x.class
    puts y.class


    They are of Fixnum type.

    Fixnum

  2. Bignum Data Type



    The Data Type for very big numbers is Bignum.

    Example :



    x = 98765432109876543210
    puts x.class
    


    Output :



      Bignum


    So, if you see the above code, the variable x is holding a very large number (i.e. 98765432109876543210),

    x = 5


    Now, if you see the Data Type of x,

    puts x.class


    It is of Bignum type.

    Bignum

  3. Float Data Type



    The Data Type for a floating point number is Float.

    Example :



    x = 5.987
    puts x.class
    


    Output :



      Float


    Now, us look at a different example with Float.

    Example :



    x = 0.1
    y = 0.2
    z = x + y
    puts z
    


    Output :



      0.30000000000000004


    The output is a little weird. The added result of (0.1 + 0.2) should be 0.3. But the output is completely unexpected.

    This is because Float doesn't gives us accurate result always. And if you want accurate result after the point .. You need to use BigDecimal Data Type .

  4. BigDecimal Data Type



    The BigDecimal Data Type is almost same as Float data type. And as we have seen the drawback of Float.

    This is where BigDecimal Data Type comes to rescue. But just remember, BigDecimal is very slow compared to Float.

    Let us rewrite the same example using BigDecimal.

    Example :



    require 'bigdecimal'
    
    x = BigDecimal("0.1")
    y = BigDecimal("0.2")
    
    z = x + y
    if (z == 0.3)
    	puts "The result matches 0.3"
    end
    


    Output :



      The result matches 0.3


    So, in the above example, we have taken the same numbers, i.e. 0.1 and 0.2 and the first thing we have done is, included bigdecimal (We will be explaining require keyword in the coming tutorials).

    require bigdecimal


    And converted them to BigDecimal.

    x = BigDecimal("0.1")
    y = BigDecimal("0.2")


    So, we convert the Float numbers 0.1 and 0.2 to BigDecimal using BigDecimal and the value to be converted should be inside the parenthesis ().
    java_Collections


    Now, x and y has BigDecimal numbers 0.1 and 0.2.

    And we add them,'

    z = x + y


    Then in the if block we check, if the added value is equal to 0.3 or not.

    if (z == 0.3)
    	puts "The result matches 0.3"
    end


    And it matched printing the below line as output.

    The result matches 0.3


    So, if you want exact decimal point. You should use BigDecimal instead of Float.

  5. Rational Data Type



    The Data Type for a fractional number is Rational. It should have a prefix r while initialising the value.

    Example :



    x = 4/2r
    puts x.class
    


    Output :



      Rational


    So, in the above example, we have a fractional number 4/2 and we have initialised to variable x.

    x = 4/2r


    But just note, to treat the number as a fractional number, there should be a prefix r.
    java_Collections


    And if we check the Data Type of x.

    puts x.class


    It is of Rational Data Type.