当前位置: 首页>>代码示例>>C++>>正文


C++ std::fabs方法代码示例

本文整理汇总了C++中std::fabs方法的典型用法代码示例。如果您正苦于以下问题:C++ std::fabs方法的具体用法?C++ std::fabs怎么用?C++ std::fabs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在std的用法示例。


在下文中一共展示了std::fabs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: R

void dry_revolute_joint_3D::doForce(kte_pass_flag aFlag, const shared_ptr<frame_storage>& aStorage) {
  if((!mEnd) || (!mBase))
    return;
  
  using std::fabs;
  
  if(!mAngle) {
    mBase->Force += mEnd->Force;
    mBase->Torque += mEnd->Torque;
  } else {
    rot_mat_3D<double> R(axis_angle<double>(mAngle->q,mAxis).getRotMat());
    vect<double,3> tmp_f = R * mEnd->Force;
    mBase->Force += tmp_f;
    double tmp_t = mEnd->Torque * mAxis;
    if(fabs(mAngle->q_dot) > mSlipVelocity) 
      mAngle->f += tmp_t - mAngle->q_dot / fabs(mAngle->q_dot) * mSlipCoef * norm_2(tmp_f);
    else {
      mAngle->f += tmp_t - mAngle->q_dot / mSlipVelocity * mStictionCoef * norm_2(tmp_f);
    };
    mBase->Torque += R * ( mEnd->Torque - tmp_t * mAxis );
  };
  
  if((aFlag == store_dynamics) && (aStorage)) {
    if(aStorage->frame_3D_mapping[mEnd]) {
      aStorage->frame_3D_mapping[mEnd]->Force = mEnd->Force;
      aStorage->frame_3D_mapping[mEnd]->Torque = mEnd->Torque;
    };
  };
};
开发者ID:mikael-s-persson,项目名称:ReaK,代码行数:29,代码来源:dry_revolute_joint.cpp

示例2: DoubleVector

//Does the actual calling of Runge Kutta: default precision is TOLERANCE defined in
//def.h
//Returns >0 if there's a problem with the running
int RGE::callRK(double x1, double x2, DoubleVector & v,
		DoubleVector (*derivs)(double, const DoubleVector &), 
		double eps) {
  using std::fabs;
  using std::log;

  double tol;
  if (eps < 0.0) tol = TOLERANCE;
  else if (eps < EPSTOL) tol = EPSTOL;
  else tol = eps;
  // x1 == x2 with high precision
  if (close(fabs(x1), fabs(x2), EPSTOL)) return 0;

  // RGE in terms of natural log of renormalisation scale
  double from = log(fabs(x1));
  double to = log(fabs(x2));

  double guess = (from - to) * 0.1; //first step size
  double hmin = (from - to) * tol * 1.0e-5; 

  int err =
    integrateOdes(v, from, to, tol, guess, hmin, derivs, odeStepper);
  
  setMu(x2);
  return err;
}
开发者ID:restrepo,项目名称:FlexibleSUSY,代码行数:29,代码来源:rge.cpp

示例3: hypot_imp

T hypot_imp(T x, T y, const Policy& pol)
{
   //
   // Normalize x and y, so that both are positive and x >= y:
   //
   using std::fabs; using std::sqrt; // ADL of std names

   x = fabs(x);
   y = fabs(y);

#ifdef BOOST_MSVC
#pragma warning(push) 
#pragma warning(disable: 4127)
#endif
   // special case, see C99 Annex F:
   if(std::numeric_limits<T>::has_infinity
      && ((x == std::numeric_limits<T>::infinity())
      || (y == std::numeric_limits<T>::infinity())))
      return policies::raise_overflow_error<T>("boost::math::hypot<%1%>(%1%,%1%)", 0, pol);
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif

   if(y > x)
      (std::swap)(x, y);

   if(x * tools::epsilon<T>() >= y)
      return x;

   T rat = y / x;
   return x * sqrt(1 + rat*rat);
} // template <class T> T hypot(T x, T y)
开发者ID:imos,项目名称:icfpc2015,代码行数:32,代码来源:hypot.hpp

示例4: TEST

