Exemplo n.º 1
0
Value* NumberValue::tan(const vector<const Value*> &args) {
  if (args[0]->type != Value::NUMBER &&
      args[0]->type != Value::DIMENSION) {
    throw new ValueException("tan() only works on numbers "
                             "or dimensions", *args[0]->getTokens());
  }
  NumberValue* n = new NumberValue(*(const NumberValue*)args[0]);
  double val = n->getValue();
  std::string unit;
    
  if (n->type == Value::DIMENSION) {
    unit = n->getUnit();
    if(unit.compare("rad") != 0 &&
       unit.compare("deg") != 0 &&
       unit.compare("grad") != 0 &&
       unit.compare("turn") != 0) {
      throw new ValueException("ta() requires rad, deg, "
                               "grad or turn units.", *args[0]->getTokens());
    }
    val = UnitValue::angleToRad(val, unit);
  }

  n->setValue(std::tan(val));
  n->type = Value::NUMBER;
  n->setUnit("");
  return n;
}    
Exemplo n.º 2
0
// DIMENSION unit(DIMENSION, UNIT)
Value* NumberValue::unit(const vector<const Value*> &arguments) {
  NumberValue* ret;
  
  if (arguments[0]->type == Value::NUMBER ||
      arguments[0]->type == Value::DIMENSION) {
    ret = new NumberValue(((const NumberValue*)arguments[0])->getValue());
    
    if (arguments.size() > 1) {
      ret->setUnit(((const UnitValue*)arguments[1])->getUnit());
    } else
      ret->setUnit("");
    return ret;
  } else 
    throw new ValueException("argument 1 has to be a number "
                             "or dimension", *arguments[0]->getTokens());
}
Exemplo n.º 3
0
Value* NumberValue::atan(const vector<const Value*> &args) {
  NumberValue* n = new NumberValue(*(const NumberValue*)args[0]);

  n->setValue(std::atan(n->getValue()));
  n->setUnit("rad");
  n->type = Value::DIMENSION;
  return n;
}   
Exemplo n.º 4
0
Value* NumberValue::convert(const vector<const Value*> &args) {
  if (!NumberValue::isNumber(*args[0]))
    throw new ValueException("convert() only works on numeric values",
                             *args[0]->getTokens());  
  if (args[1]->type != Value::STRING &&
      args[1]->type != Value::UNIT) {
    throw new ValueException("convert() requires a unit \
(or unit as a string)",
                             *args[1]->getTokens());
  }

  NumberValue* n = new NumberValue(*(const NumberValue*)args[0]);
  std::string unit;
    
  if (args[1]->type == Value::STRING)
    unit = ((const StringValue*)args[1])->getString();
  else
    unit.append(((const UnitValue*)args[1])->getUnit());

  n->setValue(n->convert(unit));
  n->setUnit(unit);
  return n;
}