Package ket.math.purpose

Source Code of ket.math.purpose.DoubleValue

/*
* Copyright (C) 2011  Alasdair C. Hamilton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

package ket.math.purpose;

import java.awt.Color;

import java.util.*;

import ket.display.*;
import ket.display.box.Box;
import ket.display.box.BoxGraphic;
import ket.display.box.BoxTools;
import ket.display.box.BoxWord;
import ket.math.Argument;
import ket.math.Purpose;
import ket.math.State;
import ket.math.Symbol;



public class DoubleValue extends NumberValue {

  double value;
  Integer precision;

  public DoubleValue(String value) {
    this.value = Double.parseDouble(value);
    precision = null;
  }

  public void setPrecision(int precision) {
    this.precision = precision;
  }

  public int getPrecision() {
    return precision;
  }

  public DoubleValue(double value) {
    this.value = value;
  }

  public String getValue() {
    return "" + value;
  }

  public String toLatex() {
    return " " + value + " ";
  }

  public boolean hasBracket() {
    return getValue().indexOf("-")!=-1 ; // || value.indexOf("e")!=-1 ) ;
  }

  public String toString() {
    //- return ""+value;
    return hasBracket() ? "("+value+")" : ""+value;
  }

  public int getInt() {
    return (int) value;
  }

  public double getDouble() {
    return value;
  }

  /**
   * Here the inverse of a number is defined as its negative value.
   */
  @Override
  public State getInverse() {
    double number = getDouble();
    return new DoubleValue(-number);
  }

  @Override
  public DoubleValue increase() {
    return new DoubleValue(value+1);
  }

  @Override
  public DoubleValue decrease() {
    return new DoubleValue(value-1);
  }

  @Override
  public DoubleValue half() {
    return new DoubleValue(value/10.0);
  }

  @Override
  public NumberValue twice() {
    return new DoubleValue(value*10.0);
  }

  @Override
  public boolean equals(Purpose purpose) {
    if (purpose instanceof DoubleValue) {
      double thisDouble = this.getDouble();
      DoubleValue doubleValue = (DoubleValue) purpose;
      double thatDouble = doubleValue.getDouble();
      return thisDouble==thatDouble;
    } else {
      return false;
    }
  }

  @Override
  public Box toBox(Argument argument, long settings, ColourScheme colourScheme) {
    if (precision!=null) {
      String value = getValue();
      int index = value.indexOf('.');
      if (precision<0) {
        int shift = index-precision+1;
        if (index!=-1 && shift<value.length()) {
          value = value.substring(0, shift); //ish
        }
      } else {
        //...
      }
      return new BoxWord(argument, value, settings);
    }
    String[] string = Suffix.suffix(value);
    switch (string.length) {
      case 1:
        return new BoxWord(argument, string[0], settings);

      case 2:
        // Repeated pattern new String[]{number.number, pattern};
        BoxWord prefix = new BoxWord(argument, string[0], settings);
        BoxWord pattern = new BoxWord(argument, string[1], settings);
        BoxGraphic horizontalBar = new BoxGraphic(argument, 0L, BoxGraphic.HORIZONTAL_LINE);
        Box argsAndOverbar = BoxTools.centredVerticalBoxList(argument, settings, horizontalBar, pattern);
        Box box = BoxTools.centredHorizontalBoxList(argument, 0L, prefix, argsAndOverbar);
        horizontalBar.setProperty(Box.HORIZONTAL_STRETCH);
        horizontalBar.setProperty(Box.BOTTOM_ALIGN);
        return box;

      default:
        // Never used, but just in case.
        return new BoxWord(argument, getValue(), settings);
    }
  }

  /**
   * Return the hash code of the value of this object.
   */
  public int hashCode() {
    return new Double(value).hashCode();
  }


  public static final Set<Symbol> MATCHES = new HashSet<Symbol>(
      Arrays.asList(
        new Symbol[]{Symbol.RE_VARIABLE, Symbol.RE_ARGUMENT,
          Symbol.RE_TOKEN, Symbol.RE_NUMBER, Symbol.RE_REAL}));

  @Override
  public Set<Symbol> getMatchSymbols() {
    return MATCHES;
  }
}
TOP

Related Classes of ket.math.purpose.DoubleValue

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.