TEST(AgradFwdFabs,FvarFvarDouble) {
  using stan::agrad::fvar;
  using std::fabs;

  fvar<fvar<double> > x;
  x.val_.val_ = 1.5;
  x.val_.d_ = 2.0;

  fvar<fvar<double> > a = fabs(x);

  EXPECT_FLOAT_EQ(fabs(1.5), a.val_.val_);
  EXPECT_FLOAT_EQ(2.0, a.val_.d_);
  EXPECT_FLOAT_EQ(0, a.d_.val_);
  EXPECT_FLOAT_EQ(0, a.d_.d_);

  fvar<fvar<double> > y;
  y.val_.val_ = 1.5;
  y.d_.val_ = 2.0;  

  a = fabs(y);
  EXPECT_FLOAT_EQ(fabs(1.5), a.val_.val_);
  EXPECT_FLOAT_EQ(0, a.val_.d_);
  EXPECT_FLOAT_EQ(2.0, a.d_.val_);
  EXPECT_FLOAT_EQ(0, a.d_.d_);
}
开发者ID:javaosos,项目名称:stan,代码行数:25,代码来源:fabs_test.cpp

示例5: closestValue

 int closestValue(TreeNode* root, double target) {
     stack<TreeNode *> st;
     TreeNode *p = root;
     int res = root->val;
     int val;
     while (true) {
         while (p != NULL) {
             st.push(p);
             p = p->left;
         }
         if (st.empty()) {
             break;
         }
         
         p = st.top()->right;
         val = st.top()->val;
         st.pop();
         
         if (fabs(val - target) < fabs(res - target)) {
             res = val;
         }
         if (val >= target) {
             break;
         }
     }
     while (!st.empty()) {
         st.pop();
     }
     return res;
 }
开发者ID:Septemberchar,项目名称:leetcode-2,代码行数:30,代码来源:closest-binary-search-tree-value_1_AC.cpp

示例6: fabs

void dry_revolute_joint_2D::doForce(kte_pass_flag aFlag, const shared_ptr<frame_storage>& aStorage) {
  if((!mEnd) || (!mBase))
    return;
  
  using std::fabs;
  
  if(!mAngle) {
    mBase->Force += mEnd->Force;
    mBase->Torque += mEnd->Torque;
  } else {
    vect<double,2> tmp_f = rot_mat_2D<double>(mAngle->q) * mEnd->Force;
    mBase->Force += tmp_f;
    if(fabs(mAngle->q_dot) > mSlipVelocity) 
      mAngle->f += mEnd->Torque - mAngle->q_dot / fabs(mAngle->q_dot) * mSlipCoef * norm_2(tmp_f);
    else {
      mAngle->f += mEnd->Torque - mAngle->q_dot / mSlipVelocity * mStictionCoef * norm_2(tmp_f);
    };
  };
  
  if((aFlag == store_dynamics) && (aStorage)) {
    if(aStorage->frame_2D_mapping[mEnd]) {
      aStorage->frame_2D_mapping[mEnd]->Force = mEnd->Force;
      aStorage->frame_2D_mapping[mEnd]->Torque = mEnd->Torque;
    };
  };  
};
开发者ID:mikael-s-persson,项目名称:ReaK,代码行数:26,代码来源:dry_revolute_joint.cpp

示例7: isOblique

bool ViewInfo::isOblique() {
    int i;
    double d = 0.0;
    for (i=0; i<3; i++) {
	d += (fabs(m2p[0][i]*m2p[1][i]) + fabs(m2p[0][i]*m2p[2][i]) + fabs(m2p[1][i]*m2p[2][i]));
    }
    if(fabs(d) < 1.0e-4) return false;
    else return true;
}
开发者ID:DanIverson,项目名称:OpenVnmrJ,代码行数:9,代码来源:aipViewInfo.C

示例8: update_location

// If the destination is within one delta step away, the object has arrived.
// Set the location to the destination, stop, and return true.
// Otherwise, add the delta to the location, and return false.
bool Moving_object::update_location()
{
	Cartesian_vector diff = destination - location;
	if ((fabs(diff.delta_x) <= fabs(delta.delta_x)) && (fabs(diff.delta_y) <= fabs(delta.delta_y))) {
		location = destination;
		stop_moving();
		return true;
		}
	location = location + delta;
	return false;
}
开发者ID:robkeim,项目名称:random,代码行数:14,代码来源:Moving_object.cpp

