Logo Search packages:      
Sourcecode: libqalculate version File versions  Download package

BinaryPrefix * Calculator::getBestBinaryPrefix ( const Number exp2,
const Number exp 
) const

Returns the best suited binary prefix for a value.

Parameters:
exp10 Base-2 exponent of the value.
exp The exponent of the unit.
Returns:
A prefix or NULL if the unit should be left without prefix.

Definition at line 836 of file Calculator.cc.

References Number::compare(), BinaryPrefix::exponent(), Number::isLessThan(), Number::isNegative(), and Number::isZero().

                                                                                         {
      if(binary_prefixes.size() <= 0 || exp2.isZero()) return NULL;
      int i = 0;
      ComparisonResult c;
      if(exp.isNegative()) {
            i = binary_prefixes.size() - 1;
      }
      BinaryPrefix *p = NULL, *p_prev = NULL;
      Number exp2_1, exp2_2;
      while((exp.isNegative() && i >= 0) || (!exp.isNegative() && i < (int) binary_prefixes.size())) {
            p = binary_prefixes[i];
            if(p_prev && p_prev->exponent() >= 0 != p->exponent() >= 0 && p_prev->exponent() != 0) {
                  if(exp.isNegative()) {
                        i++;
                  } else {
                        i--;
                  }
                  p = binary_null_prefix;
            }
            c = exp2.compare(p->exponent(exp));
            if(c == COMPARISON_RESULT_EQUAL) {
                  if(p == binary_null_prefix) return NULL;
                  return p;
            } else if(c == COMPARISON_RESULT_GREATER) {
                  if(i == 0) {
                        if(p == binary_null_prefix) return NULL;
                        return p;
                  }
                  exp2_1 = exp2;
                  if(p_prev) {
                        exp2_1 -= p_prev->exponent(exp);
                  }
                  exp2_2 = p->exponent(exp);
                  exp2_2 -= exp2;
                  exp2_2 *= 2;
                  exp2_2 += 2;
                  if(exp2_1.isLessThan(exp2_2)) {
                        if(p_prev == binary_null_prefix) return NULL;
                        return p_prev;
                  } else {
                        return p;
                  }
            }
            p_prev = p;
            if(exp.isNegative()) {
                  i--;
            } else {
                  i++;
            }
      }
      return p_prev;
}


Generated by  Doxygen 1.6.0   Back to index