當前位置: 首頁>>代碼示例>>C++>>正文


C++ std::abs方法代碼示例

本文整理匯總了C++中std::abs方法的典型用法代碼示例。如果您正苦於以下問題:C++ std::abs方法的具體用法?C++ std::abs怎麽用?C++ std::abs使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在std的用法示例。


在下文中一共展示了std::abs方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。

示例1:

::testing::AssertionResult array_are_close(X const &x1, Y const &y1,double precision = 1.e-10) {
 triqs::arrays::array<typename X::value_type, X::rank> x = x1; 
 triqs::arrays::array<typename X::value_type, X::rank> y = y1; 
 if (x.domain() != y.domain()) 
 return ::testing::AssertionFailure() << "Comparing two arrays of different size "
          << "\n X = "<<  x << "\n Y = "<< y;

 if (max_element(abs(x - y)) < precision)
  return ::testing::AssertionSuccess();
 else
  return ::testing::AssertionFailure() << "max_element(abs(x-y)) = " << max_element(abs(x - y)) << "\n X = "<<  x << "\n Y = "<< y;
}
開發者ID:cyrilmartins,項目名稱:triqs,代碼行數:12,代碼來源:test_tools.hpp

示例2: sizeOfBlackWhiteBlackRun

float Detector::sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY) {
  // Mild variant of Bresenham's algorithm;
  // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  bool steep = abs(toY - fromY) > abs(toX - fromX);
  if (steep) {
    int temp = fromX;
    fromX = fromY;
    fromY = temp;
    temp = toX;
    toX = toY;
    toY = temp;
  }

  int dx = abs(toX - fromX);
  int dy = abs(toY - fromY);
  int error = -dx >> 1;
  int xstep = fromX < toX ? 1 : -1;
  int ystep = fromY < toY ? 1 : -1;

  // In black pixels, looking for white, first or second time.
  int state = 0;
  // Loop up until x == toX, but not beyond
  int xLimit = toX + xstep;
  for (int x = fromX, y = fromY; x != xLimit; x += xstep) {
    int realX = steep ? y : x;
    int realY = steep ? x : y;

    // Does current pixel mean we have moved white to black or vice versa?
    if (!((state == 1) ^ image_->get(realX, realY))) {
      if (state == 2) {
        return MathUtils::distance(x, y, fromX, fromY);
      }
      state++;
    }

    error += dy;
    if (error > 0) {
      if (y == toY) {
        break;
      }
      y += ystep;
      error -= dx;
    }
  }
  // Found black-white-black; give the benefit of the doubt that the next pixel outside the image
  // is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a
  // small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
  if (state == 2) {
    return MathUtils::distance(toX + xstep, toY, fromX, fromY);
  }
  // else we didn't find even black-white-black; no estimate is really possible
  return nan();
}
開發者ID:Brigadier1-9,項目名稱:node-dv,代碼行數:53,代碼來源:4Detector.cpp

示例3: gcd_iterative

int gcd_iterative(int a, int b) {
    a = abs(a);
    b = abs(b);

    while (a && b) {
        if (a > b)
            a -= b;
        else
            b -= a;
    }
    return a+b;  // at least one of these will be 0    
}
開發者ID:venherr,項目名稱:assignment-05,代碼行數:12,代碼來源:solution--benblazak.cpp

示例4: gcd

int gcd(int a, int b) {
    a = abs(a);
    b = abs(b);

    if (a == 0 || b == 0)
        return a+b;

    if (a > b)
        return gcd(a-b, b);

    return gcd(a, b-a);
}
開發者ID:venherr,項目名稱:assignment-05,代碼行數:12,代碼來源:solution--benblazak.cpp

示例5: operator

    void operator()(T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4)
    {
        using std::abs;

        t1 = adapted_pow(abs(t2), -beta1/(m_steps + 1)) *
            adapted_pow(abs(t3), -beta2/(m_steps + 1)) *
            adapted_pow(abs(t4), -beta3/(m_steps + 1)) *
            adapted_pow(abs(dt1/dt2), -alpha1/(m_steps + 1))*
            adapted_pow(abs(dt2/dt3), -alpha2/(m_steps + 1));

        t1 = 1/t1;
    };
開發者ID:ds283,項目名稱:odeint-v2,代碼行數:12,代碼來源:pid_step_adjuster.hpp

示例6: collideByDist

float CollisionDetector::collideByDist(Vector3 object, Vector3 target)
{
	//A
	diffInX = abs(target.x - object.x);

	//B
	diffInZ = abs(target.z - object.z);

	//TOA CAH SOH   A square + B square = C square
	dist = sqrt(diffInX * diffInX + diffInZ * diffInZ);

	return dist;
}
開發者ID:Larkie11,項目名稱:Sp2-16,代碼行數:13,代碼來源:CollisionDetector.cpp

