Welcome





Tuesday, February 8, 2011

COPY CONSTRUCTOR IN JAVA

Click this link to view the presentation regarding Copy Constructor Concept in Java.

Wednesday, February 2, 2011

ROUND OFF ERRORS:

      A round-off error, also called rounding error, is the difference between the calculated approximation of a number and its exact mathematical value.
v                          When dealing with floating-point numbers, computers cannot store exact values. They may store floating point values to many decimal places, and calculate results to many decimal places of precision, but round-off error will always be present. To ensure that results of floating-point routines are meaningful, the need exists to quantify the round-off error of such routines.
v       
         Machine epsilon, epsmch, is defined as the smallest positive number such that 1.0 + epsmch is not equal to 1.0.
v      If you are familiar with the "C" or "C++" programming languages, epsmch is supplied in one of the C library files as a constant: DBL_EPSILON. It is accessed by including the header file float.h in the "C" or "C++" program.



 Precision  vs.  Accuracy.
    Precision = tightness of specification. Accuracy = correctness. Do not confuse precision with accuracy. 3.133333333 is an estimate of the mathematical constant π which is specified with 10 decimal digits of precision, but it only has two decimal digits of accuracy. As John von Neumann once said "There's no sense in being precise when you don't even know what you're talking about." Java typically prints out floating point numbers with 16 or 17 decimal digits of precision, but do not blindly believe that this means there that many digits of accuracy! Calculators typically display 10 digits, but compute with 13 digits of precision.


         Kahan : the mirror for the Hubble space telescope was ground with great precision, but to the wrong specification. Hence, it was initially a great failure since it couldn't produce high resolution images as expected.However, it's precision enabled an astronaut to install a corrective lens to counter-balance the error.



Ø ROUNDING MODES :
§         round to nearest, where ties round to the nearest even digit in the required position (the default and by far the most common mode)
§       round to nearest, where ties round away from zero (optional for binary floating-point and commonly used in decimal)
§       round up (toward +∞; negative results thus round toward zero)
§        round down (toward −∞; negative results thus round away from zero)
§       round toward zero (truncation; it is similar to the common behavior of float-to-integer conversions, which convert −3.9 to −3)

 

Floating-point rounding:

 

        In floating-point arithmetic, rounding aims to turn a given value x into a value z with a specified number of significant digits. In other words, z should be a multiple of a number m that depends on the magnitude of z. The number m is a power of the base (usually 2 or 10) of the floating-point representation.
       Apart from this detail, all the variants of rounding discussed above apply to the rounding of floating-point numbers as well. The algorithm for such rounding is presented in the Scaled rounding section above, but with a constant scaling factor s=1, and an integer base b>1.
       For results where the rounded result would overflow the result for a directed rounding is either the appropriate signed infinity, or the highest representable positive finite number (or the lowest representable negative finite number if x is negative), depending on the direction of rounding. The result of an overflow for the usual case of round to even is always the appropriate infinity.
       In addition, if the rounded result would underflow, i.e. if the exponent would exceed the lowest representable integer value, the effective result may be either zero (possibly signed if the representation can maintain a distinction of signs for zeroes), or the smallest representable positive finite number (or the highest representable negative finite number if x is negative), possibly a denormal positive or negative number (if the mantissa is storing all its significant digits, in which case the most significant digit may still be stored in a lower position by setting the highest stored digits to zero, and this stored mantissa does not drop the most significant digit, something that is possible when base b=2 because the most significant digit is always 1 in that base), depending on the direction of rounding. The result of an underflow for the usual case of round to even is always the appropriate zero.
        It is important to understand that the number of significant digits in a value provides, only a rough indication of its precision, and that information is lost when rounding off occurs.

THE strictfp CONSTRUCT:

       strictfp is a keyword in the Java programming language that restricts floating-point calculations to ensure portability. It was introduced into Java with the Java virtual machine (JVM) version 1.2.

          The IEEE standard, specifies a standard method for both floating-point calculations and storage of floating-point values in either single or double precision. Prior to JVM 1.2, floating-point calculations were strict; that is, all intermediate floating-point results were represented as IEEE single or double precisions. As a consequence, errors of calculation, overflows and underflows, could occur. Whether or not an error had occurred, the calculation would always return a valid number; if an overflow or underflow had occurred, that number would be incorrect. Hence, whether an error had occurred was typically not obvious. Since JVM 1.2, intermediate computations are not limited to the standard 32- and 64- bit precisions. On platforms that can handle other representations, those representations can be used, preventing overflows and underflows, thereby increasing precision. For some applications, a programmer might need every platform to have precisely the same floating-point behavior, even on platforms that could handle greater precision. The strictfp modifier accomplishes this by truncating all intermediate values to IEEE single- and double- precision, as occurred in earlier versions of the JVM[1].

Usage:
          Programmers can use the modifier strictfp to ensure that calculations are performed as in the earlier versions; that is, only with IEEE single and double precision types used. Using strictfp guarantees that results of floating-point calculations are identical on all platforms. This can be extremely useful when comparing floating-point numbers.
         It can be used on classesinterfaces and non-abstract methods .When applied to a method, it causes all calculations inside the method to use strict floating-point math. When applied to a class, all calculations inside the class uses stritfp floating-point math. Compile-time constant expressions must always use strict floating-point behavior

      Ø  Java's default class Math uses this strictfp modifier.
The Java package java.lang.Math class contains these strictfp methods:
 public static strictfp double abs(double);
 public static strictfp int max(int, int);
 public static strictfp long max(long, long);
 public static strictfp float max(float, float);
 public static strictfp double max(double, double



·       How to use strictfp ??
Ø Syntax for classes:
public strictfp class MyClass 
{ 
  //...
}


ØSyntax for methods:
public strictfp void method() 
{ 
  //...
}