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

DecimalPrefix * Calculator::getBestDecimalPrefix ( const Number exp10,
const Number exp,
bool  all_prefixes = true 
) const

Returns the best suited decimal prefix for a value.

Parameters:
exp10 Base-10 exponent of the value.
exp The exponent of the unit.
all_prefixes If false, prefixes which is not a multiple of thousand (centi, deci, deka, hekto) will be skipped.
Returns:
A prefix or NULL if the unit should be left without prefix.

Definition at line 706 of file Calculator.cc.

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

                                                                                                               {
      if(decimal_prefixes.size() <= 0 || exp10.isZero()) return NULL;
      int i = 0;
      ComparisonResult c;
      if(exp.isNegative()) {
            i = decimal_prefixes.size() - 1;
      }
      DecimalPrefix *p = NULL, *p_prev = NULL;
      Number exp10_1, exp10_2;
      while((exp.isNegative() && i >= 0) || (!exp.isNegative() && i < (int) decimal_prefixes.size())) {
            if(all_prefixes || decimal_prefixes[i]->exponent() % 3 == 0) {
                  p = decimal_prefixes[i];
                  if(p_prev && p_prev->exponent() >= 0 != p->exponent() >= 0 && p_prev->exponent() != 0) {
                        if(exp.isNegative()) {
                              i++;
                        } else {
                              i--;
                        }
                        p = decimal_null_prefix;
                  }
                  c = exp10.compare(p->exponent(exp));
                  if(c == COMPARISON_RESULT_EQUAL) {
                        if(p == decimal_null_prefix) return NULL;
                        return p;
                  } else if(c == COMPARISON_RESULT_GREATER) {
                        if(i == 0) {
                              if(p == decimal_null_prefix) return NULL;
                              return p;
                        }
                        exp10_1 = exp10;
                        if(p_prev) {
                              exp10_1 -= p_prev->exponent(exp);
                        }
                        exp10_2 = p->exponent(exp);
                        exp10_2 -= exp10;
                        exp10_2 *= 2;
                        exp10_2 += 2;
                        if(exp10_1.isLessThan(exp10_2)) {
                              if(p_prev == decimal_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