当前位置: 首页>>代码示例>>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;未经允许,请勿转载。