示例9: FindMaxInCol

//member function to find maximum absolute value in a given column and range within that column and return the corresponding row indice
int squareMatrix::FindMaxInCol(const int &c, const int &start, const int &end)const{
  double maxVal = 0.0;
  int maxRow = 0;
  for( int ii = start; ii < end+1; ii++ ){
    if( fabs((*this).Data(ii,c)) > maxVal ){
      maxVal = fabs((*this).Data(ii,c));
      maxRow = ii;
    }
  }
  return maxRow;
}
开发者ID:Nasrollah,项目名称:cfd3d,代码行数:12,代码来源:matrix.cpp

示例10: test_grad

void test_grad(const F& fun,
               const std::vector<double>& args) {
  using std::fabs;
  std::vector<double> diffs_finite = finite_diffs(fun,args);
  std::vector<double> diffs_var = grad(fun,args);
  EXPECT_EQ(diffs_finite.size(), diffs_var.size());
  for (size_t i = 0; i < args.size(); ++i) {
    double tolerance = 1e-6 * fmax(fabs(diffs_finite[i]), fabs(diffs_var[i])) + 1e-14;
    EXPECT_NEAR(diffs_finite[i], diffs_var[i], tolerance);
  }
}
开发者ID:frenchjl,项目名称:stan,代码行数:11,代码来源:test_gradients.hpp

示例11: fixPt

// get consistent clipping on different platforms by making line
// edges meet clipping box if close
void _Clipper::fixPt(QPointF& pt) const
{
  if( fabs(pt.x() - clip.left()) < 1e-4 )
    pt.setX(clip.left());
  if( fabs(pt.x() - clip.right()) < 1e-4 )
    pt.setX(clip.right());
  if( fabs(pt.y() - clip.top()) < 1e-4 )
    pt.setY(clip.top());
  if( fabs(pt.y() - clip.bottom()) < 1e-4 )
    pt.setY(clip.bottom());
}
开发者ID:jeremysanders,项目名称:veusz,代码行数:13,代码来源:polylineclip.cpp

示例12: zciisconsistency

void CPISwapTest::zciisconsistency() {
    CommonVars common;

    ZeroCouponInflationSwap::Type ztype = ZeroCouponInflationSwap::Payer;
    Real  nominal = 1000000.0;
    Date startDate(common.evaluationDate);
    Date endDate(25, November, 2059);
    Calendar cal = UnitedKingdom();
    BusinessDayConvention paymentConvention = ModifiedFollowing;
    DayCounter dummyDC, dc = ActualActual();
    Period observationLag(2,Months);

    Rate quote = 0.03714;
    ZeroCouponInflationSwap zciis(ztype, nominal, startDate, endDate, cal,
                                  paymentConvention, dc, quote, common.ii,
                                  observationLag);

    // simple structure so simple pricing engine - most work done by index
    ext::shared_ptr<DiscountingSwapEngine>
    dse(new DiscountingSwapEngine(common.nominalTS));

    zciis.setPricingEngine(dse);
    QL_REQUIRE(fabs(zciis.NPV())<1e-3,"zciis does not reprice to zero");

    std::vector<Date> oneDate;
    oneDate.push_back(endDate);
    Schedule schOneDate(oneDate, cal, paymentConvention);

    CPISwap::Type stype = CPISwap::Payer;
    Real inflationNominal = nominal;
    Real floatNominal = inflationNominal * std::pow(1.0+quote,50);
    bool subtractInflationNominal = true;
    Real dummySpread=0.0, dummyFixedRate=0.0;
    Natural fixingDays = 0;
    Date baseDate = startDate - observationLag;
    Real baseCPI = common.ii->fixing(baseDate);

    ext::shared_ptr<IborIndex> dummyFloatIndex;

    CPISwap cS(stype, floatNominal, subtractInflationNominal, dummySpread, dummyDC, schOneDate,
               paymentConvention, fixingDays, dummyFloatIndex,
               dummyFixedRate, baseCPI, dummyDC, schOneDate, paymentConvention, observationLag,
               common.ii, CPI::AsIndex, inflationNominal);

    cS.setPricingEngine(dse);
    QL_REQUIRE(fabs(cS.NPV())<1e-3,"CPISwap as ZCIIS does not reprice to zero");

    for (Size i=0; i<2; i++) {
        QL_REQUIRE(fabs(cS.legNPV(i)-zciis.legNPV(i))<1e-3,"zciis leg does not equal CPISwap leg");
    }
    // remove circular refernce
    common.hcpi.linkTo(ext::shared_ptr<ZeroInflationTermStructure>());
}
开发者ID:SePTimO7,项目名称:QuantLib,代码行数:53,代码来源:inflationcpiswap.cpp