示例7: roughly_equal_to

	static bool roughly_equal_to (const T &lhs, const T &rhs)
	{
		assert(!std::numeric_limits<T>::is_integer);
		using std::max; using std::pow; using std::abs;

		// last two digits of largest number
		const T epsilon = max(abs(lhs), abs(rhs)) * pow(T(2), -T(std::numeric_limits<T>::digits-2));
		assert(epsilon >= T(0));
		assert(abs(lhs) + epsilon >= lhs);
		assert(abs(rhs) + epsilon >= rhs);
		//std::cerr << lhs << " " << rhs << " " << epsilon << " " << std::numeric_limits<T>::epsilon() << std::endl;
		return abs(rhs-lhs) <= epsilon; // "<=", should also work for zero
	}
開發者ID:PokerN,項目名稱:sailfish,代碼行數:13,代碼來源:Functors.hpp

示例8: canMove

bool GameState::canMove(const pos& i_s,const pos& j_s,const pos& i_f,const pos& j_f) const {
	if(!gameStarted) { DEBUG("game hasn't started"); return false; } //are we moving pieces?
	if(i_s < 0 || j_s < 0 || i_s > 7 || j_s > 7 || i_f < 0 || j_f < 0 || i_f > 7 || j_f > 7) { DEBUG("not in board"); return false; } //is this inside the board?
	if(!piece(i_s,j_s)) { DEBUG("no piece"); return false; }
	if((piece(i_s,j_s) & COLOR_MASK) != toMove) { DEBUG("wrong color"); return false; } //is it this person's turn?
	if(piece(i_f,j_f)!=0) { DEBUG("moving to occupied"); return false; } // is there something occupying the spot to move to?
	if(frozen(i_s,j_s)) { DEBUG("frozen"); return false; }
	if(abs(i_f-i_s)+abs(j_f-j_s)!=1) { DEBUG("too far"); return false; }
	if(isPawn(piece(i_s,j_s))) {
		if((i_f-i_s) == (getColor(piece(i_s,j_s)) * 2 - 1)) { DEBUG("pawns don't move back"); return false; } //pawns going in the right direction?
	}
	return true;
}
開發者ID:Databean,項目名稱:gigantoraptor,代碼行數:13,代碼來源:GameState.cpp

示例9: assert

//added by ali 24 Jul 2011
Float analytic_straight_velocity2::operator() (const point & x)
{
        point u;
        assert(x[1]<=1.);
        if (abs(x[1]) <= _ys)
        {
                u[0]=_Bn/(2.*_ys)*sqr(1.-_ys);
        }
        else
        {
                u[0]=_Bn/(2.*_ys)*(sqr(1.-_ys)-sqr(abs(x[1])-_ys));
        }
        return u[0];
}
開發者ID:arousta,項目名稱:RheolefCode,代碼行數:15,代碼來源:analytic_straightchannel.cpp

示例10: abs

/**
 * @brief This defines the functionpart of the whole thing. Needed for rheolef::interpolate
 *
 *	Let \f$ x = (x_0,x_1)\f$	

	\f[
		u(x) =  (u_0(x_1), 0)
	\f]
	\f{eqnarray}
		\begin{cases}
			\frac{f}{2}
			\left[
				|y_w|-\frac{Bn}{|f|}
			\right]^2
			&
			x_1 \in [\pm Bn / |f|] \\
			\frac{f}{2}
			\left[ 
				\left( |y_w| - \frac{Bn}{|f|} \right )^2-
				\left( |y|   - \frac{Bn}{|f|} \right )^2
			\right]
			&
			|x_1| >  Bn / |f|
		\end{cases}
	\f}
 * @param x point position
 * @return analytic velocity
 */
point analytic_straight_velocity ::operator() (const point & x)
{
	point u;
	Float yc = _Bn / abs(_dpdx);
	if (abs(x[1]) <= yc)
	{
		u[0]=(_dpdx/2)*sqr(abs(_ywall)-yc);
	}
	else
	{
		u[0]=(_dpdx/2)*(sqr(abs(_ywall)-yc)-sqr(abs(x[1])-yc));
	}
	return u;
}
開發者ID:arousta,項目名稱:RheolefCode,代碼行數:42,代碼來源:analytic_straightchannel.cpp

示例11: setDestinationBasedOnRiders

void Elevator::setDestinationBasedOnRiders() // reset toFloor based on riders' destinations
{
  if (hasRiders()==true)
  {
    setDestination(&(r[0].getDestination()));
  }
  for (int i=0; i<r.size(); i++) //1) Query each rider in vector
  {
    if (abs(toFloor->getLocation() - location) >
        abs( r[i].getDestination().getLocation() - location))
    {
      setDestination(&(r[i].getDestination()));
    }
  }
}
開發者ID:yutuotuo,項目名稱:OOP-in-CPP,代碼行數:15,代碼來源:Elevator.cpp

