117 return a > b ? 1.0 : 0.0;
130 return a < b ? 1.0 : 0.0;
143 return a == b ? 1.0 : 0.0;
156 return a >= b ? 1.0 : 0.0;
169 return a <= b ? 1.0 : 0.0;
182 return a != b ? 1.0 : 0.0;
229 return (a > 0) ? (int)(a + 0.5) : (int)(a - 0.5);
281 if (isnan(a))
return (a);
282 if (isnan(b))
return (b);
283 return (a > b) ? a : b;
295 if (isnan(a))
return (a);
296 if (isnan(b))
return (b);
297 return (a < b) ? a : b;
302 Calculator::Calculator() {
305 p_valStack =
new QStack< QVector<double> >();
309 Calculator::~Calculator() {
320 void Calculator::Negative() {
321 QVector<double> result = Pop();
322 PerformOperation(result, result.begin(), result.end(),
NegateOperator);
330 void Calculator::Multiply() {
331 QVector<double> y = Pop();
332 QVector<double> x = Pop();
333 QVector<double> result;
335 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
MultiplyOperator);
343 void Calculator::Add() {
344 QVector<double> y = Pop();
345 QVector<double> x = Pop();
346 QVector<double> result;
347 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
AddOperator);
355 void Calculator::Subtract() {
356 QVector<double> y = Pop();
357 QVector<double> x = Pop();
358 QVector<double> result;
359 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
SubtractOperator);
367 void Calculator::Divide() {
368 QVector<double> y = Pop();
369 QVector<double> x = Pop();
370 QVector<double> result;
372 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
DivideOperator);
379 void Calculator::Modulus() {
380 QVector<double> y = Pop();
381 QVector<double> x = Pop();
382 QVector<double> result;
384 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
ModulusOperator);
395 void Calculator::Exponent() {
396 QVector<double> exponent = Pop();
397 QVector<double> x = Pop();
398 QVector<double> result;
400 PerformOperation(result, x.begin(), x.end(), exponent.begin(), exponent.end(), pow);
410 void Calculator::SquareRoot() {
411 QVector<double> result = Pop();
412 PerformOperation(result, result.begin(), result.end(), sqrt);
420 void Calculator::AbsoluteValue() {
421 QVector<double> result = Pop();
422 PerformOperation(result, result.begin(), result.end(), fabs);
432 void Calculator::Log() {
433 QVector<double> result = Pop();
434 PerformOperation(result, result.begin(), result.end(), log);
442 void Calculator::Log10() {
443 QVector<double> result = Pop();
444 PerformOperation(result, result.begin(), result.end(), log10);
454 void Calculator::LeftShift() {
455 QVector<double> y = Pop();
457 IString msg =
"When trying to do a left shift calculation, a non-scalar "
458 "shift value was encountered. Shifting requires scalars.";
462 QVector<double> x = Pop();
464 if((
int)y[0] > (
int)x.size()) {
465 IString msg =
"When trying to do a left shift calculation, a shift "
466 "value greater than the data size was encountered. "
467 "Shifting by this value would erase all of the data.";
471 QVector<double> result;
472 int shift = (int)y[0];
473 result.resize(x.size());
475 for(
int i = 0; i < result.size(); i++) {
476 if(i + shift < x.size() && i + shift >= 0)
477 result[i] = x[i+shift];
479 result[i] = sqrt(-1.0);
493 void Calculator::RightShift() {
494 QVector<double> y = Pop();
496 IString msg =
"When trying to do a right shift calculation, a non-scalar "
497 "shift value was encountered. Shifting requires scalars.";
501 QVector<double> x = Pop();
503 if((
int)y[0] > (
int)x.size()) {
504 IString msg =
"When trying to do a right shift calculation, a shift "
505 "value greater than the data size was encountered. "
506 "Shifting by this value would erase all of the data.";
510 QVector<double> result;
511 int shift = (int)y[0];
512 result.resize(x.size());
514 for(
int i = 0; i < (int)result.size(); i++) {
515 if(i - shift < (
int)x.size() && i - shift >= 0) {
516 result[i] = x[i-shift];
519 result[i] = sqrt(-1.0);
531 void Calculator::MinimumLine() {
532 QVector<double> result = Pop();
534 double minVal = result[0];
535 for(
int i = 0; i < result.size(); i++) {
537 minVal = min(minVal, result[i]);
542 result.push_back(minVal);
550 void Calculator::MaximumLine() {
551 QVector<double> result = Pop();
553 double maxVal = result[0];
554 for(
int i = 0; i < result.size(); i++) {
556 maxVal = max(maxVal, result[i]);
561 result.push_back(maxVal);
570 void Calculator::MinimumPixel() {
571 QVector<double> x = Pop();
572 QVector<double> y = Pop();
573 QVector<double> result;
575 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
585 void Calculator::MaximumPixel() {
586 QVector<double> x = Pop();
587 QVector<double> y = Pop();
588 QVector<double> result;
590 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
600 void Calculator::GreaterThan() {
601 QVector<double> y = Pop();
602 QVector<double> x = Pop();
603 QVector<double> result;
605 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
615 void Calculator::LessThan() {
616 QVector<double> y = Pop();
617 QVector<double> x = Pop();
618 QVector<double> result;
620 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
630 void Calculator::Equal() {
631 QVector<double> y = Pop();
632 QVector<double> x = Pop();
633 QVector<double> result;
635 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
645 void Calculator::GreaterThanOrEqual() {
646 QVector<double> y = Pop();
647 QVector<double> x = Pop();
648 QVector<double> result;
650 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
660 void Calculator::LessThanOrEqual() {
661 QVector<double> y = Pop();
662 QVector<double> x = Pop();
663 QVector<double> result;
665 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
675 void Calculator::NotEqual() {
676 QVector<double> y = Pop();
677 QVector<double> x = Pop();
678 QVector<double> result;
680 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
692 void Calculator::And() {
693 QVector<double> y = Pop();
694 QVector<double> x = Pop();
695 QVector<double> result;
697 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
706 void Calculator::Or() {
707 QVector<double> y = Pop();
708 QVector<double> x = Pop();
709 QVector<double> result;
711 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
720 void Calculator::Sine() {
721 QVector<double> result = Pop();
722 PerformOperation(result, result.begin(), result.end(), sin);
730 void Calculator::Cosine() {
731 QVector<double> result = Pop();
732 PerformOperation(result, result.begin(), result.end(), cos);
740 void Calculator::Tangent() {
741 QVector<double> result = Pop();
742 PerformOperation(result, result.begin(), result.end(), tan);
750 void Calculator::Cosecant() {
751 QVector<double> result = Pop();
760 void Calculator::Secant() {
761 QVector<double> result = Pop();
762 PerformOperation(result, result.begin(), result.end(),
SecantOperator);
770 void Calculator::Cotangent() {
771 QVector<double> result = Pop();
780 void Calculator::Arcsine() {
781 QVector<double> result = Pop();
782 PerformOperation(result, result.begin(), result.end(), asin);
790 void Calculator::Arccosine() {
791 QVector<double> result = Pop();
792 PerformOperation(result, result.begin(), result.end(), acos);
800 void Calculator::Arctangent() {
801 QVector<double> result = Pop();
802 PerformOperation(result, result.begin(), result.end(), atan);
810 void Calculator::ArcsineH() {
811 QVector<double> result = Pop();
812 PerformOperation(result, result.begin(), result.end(), asinh);
820 void Calculator::ArccosineH() {
821 QVector<double> result = Pop();
822 PerformOperation(result, result.begin(), result.end(), acosh);
830 void Calculator::ArctangentH() {
831 QVector<double> result = Pop();
832 PerformOperation(result, result.begin(), result.end(), atanh);
840 void Calculator::Arctangent2() {
841 QVector<double> y = Pop();
842 QVector<double> x = Pop();
843 QVector<double> result;
845 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(), atan2);
853 void Calculator::SineH() {
854 QVector<double> result = Pop();
855 PerformOperation(result, result.begin(), result.end(), sinh);
863 void Calculator::CosineH() {
864 QVector<double> result = Pop();
865 PerformOperation(result, result.begin(), result.end(), cosh);
873 void Calculator::TangentH() {
874 QVector<double> result = Pop();
875 PerformOperation(result, result.begin(), result.end(), tanh);
883 int Calculator::StackSize() {
884 return p_valStack->size();
892 void Calculator::Push(QVector<double> &vect) {
893 p_valStack->push(vect);
902 void Calculator::Push(
double scalar) {
915 QVector<double> b(buff.
size());
917 for(
int i = 0; i < buff.
size(); i++) {
958 QVector<double> Calculator::Pop(
bool keepSpecials) {
961 if(p_valStack->empty()) {
962 IString msg =
"Math calculator stack is empty, cannot perform any "
967 top = p_valStack->top();
970 for(
int i = 0; i < (int)top.size(); i++) {
975 else if(top[i] > DBL_MAX) {
979 else if(top[i] < -DBL_MAX) {
998 void Calculator::PrintTop() {
999 if(p_valStack->empty())
return;
1002 QVector<double> top = p_valStack->top();
1003 for(
int i = 0; i < (int)top.size(); i++) {
1004 std::cout << top[i] <<
" ";
1006 std::cout <<
"]" << std::endl;
1015 bool Calculator::Empty() {
1016 return p_valStack->empty();
1023 void Calculator::Clear() {
1024 while(!p_valStack->empty()) {
1039 void Calculator::PerformOperation(QVector<double> &results,
1040 QVector<double>::iterator arg1Start,
1041 QVector<double>::iterator arg1End,
1042 double operation(
double)) {
1043 results.resize(arg1End - arg1Start);
1045 for(
int pos = 0; pos < results.size(); pos++) {
1046 results[pos] = operation(*arg1Start);
1068 void Calculator::PerformOperation(QVector<double> &results,
1069 QVector<double>::iterator arg1Start,
1070 QVector<double>::iterator arg1End,
1071 QVector<double>::iterator arg2Start,
1072 QVector<double>::iterator arg2End,
1073 double operation(
double,
double)) {
1074 if(arg1End - arg1Start != 1 && arg2End - arg2Start != 1 &&
1075 arg1End - arg1Start != arg2End - arg2Start) {
1076 IString msg =
"The stack based calculator cannot operate on vectors "
1077 "of differing sizes.";
1081 int iSize = max(arg1End - arg1Start, arg2End - arg2Start);
1082 results.resize(iSize);
1084 for(
int pos = 0; pos < results.size(); pos++) {
1085 results[pos] = operation(*arg1Start, *arg2Start);
1087 if(arg1Start + 1 != arg1End) arg1Start++;
1088 if(arg2Start + 1 != arg2End) arg2Start++;