Package com.clarkparsia.pellet.datatypes.types.real

Examples of com.clarkparsia.pellet.datatypes.types.real.Rational


  private static BigDecimal decimal(String s) {
    return new BigDecimal( s );
  }

  private static Rational rational(Number n, Number d) {
    return new Rational( n, d );
  }
View Full Code Here


  private static Number decimal(String s) {
    return new BigDecimal(s);
  }

  private static Rational rational(Number a, Number b) {
    return new Rational(a, b);
  }
View Full Code Here

    assertEquals( 0, rational( "-1/3" ).compareTo( rational( "-17/51" ) ) );
    assertEquals( 1, rational( "-1/3" ).compareTo( rational( "-18/51" ) ) );
    assertEquals( -1, rational( "-1/3" ).compareTo( rational( "-16/51" ) ) );

    assertEquals( 0, new Rational( 1, 3 ).compareTo( new Rational( 1l, 3l ) ) );
  }
View Full Code Here

    assertFalse( rational( "1/3" ).equals( rational( "-1/3" ) ) );

    /*
     * The following holds because !Integer(1).equals(Long(1))
     */
    assertFalse( new Rational( 1, 3 ).equals( new Rational( 1l, 3l ) ) );
  }
View Full Code Here

  /**
   * Test parsing of valid values
   */
  @Test
  public void validParse() {
    Rational r;

    r = Rational.valueOf( "1 / 3" );
    assertNotNull( r );
    assertEquals( new Rational( OWLRealUtils.getCanonicalObject( 1 ), OWLRealUtils
        .getCanonicalObject( 3 ) ), r );

    r = Rational.valueOf( "12 / 36" );
    assertNotNull( r );
    assertEquals( new Rational( OWLRealUtils.getCanonicalObject( 12 ), OWLRealUtils
        .getCanonicalObject( 36 ) ), r );

    r = Rational.valueOf( "-1 / 3" );
    assertNotNull( r );
    assertEquals( new Rational( OWLRealUtils.getCanonicalObject( -1 ), OWLRealUtils
        .getCanonicalObject( 3 ) ), r );
  }
View Full Code Here

          return ((BigDecimal) n).toBigIntegerExact();
        } catch( ArithmeticException e ) {
          throw new IllegalArgumentException( e );
        }
      case RATIONAL:
        Rational r = (Rational) n;
        if( compare( 1, r.getDenominator() ) != 0 ) {
          r = Rational.simplify( r );
          if( compare( 1, r.getDenominator() ) != 0 )
            throw new IllegalArgumentException();
        }
        return bigInteger( r.getNumerator() );
      default:
        throw new IllegalArgumentException();
      }
    case BIG_DECIMAL:
      switch ( in ) {
      case BYTE:
      case SHORT:
      case INTEGER:
      case LONG:
        return BigDecimal.valueOf( n.longValue() );
      case BIG_INTEGER:
        return new BigDecimal( (BigInteger) n );
      case BIG_DECIMAL:
        return n;
      default:
      }
    case RATIONAL:
      switch ( in ) {
      case BYTE:
      case SHORT:
      case INTEGER:
      case LONG:
      case BIG_INTEGER:
        return new Rational( n, 1 );
      case BIG_DECIMAL:
        final BigDecimal d = (BigDecimal) n;
        Number num = d.unscaledValue();
        int scale = d.scale();
        Number denom = BigInteger.TEN.pow( scale );
        return new Rational( num, denom );
      case RATIONAL:
        return n;
      default:
      }
    default:
View Full Code Here

    // TODO: re-implement shrink to avoid always going to BigInteger
    if( isInteger( n ) )
      return shrinkBigInteger( bigInteger( n ) );
    else {
      if( Type.RATIONAL.equals( t ) ) {
        final Rational r = (Rational) n;
        if( r.isQuotientExact() )
          return getCanonicalObject( r.getQuotient() );
        else
          return Rational.simplify( r );
      }
      else if( Type.BIG_DECIMAL.equals( t ) ) {
        final BigDecimal d = (BigDecimal) n;
View Full Code Here

    if( t.equals( Type.BIG_DECIMAL ) )
      return true;

    if( t.equals( Type.RATIONAL ) ) {
      Rational ratVal = (Rational) n;
      return ratVal.isQuotientExact();
    }

    throw new IllegalStateException();
  }
View Full Code Here

    if( t.equals( Type.BIG_DECIMAL ) ) {
      BigDecimal decVal = (BigDecimal) n;
      return BigInteger.ZERO.equals(decVal.unscaledValue()) || decVal.stripTrailingZeros().scale() <= 0;
    }
    else if( t.equals( Type.RATIONAL ) ) {
      Rational ratVal = (Rational) n;
      if( compare( 1, ratVal.getDenominator() ) == 0 )
        return true;
      else
        return compare( 1, Rational.simplify( ratVal ).getDenominator() ) == 0;
    }
View Full Code Here

        return shrinkBigInteger( dandr[0].toBigIntegerExact() );
      else
        return shrinkBigInteger( dandr[0].toBigIntegerExact().add( BigInteger.ONE ) );
    }
    else if( Type.RATIONAL.equals( t ) ) {
      final Rational r = (Rational) n;
      return roundCeiling( r.getQuotient() );
    }
    else
      throw new IllegalStateException();
  }
View Full Code Here

TOP

Related Classes of com.clarkparsia.pellet.datatypes.types.real.Rational

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.