Friday, 4 April 2014

Floating-Point types

TARGET LANGUAGE:JAVA
THEORY:(DATA TYPE:Float)

Floating Point Type

Floating point numbers also known as real numbers are used when evaluating expressions that require fractional precision.

Example:

  1. Calculations such as  square root..
Program:

public class CalculateSquareRootWithMathSqrt
 {
 public static void main(String[] args) 
        {       
                float a=30;
                 
  // square root of 16 which equals with 4
  System.out.println(Math.sqrt(16));
   
  // square root of 44 which is 6.6332495807108
  System.out.println(Math.sqrt(44));
  
  // sin of 30 is -0.9880316240928618
  System.out.print(Math.sin(a));
  
  //cos of 30 is 0.15425144988758405
  System.out.print(Math.cos(a));
        }

}

There are two kinds of floating point types Float and double.
Their ranges are specified in the table below.

Primitive  data Types

FLOAT

  • The type specifies a single precision value that uses 32 bit of storage.
  • Variables of float are useful when you need a fractional component but don't require large degree of precision. 
DOUBLE

  • The type specifies double  precision values that uses 64 bit of storage
  • Variables of float are useful when you need a fractional component but  require large degree of precision. 
Program: Area of circle

class Area
{
 public static void main(String args[])
 {
   double pi,r,a;
   r=10.7;//radius of circle
   pi=3.14;//pi,approximately
   a=pi*r*r;//compute area
   System.out.print("Area of circle"+a);
 }
}
---------------------------------------------
In the next publication,we shall see character type and then literals.
---------------------------------------------
After which we shall take a closer look at the simple program...in the next Publication 
--------------------------------------------


No comments:

Post a Comment