Class BigIntegerToRationalDecorator

java.lang.Object
com.singularsys.extensions.field.implementations.FieldDecorator
com.singularsys.extensions.field.implementations.BigIntegerToRationalDecorator
All Implemented Interfaces:
FieldI, IntegerConvertor, Serializable

public class BigIntegerToRationalDecorator extends FieldDecorator
Converts BigIntegers, Long, Integer and Short to Rationals on input. Optionally on output if the rational number represents an integer then convert it back to a BigInteger.

This should be used to wrap an RationalField. Problems will occur if combined with an IntegerField as the IntegerField will convert a fraction like "5/2" into integer division giving a result of the integer 2. Instead, to mix Rationals and BigIntegers use

     RationalField rf = new RationalField();
     BigIntegerToRationalDecorator ird = new BigIntegerToRationalDecorator(rf,true);
     FieldOperatorTable fot = new FieldOperatorTable(ird);
     BigIntegerNumberFactory inf = new BigIntegerNumberFactory();
     Jep jep = new Jep(fot,inf);
 
which performing all calculations in rationals and converts results to BigInteger if possible.

To mix Integers and Rationals use

     RationalField rf = new RationalField();
     BigIntegerToRationalDecorator ird = new BigIntegerToRationalDecorator(rf,true);
     IntegerToBigIntegerDecorator ibid = new IntegerToBigIntegerDecorator(ird,true);
     FieldOperatorTable fot = new FieldOperatorTable(ibid);
     IntegerNumberFactory inf = new IntegerNumberFactory();
     Jep jep = new Jep(fot,inf);
 
See Also:
  • Constructor Details

    • BigIntegerToRationalDecorator

      public BigIntegerToRationalDecorator(FieldI field)
    • BigIntegerToRationalDecorator

      public BigIntegerToRationalDecorator(FieldI field, boolean simplifyResults)
  • Method Details

    • convertInput

      public Object convertInput(Object in)
      Converts BigInteger, Integer, Long and Short to Rational.
      Specified by:
      convertInput in class FieldDecorator
      Parameters:
      in -
      Returns:
      If appropriate convert in to a different type, otherwise return in.
    • convertOutput

      public Object convertOutput(Object out)
      If possible convert Rationals to BigIntegers.
      Specified by:
      convertOutput in class FieldDecorator
      Parameters:
      out -
      Returns:
      If appropriate convert out to a different type, otherwise return out.