當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。