示例13: testEquals

    bool TesterDouble::testEquals( double actualValue, double expectedValue,
            const char * lineText, int lineNumber ) const
    {
        if( fabs(actualValue - expectedValue) < epsilon )
            return true;

        printf( "%s\nat line %i - "
                "Actual: %le - Expected: %le\n"
                "The value is within %le of expected. Epsilon is %le\n\n",
                lineText, lineNumber,
                actualValue, expectedValue,
                fabs(actualValue - expectedValue), epsilon);
        return false;
    }
开发者ID:brunosouzasilva,项目名称:INE5420,代码行数:14,代码来源:testerDouble.cpp

示例14: test_grad_multi_student_t

void test_grad_multi_student_t(const F& fun,
               const std::vector<T_y> & vec_y,
               const std::vector<T_mu> & vec_mu,
               const std::vector<T_sigma> & vec_sigma,
               const T_nu & nu) {
  using std::fabs;
  std::vector<double> diffs_finite = finite_diffs_multi_normal(fun,vec_y,vec_mu,vec_sigma,nu);
  std::vector<double> diffs_var = grad_multi_normal(fun,vec_y,vec_mu,vec_sigma,nu);
  EXPECT_EQ(diffs_finite.size(), diffs_var.size());
  for (size_t i = 0; i < diffs_finite.size(); ++i) {
    double tolerance = 1e-6 * fmax(fabs(diffs_finite[i]), fabs(diffs_var[i])) + 1e-14;
    EXPECT_NEAR(diffs_finite[i], diffs_var[i], tolerance);
  }
}
开发者ID:javaosos,项目名称:stan,代码行数:14,代码来源:test_gradients_multi_student_t.hpp

示例15: broyden_good_method

void broyden_good_method(const Vector& x_prev, Vector& x0, RootedFunction f, const T& tol = std::numeric_limits<T>::epsilon(), std::size_t max_iter = 50) 
{
  using std::fabs;
  typedef typename vect_traits<Vector>::value_type ValueType;
  typedef typename vect_traits<Vector>::size_type SizeType;
  
  Vector dx = x0 - x_prev;
  Vector y0 = f(x0);
  Vector dy = y0 - f(x_prev);
  std::size_t iter = 0;
  
  mat<ValueType, mat_structure::square> J_inv = mat<ValueType, mat_structure::identity>(dx.size());
  Vector Jdy = dy;
  ValueType denom = dx * dy;
  if( fabs(denom) < tol )
    throw singularity_error("Broyden's good method failed due to a stationary point!");
  Vector dxJ = dx;
  for(SizeType i = 0; i < dx.size(); ++i)
    for(SizeType j = 0; j < dx.size(); ++j)
      J_inv(i,j) += (dx[i] - dy[i]) * dx[j] / denom;
  
  while(true) {
    
    dx = -J_inv * y0;
    x0 += dx;
    
    if(norm_2(dx) < tol)
      return;
  
    if( ++iter > max_iter ) 
      throw maximum_iteration("Broyden's good method diverged, as detected by reaching the maximum iteration limit!");
  
    dy = f(x0) - y0;
    y0 += dy;
    
    Jdy = J_inv * dy;
    denom = dx * Jdy;
    if( fabs(denom) < tol )
      throw singularity_error("Broyden's good method failed due to a stationary point!");
    dxJ = dx * J_inv;
    
    for(SizeType i = 0; i < dx.size(); ++i)
      for(SizeType j = 0; j < dx.size(); ++j)
        J_inv(i,j) += (dx[i] - Jdy[i]) * dxJ[j] / denom;
    
  };
  
  return x0;
};
开发者ID:ahmadyan,项目名称:ReaK,代码行数:49,代码来源:broyden_method.hpp


注:本文中的std::fabs方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。