Tuesday, March 27, 2007

TYPES, VALUES AND VARIABLES(2)

TYPES, VALUES AND VARIABLES(2)

FLOATING POINT TYPES, FORMULAS AND VALUES:

Floating point types are float (32-bit, single precision) and double (64-bit, double precision) format for IEEE 754 values and operations. This include positive and negative numbers with sign and magnitude, positive and negative zeros, positive and negative infinities, and special Not-a-Number(NaN) values. NaN is used to represent the result of certain invalid operations.

Every implementation of the java programming language(like your favorite compiler), is required to support these three sets:

  1. Float value set (standard).
  2. Double value set (standard).
  3. One or both of float-extended-exponent value set and double-exponent-extended value set.

1-3 above are not types. Float value set and double value set are standard IEEE 754 formats and can represent a value of float and double respectively, but the float-exponent-extended value set and the double-exponent-extended value set are not and may be used by implementations of the java programming language.

Except for NaN, float point values are ordered.

For some illustrative examples:

package api_package;

class FloatExamples {

static float negfinite = -2.4021e2f, posfinite = 2.4021e2f;

static float poszero = 0.0f, negzero = -0.0f;

static int age = 25;

public static void main(String[] args) {

//let's show that floating-point values are ordered

if (Float.NEGATIVE_INFINITY>negfinite){

System.out.println("No ordering here.");

}else {

System.out.println("Truly, Negative infinity comes"

+" before negative finite nonzero values.");

}

//the next in line

if (negfinite>negzero){

System.out.println("There's no ordering here.");

}else{

System.out.println("Negative finite comes before zero

again.");

}

//both zeros now

if (negzero==poszero){

System.out.println("Both zeros compare equal.");

}else{

System.out.println("Both zeros don't compare

equal.");

}

//operation float if one operand a floating point value

System.out.print("Result a float of: "+(age * posfinite));

//if we do casting to int, we'll get a round toward zero

Float result = age*posfinite;

System.out.println(" which when casted to int is:

"+result.intValue());

//an operation that overflows produces a signed infinity

//remember what happened with int

System.out.println("Overflowing operation produces:

"+Float.MAX_VALUE*5);

}

}

No comments: