Пример #1
0
Value* NumberValue::multiply(const Value &v) const {
  const NumberValue* n;
  NumberValue* ret;
  
  if (isNumber(v)) {
    n = static_cast<const NumberValue*>(&v);
    ret = new NumberValue(getValue());
    
    if (type == NUMBER) 
      ret->setType(*n);
    else {
      ret->setType(*this);
      ret->verifyUnits(*n);
    }
    ret->setValue(ret->getValue() * n->getValue());
    return ret;

  } else if (v.type == COLOR) {
    return static_cast<const Color*>(&v)->multiply(*this);
    
  } else if(v.type == STRING) {
    return static_cast<const StringValue*>(&v)->multiply(*this);

  } else {
    throw new ValueException("Unsupported type.", *this->getTokens());
  }
}
Пример #2
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;
}    
Пример #3
0
Value* NumberValue::add(const Value &v) const {
  const NumberValue* n;
  const StringValue* s;
  NumberValue* nret;
  StringValue* sret;
  
  if (isNumber(v)) {
    n = static_cast<const NumberValue*>(&v);
    nret = new NumberValue(getValue());
    if (type == NUMBER) 
      nret->setType(*n);
    else {
      nret->setType(*this);
      nret->verifyUnits(*n);
    }
    nret->setValue(nret->getValue() + n->getValue());
    
    return nret;
    
  } else if (v.type == COLOR) {
    return static_cast<const Color*>(&v)->add(*this);
    
  } else if (v.type == STRING) {
    s = static_cast<const StringValue*>(&v);
    sret = new StringValue(*this, s->getQuotes());
    sret->append(v);
    return sret;

  } else {
    throw new ValueException("Unsupported type.", *this->getTokens());
  }
}
Пример #4
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;
}   
Пример #5
0
Value* NumberValue::abs(const vector<const Value*> &args) {
  if (!NumberValue::isNumber(*args[0]))
    throw new ValueException("abs() only works on numeric "
                             "values", *args[0]->getTokens());

  NumberValue* n = new NumberValue(*(const NumberValue*)args[0]);
  n->setValue(fabs(n->getValue()));
  return n;
}
Пример #6
0
boost::shared_ptr<AbstractValue> NumberValue<T, TypeID>::clone()
{
    NumberValue<T, TypeID>* numVal = new NumberValue<T, TypeID>();
    numVal->setValue(this->value());
    
    boost::shared_ptr<AbstractValue> ptr(numVal);
    
    return ptr;
}
Пример #7
0
Value* NumberValue::pow(const vector<const Value*> &args) {
  if (!NumberValue::isNumber(*args[0]))
    throw new ValueException("pow() only works on numeric values",
                             *args[0]->getTokens());

  NumberValue* n = new NumberValue(*(const NumberValue*)args[0]);
  double exp = ((const NumberValue*)args[1])->getValue();
  
  n->setValue(std::pow(n->getValue(), exp));
  return n;
}    
Пример #8
0
Value* NumberValue::mod(const vector<const Value*> &args) {
  if (!NumberValue::isNumber(*args[0]) ||
      !NumberValue::isNumber(*args[1]))
    throw new ValueException("mod() only works on numeric values",
                             *args[0]->getTokens());

  NumberValue* n = new NumberValue(*(const NumberValue*)args[0]);
  double val2 = ((NumberValue*)args[1])->getValue();

  n->setValue(std::fmod(n->getValue(), val2));
  return n;
}    
Пример #9
0
Value* NumberValue::floor(const vector<const Value*> &args) {
  NumberValue *n;
  
  if (!NumberValue::isNumber(*args[0]))
    throw new ValueException("floor() only works on numeric "
                             "values", *args[0]->getTokens());

  n = new NumberValue(*static_cast<const NumberValue*>(args[0]));

  double val = n->getValue();
  n->setValue(std::floor(val));
  return n;
}  
Пример #10
0
Value* NumberValue::round(const vector<const Value*> &args) {
  if (!NumberValue::isNumber(*args[0]))
    throw new ValueException("round() only works on numeric "
                             "values", *args[0]->getTokens());

  NumberValue* n = new NumberValue(*(const NumberValue*)args[0]);
  double val = n->getValue();
  double decimalplaces = 0;
  
  if (args.size() > 1)
    decimalplaces = ((const NumberValue*)args[1])->getValue();

  val = val * std::pow(10, decimalplaces);
  val = std::floor(val + 0.5);
  val = val / std::pow(10, decimalplaces);
  n->setValue(val);
  return n;
}  
Пример #11
0
Value* NumberValue::divide(const Value &v) const {
  const NumberValue* n;
  NumberValue* ret;

  if (isNumber(v)) {
    n = static_cast<const NumberValue*>(&v);
    ret = new NumberValue(getValue());
    
    if (type == NUMBER) 
      ret->setType(*n);
    else {
      ret->setType(*this);
      ret->verifyUnits(*n);
    }
    ret->setValue(ret->getValue() / n->getValue());
    
    return ret;
  } else
    throw new ValueException("You can only divide a number "
                             "by a *number*.", *this->getTokens());
}
Пример #12
0
Value* NumberValue::substract(const Value &v) const {
  const NumberValue* n;
  NumberValue* ret;
  
  if (isNumber(v)) {
    n = static_cast<const NumberValue*>(&v);
    ret = new NumberValue(getValue());

    if (type == NUMBER) 
      ret->setType(*n);
    else {
      ret->setType(*this);
      ret->verifyUnits(*n);
    }

    ret->setValue(ret->getValue() - n->getValue());

    return ret;
  } else
    throw new ValueException("You can only substract a "
                             "*number* from a number.", *this->getTokens());
}
Пример #13
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;
}