hu.birot.OTKit.dataType.violation
Class FloatViolation

java.lang.Object
  extended by hu.birot.OTKit.dataType.violation.Violation
      extended by hu.birot.OTKit.dataType.violation.FloatViolation

public class FloatViolation
extends Violation

A violation with a (double) floating point value.

This subclass of the class Violation is a generalization of the traditional violations, allowing not only integer, and not only non-negative values. Yet, if traditional constraints are used, the subclass IntegerViolation is recommended, which makes computations faster and more precise.

Note the method v(double), which returns an already constructed violation, unless no such violation was needed so far. Using this method instead of the constructor FloatViolation(double violation_level) is more efficient, because of several reasons: the time needed for constructing new violations (and for the corresponding garbage collection) is saved, and the Violation.isEqual(Violation,Violation) is also faster if the two violations refer to the same object. So, the use of the static method v is highly recommended, unless extremely many different violation levels are needed.

Concerning possible errors due to rounding errors in floating point arithmetics: see the documentation of the constant field EPSILON. In short: make sure you always use doubles and not floats to construct real-valued violations.

Two constant violations complement this class: FloatViolation.nullViolation (corresponding to the case when the constraint is satisfied), and FloatViolation.oneViolation (corresponding to a single violation).

See Also:
Violation, IntegerViolation

Field Summary
static java.lang.RuntimeException ArgumentNotFloatViolation
          Exception "Argument is not FloatViolation".
protected  java.util.regex.Pattern delete_after_comma
           
static double EPSILON
          A constant used to avoid errors due to inexact floating point arithmetics.
static FloatViolation nullViolation
          Zero violation level: the constraint is satisfied.
static FloatViolation oneViolation
          One violation level: the constraint is violated once.
 
Fields inherited from class hu.birot.OTKit.dataType.violation.Violation
thisclass
 
Constructor Summary
FloatViolation(double f)
          Construct a Violation (that is, a subclass FloatViolation) with value f.
 
Method Summary
 boolean betterEqual(Violation v)
          Compares the present Violation to Violation v, and returns true if and only if this violation is better than or equal to v.
 Violation clone()
          Creates a clone of the Violation, with the same value.
 double difference(Violation v)
          Returns the difference of this violation and of violation v (in this order: "this-v").
 boolean equals(java.lang.Object o)
          Equality of two violations.
 int hashCode()
          A hash code corresponding to the Violation.
 java.lang.String toString()
          Transforms a Violation into a String, returning the decimal representation of the violation.
static FloatViolation v(double v)
          "Quick constructor".
 java.lang.Double value()
          Returns the value of the violation.
 
Methods inherited from class hu.birot.OTKit.dataType.violation.Violation
isBetter, isEqual, notComparable
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

nullViolation

public static final FloatViolation nullViolation
Zero violation level: the constraint is satisfied. Using this pre-compiled value can speed up computations.


oneViolation

public static final FloatViolation oneViolation
One violation level: the constraint is violated once. Using this pre-compiled value can speed up computations.


EPSILON

public static final double EPSILON

A constant used to avoid errors due to inexact floating point arithmetics. If a violation is constructed using a value less than EPSILON (and larger than -EPSILON), then the resulting violation is a 0 violation.

EPSILON = 10^(-9) = 0.000000001.

Note that other possible errors resulting from inaccuracies in floating point arithmetics is solved by the fact that the constructor FloatViolation(double r) recasts r into float. So, if you use double float arithmetics instead of single float arithmetics, you should not have any problem. (If you do, please let me know about it.) For example, 1.1-0.9 will not be 0.2 exactly, but at least it will be equal to 1.5-1.3.

Otherwise, you can have unfortunate situations in OT such as candidate 1 becomes better than candidate 2, simply because candidate 1 violates a constraint 1.000 times, whereas candidate 2 violates it (due to rounding errors) 1.000000000000014 times.

See Also:
Constant Field Values

ArgumentNotFloatViolation