示例12: if

size_t
seqElements(const af_seq &seq) {
    size_t out = 0;
    if      (seq.step > DBL_MIN)    {
        out = ((seq.end - seq.begin) / abs(seq.step)) + 1;
    }
    else if (seq.step < -DBL_MIN)   {
        out = ((seq.begin - seq.end) / abs(seq.step)) + 1;
    }
    else                            {
        out = numeric_limits<size_t>::max();
    }

    return out;
}
開發者ID:hxiaox,項目名稱:arrayfire,代碼行數:15,代碼來源:dim4.cpp

示例13:

//#####################################################################
// Function Line_Search_Quadratic_Golden_Section
//#####################################################################
template<class T> bool LINE_SEARCH<T>::
Line_Search_Golden_Section(NONLINEAR_FUNCTION<T(T)>& F,T a,T b,T& x,int max_iterations,T interval_tolerance) const
{
    PHYSBAM_ASSERT(a<=b);
    T tau=(T).5*(sqrt((T)5)-1),m=a+tau*(b-a),t;
    BRACKET s={a,m,b,F(a),F(m),F(b)};
    int i;
    interval_tolerance*=abs(s.a)+abs(s.b);
    for(i=1;i<=max_iterations;i++){
        if(abs(s.b-s.a)<interval_tolerance) break;
        t=s.a+s.b-s.m;
        Update_Interval(s,t,F(t));}
    x=Best_Value(s);
    return i<=max_iterations;
}
開發者ID:acrlakshman,項目名稱:physbam_public,代碼行數:18,代碼來源:LINE_SEARCH.cpp

示例14: columns

vector<uint> matrix_utils::remove_linear_dependence_rows(Matrix &matrix) {
	vector<uint> removed_rows;
	//need to prevent swap whole columns, just swap indexes
	vector<uint> columns(matrix[0].size());
	for (uint i = 0; i != columns.size(); i++) {
		columns[i] = i;
	}

	Matrix temp_matrix;
	temp_matrix.reserve(matrix.size());
	for (uint i = 0; i != matrix.size(); i++) {
		temp_matrix.push_back(vector<double>(matrix[i].begin(), matrix[i].end()));
	}
	for (uint i = 0; i != temp_matrix.size(); i++) {
		uint index_max = i;
		for (uint j = i + 1; j != temp_matrix[0].size(); j++) {
			if (abs(temp_matrix[i][columns[index_max]]) < abs(temp_matrix[i][columns[j]])) {
				index_max = j;
			}
		}
		if (index_max != i) {
			uint tmp = columns[i];
			columns[i] = columns[index_max];
			columns[index_max] = tmp;
		}
		index_max = columns[i];
		if (abs(temp_matrix[i][index_max]) < 1e-7) {
			temp_matrix.erase(temp_matrix.begin() + i);
			matrix.erase(matrix.begin() + i);
			removed_rows.push_back(i);
			i--;
			continue;
		}

		double divisor = temp_matrix[i][index_max];
		for (uint k = 0; k != temp_matrix[0].size(); k++) {
			temp_matrix[i][k] /= divisor;
		}
		for (uint j = i + 1; j != temp_matrix.size(); j++) {
			double multiplier = -temp_matrix[j][index_max];
			for (uint k = 0; k != temp_matrix[0].size(); k++) {
				temp_matrix[j][k] += temp_matrix[i][k] * multiplier;
			}
		}
	}

	return removed_rows;
}
開發者ID:DragoonXen,項目名稱:HMEinit,代碼行數:48,代碼來源:matrix_utils.cpp

示例15: test_cp

int test_cp( const std::string& species_name, unsigned int species, Scalar cp_exact, Scalar T,
	     const Antioch::CEAEvaluator<Scalar>& thermo )
{
  using std::abs;

  int return_flag = 0;

  const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 5;

  typedef typename Antioch::template TempCache<Scalar> Cache;

  const Scalar cp = thermo.cp(Cache(T), species);

  if( abs( (cp_exact - cp)/cp_exact ) > tol )
    {
      std::cerr << "Error: Mismatch in species specific heat."
		<< "\nspecies    = " << species_name
		<< "\ncp         = " << cp
		<< "\ncp_exact   = " << cp_exact
		<< "\ndifference = " << (cp_exact - cp)
		<< "\ntolerance  = " << tol
		<< "\nT = " << T << std::endl;
      return_flag = 1;
    }

  return return_flag;
}
開發者ID:GuillaumeReinisch,項目名稱:antioch,代碼行數:27,代碼來源:cea_evaluator_unit.C


注:本文中的std::abs方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。