Package com.confluenity.jaylen.entity.math

Source Code of com.confluenity.jaylen.entity.math.Decimal

/*
* Copyright (c) 2013-2014. Confluenity
* This content is released under the Apache 2 license:
* http://www.apache.org/licenses/LICENSE-2.0
*/

package com.confluenity.jaylen.entity.math;

import ral.Real;

import java.math.BigDecimal;

/**
* Wrapper around {@link ral.Real} class.
*/
public class Decimal {

  private Real value;

  public Decimal() {
    this.value = new Real();
  }

  public Decimal(Decimal value) {
    this.value = new Real(value.value);
  }

  public Decimal(double value) {
    this(new BigDecimal(value));
  }

  public Decimal(BigDecimal value) {
    this.value = new Real(value.toString());
  }

  public Decimal(Real value) {
    this.value = new Real(value);
  }

  public Decimal(String value) {
    this.value = new Real(value);
  }

  public Decimal(int value) {
    this.value = new Real(value);
  }

  public Decimal(long value) {
    this.value = new Real(value);
  }

  public Decimal(String value, int base) {
    this.value = new Real(value, base);
  }

  public Decimal(byte[] date, int offset) {
    this.value = new Real(date, offset);
  }

  /**
   * {@link ral.Real#mantissa}
   */
  public long getMantissa() {
    return this.value.mantissa;
  }

  /**
   * {@link ral.Real#exponent}
   */
  public long getExponent() {
    return this.value.exponent;
  }

  /**
   * {@link ral.Real#sign}
   */
  public byte getSign() {
    return this.value.sign;
  }

  /**
   * Executes addition and returns value of {@code this.value} + {@code other.value} according to {@link ral.Real#add(ral.Real)}.
   * @param other summand.
   * @return new instance of {@code Decimal} which value is {@code this.value} + {@code other.value}.
   */
  public Decimal add(final Decimal other) {
    Real result = new Real(this.value);
    result.add(other.value);
    return new Decimal(result);
  }

  /**
   * Executes subtraction and returns value of {@code this.value} - {@code other.value} according to {@link ral.Real#sub(ral.Real)}.
   * @param other subtrahend.
   * @return new instance of {@code Decimal} which value is {@code this.value} - {@code other.value}.
   */
  public Decimal sub(final Decimal other) {
    Real result = new Real(this.value);
    result.sub(other.value);
    return new Decimal(result);
  }

  /**
   * Calculates square of {@code this.value} according to {@link ral.Real#sqr()}.
   * @return new instance of {@code Decimal} which value is square of {@code this.value}.
   */
  public Decimal sqr() {
    Real result = new Real(this.value);
    result.sqr();
    return new Decimal(result);
  }

  /**
   * Calculates square root of {@code this.value} according to {@link ral.Real#sqrt()}.
   * @return new instance of {@code Decimal} which value is square root of {@code this.value}.
   */
  public Decimal sqrt() {
    Real result = new Real(this.value);
    result.sqrt();
    return new Decimal(result);
  }

}
TOP

Related Classes of com.confluenity.jaylen.entity.math.Decimal

TOP
Copyright © 2018 www.massapi.com. 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.