public static java.lang.RuntimeException ArgumentNotFloatViolation
Exception "Argument is not FloatViolation". It is thrown by the method FloatViolation.difference(Violation v) whenever the argument is not a FloatViolation. To be more precise, whenever v.value() for the argument v cannot be cast to Double.

See Also:
difference(Violation)

delete_after_comma

protected final java.util.regex.Pattern delete_after_comma
Constructor Detail

FloatViolation

public FloatViolation(double f)
Construct a Violation (that is, a subclass FloatViolation) with value f. To be more exact, f is recast to float, and to 0.0f, if the absolute value of f is less than EPSILON.

Parameters:
f - : The float value defining the violation level.
See Also:
EPSILON
Method Detail

value

public java.lang.Double value()

Returns the value of the violation.

Specified by:
value in class Violation
Returns:
The value represented by the violation, a floating point value.
See Also:
IntegerViolation.value(), value()

v

public static FloatViolation v(double v)

"Quick constructor". Returns the violation with value v that has been already constructed earlier. If no violation with that value has ever been constructed, then it is first created.

With equal arguments, this method always returns the same violation; that is, not only equal violations, with respect to the equals method. Subsequently, several methods comparing two violations run more efficiently, and less time is spent on constructing new violations, and then on collecting garbage. Therefore it is recommended to use this static method in the definition of constraints, and not the standard constructor of the violations.

If the number of different violation levels is extremely high, and each of them is used only very seldom, then keeping all of them in memory may nevertheless be less efficient.

Parameters:
v - Value of the violation to be returned.
Returns:
The violation with value v, the only one constructed.

betterEqual

public boolean betterEqual(Violation v)

Compares the present Violation to Violation v, and returns true if and only if this violation is better than or equal to v.

More specifically, it returns true if and only if the floating point value of this violation is less than or equal to v.value() – provided that v is also an instance of the FLoatViolation class. For other types of violations, the method always returns false. For instance, an instance of IntegerViolation cannot be compared to an instance of FloatViolation, even if everyday arithmetics would make their comparison possible.

This method implements Violation.betterEqual, and is used in the static comparison methods in Violation.

Specified by:
betterEqual in class Violation
Parameters:
v - The second violation, to which this violation is compared.
Returns:
true if this violation is better than or equal to v, and if they are both instances of FloatViolation. Otherwise false is returned.
See Also:
Violation.betterEqual(Violation), Violation.isBetter(Violation, Violation), Violation.isEqual(Violation, Violation), Violation.notComparable(Violation, Violation)

difference

public double difference(Violation v)

Returns the difference of this violation and of violation v (in this order: "this-v").

If violation v is not an instance of FloatViolation, or if v.value() cannot be cast to Double, then exception FloatViolation.ArgumentNotFloatViolation is thrown. This happens even if v belongs to another numeric violation subclass, such as IntegerViolation.

Otherwise, the arithmetic difference of the two values is returned: this.value()-v.value() (the value of the present violation minus the value of v).

Specified by:
difference in class Violation
Parameters:
v - The subtrahend violation.
Returns:
The difference of the two violations (this - v).
See Also:
ArgumentNotFloatViolation, Violation.difference(Violation)

equals

public boolean equals(java.lang.Object o)
Equality of two violations. Returns true if and only if o belongs to the same class (FloatViolation) and the value() of the two violations are equal. This method overrides java.lang.Object.equals.

Overrides:
equals in class Violation
Parameters:
o - Object to be compared to the current violation.
Returns:
Value true for FloatViolations whose value are equal to the value of the current violation; false in all other cases.
See Also:
Violation.equals(Object)

clone

public Violation clone()
Creates a clone of the Violation, with the same value. This method overrides java.lang.Object.clone.

Specified by:
clone in class Violation

toString

public java.lang.String toString()

Transforms a Violation into a String, returning the decimal representation of the violation. Overrides the method java.lang.Object.toString.

Specified by:
toString in class Violation
Returns:
A string representation of the Violation.

hashCode

public int hashCode()

A hash code corresponding to the Violation. It is the hash code produced by the string representation of value.

Overrides:
hashCode in class Violation
Returns:
The hash code corresponding to the Violation