Example #1
0
Value Iterator::Prod(Environment &env)
{
	Signal &sig = env.GetSignal();
	Value value;
	if (!Next(env, value)) return Value::Nil;
	if (!value.Is_number()) {
		const Operator *pOperatorMul = env.GetOperator(OPTYPE_Mul);
		Value valResult(value);
		while (Next(env, value)) {
			valResult = pOperatorMul->EvalBinary(env, valResult, value, FLAG_None);
			if (sig.IsSignalled()) return Value::Nil;
		}
		return valResult;
	}
	Number result = value.GetNumber();
	while (Next(env, value)) {
		if (value.Is_number()) {
			if (result == 0) break;
			result *= value.GetNumber();
		} else {
			const Operator *pOperatorMul = env.GetOperator(OPTYPE_Mul);
			Value valResult(result);
			do {
				valResult = pOperatorMul->EvalBinary(env, valResult, value, FLAG_None);
				if (sig.IsSignalled()) return Value::Nil;
			} while (Next(env, value));
			return valResult;
		}
	}
	return Value(result);
}
Example #2
0
Value Iterator::Sum(Environment &env, size_t &cnt)
{
	Signal &sig = env.GetSignal();
	cnt = 0;
	Value value;
	if (!Next(env, value)) return Value::Nil;
	if (!value.Is_number()) {
		const Operator *pOperatorAdd = env.GetOperator(OPTYPE_Add);
		Value valResult(value);
		cnt = 1;
		while (Next(env, value)) {
			valResult = pOperatorAdd->EvalBinary(env, valResult, value, FLAG_None);
			cnt++;
			if (sig.IsSignalled()) return Value::Nil;
		}
		return valResult;
	}
	Number result = value.GetNumber();
	cnt = 1;
	while (Next(env, value)) {
		if (value.Is_number()) {
			result += value.GetNumber();
			cnt++;
		} else {
			const Operator *pOperatorAdd = env.GetOperator(OPTYPE_Add);
			Value valResult(result);
			do {
				valResult = pOperatorAdd->EvalBinary(env, valResult, value, FLAG_None);
				cnt++;
				if (sig.IsSignalled()) return Value::Nil;
			} while (Next(env, value));
			return valResult;
		}
	}
	return Value(result);
}
Example #3
0
Value Iterator::Average(Environment &env, size_t &cnt)
{
	Signal &sig = env.GetSignal();
	if (IsInfinite()) {
		SetError_InfiniteNotAllowed(sig);
		return Value::Nil;
	}
	Value valueSum = Clone()->Sum(env, cnt);
	if (valueSum.IsInvalid()) {
		return Value::Nil;
	} else if (valueSum.Is_number()) {
		return Value(valueSum.GetNumber() / static_cast<Number>(cnt));
	} else if (valueSum.Is_complex()) {
		return Value(valueSum.GetComplex() / static_cast<Number>(cnt));
	} else {
		const Operator *pOperatorDiv = env.GetOperator(OPTYPE_Div);
		return pOperatorDiv->EvalBinary(env, valueSum, Value(cnt), FLAG_None);
	}
}