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

MathStructure Calculator::convert ( const MathStructure mstruct,
Unit to_unit,
const EvaluationOptions eo = default_evaluation_options,
bool  always_convert = true 
)

Converts to a unit. The converted value is evaluated.

Parameters:
mstruct The value to convert.
composite_ Unit to convert to.
eo Evaluation options.
always_convert ...
Returns:
Converted value.

Definition at line 2248 of file Calculator.cc.

References MathStructure::base(), MathStructure::childrenUpdated(), CompositeUnit::containsRelativeTo(), convert(), MathStructure::convert(), MathStructure::countChildren(), MathStructure::divide(), MathStructure::eval(), MathStructure::getChild(), MathStructure::isAddition(), MathStructure::isOne(), MathStructure::isPower(), MathStructure::isUnit(), EvaluationOptions::keep_prefixes, MathStructure::multiply(), MathStructure::set(), MathStructure::size(), Unit::subtype(), SUBTYPE_ALIAS_UNIT, SUBTYPE_COMPOSITE_UNIT, EvaluationOptions::sync_units, MathStructure::type(), and MathStructure::unit().

                                                                                                                               {
      if(to_unit->subtype() == SUBTYPE_COMPOSITE_UNIT) return convertToCompositeUnit(mstruct, (CompositeUnit*) to_unit, eo, always_convert);
      if(to_unit->subtype() != SUBTYPE_ALIAS_UNIT || (((AliasUnit*) to_unit)->baseUnit()->subtype() != SUBTYPE_COMPOSITE_UNIT && ((AliasUnit*) to_unit)->baseExponent() == 1)) {
            MathStructure mstruct_new(mstruct);
            if(!mstruct_new.convert(to_unit, true)) {
                  mstruct_new = mstruct;
            } else {
                  mstruct_new.eval(eo);
                  return mstruct_new;
            }
      }
      MathStructure mstruct_new(mstruct);
      if(mstruct_new.isAddition()) {
            for(size_t i = 0; i < mstruct_new.size(); i++) {
                  mstruct_new[i] = convert(mstruct_new[i], to_unit, eo, false);
            }
            mstruct_new.childrenUpdated();
            EvaluationOptions eo2 = eo;
            //eo2.calculate_functions = false;
            eo2.sync_units = false;
            eo2.keep_prefixes = true;
            mstruct_new.eval(eo2);
      } else {
            bool b = false;
            if(mstruct_new.convert(to_unit) || always_convert) {
                  b = true;
            } else if(to_unit->subtype() == SUBTYPE_ALIAS_UNIT && ((AliasUnit*) to_unit)->baseUnit()->subtype() == SUBTYPE_COMPOSITE_UNIT) {
                  CompositeUnit *cu = (CompositeUnit*) ((AliasUnit*) to_unit)->baseUnit();
                  switch(mstruct.type()) {
                        case STRUCT_UNIT: {
                              if(cu->containsRelativeTo(mstruct_new.unit())) {
                                    b = true;
                              }
                              break;
                        } 
                        case STRUCT_MULTIPLICATION: {
                              for(size_t i = 1; i <= mstruct_new.countChildren(); i++) {
                                    if(mstruct_new.getChild(i)->isUnit() && cu->containsRelativeTo(mstruct_new.getChild(i)->unit())) {
                                          b = true;
                                          break;
                                    }
                                    if(mstruct_new.getChild(i)->isPower() && mstruct_new.getChild(i)->base()->isUnit() && cu->containsRelativeTo(mstruct_new.getChild(i)->base()->unit())) {
                                          b = true;
                                          break;
                                    }
                              }
                              break;
                        }
                        case STRUCT_POWER: {                      
                              if(mstruct_new.base()->isUnit() && cu->containsRelativeTo(mstruct_new.base()->unit())) {
                                    b = true;
                              }
                              break;
                        }
                        default: {}
                  }
            }
            if(b) {
                  mstruct_new.divide(to_unit);
                  EvaluationOptions eo2 = eo;
                  //eo2.calculate_functions = false;
                  eo2.sync_units = true;
                  eo2.keep_prefixes = false;
                  mstruct_new.eval(eo2);
                  if(mstruct_new.isOne()) mstruct_new.set(to_unit);
                  else mstruct_new.multiply(to_unit, true);
                  eo2.sync_units = false;
                  eo2.keep_prefixes = true;
                  mstruct_new.eval(eo2);
            }
      }
      return mstruct_new;
}


Generated by  Doxygen 1.6.0   Back to index