Hi all,

I was wondering if there is any standard way in C++ (perhaps in the standard library) of handling mathematical equations that are composed of +/- signs.

For instance, if you are given the equations:

y = +/- sqrt(x)
z = +/- y

Is there an efficient way to deal with equations?

I've tried first naively to program all the possible cases, but the problem stems from the fact that if you have a series of these kinds of equations that all depend on each other, the numbers of equations grows to a point at which it's not really sensible to work out all the cases explicitly. By this I mean coding:

yPlus = sqrt(x);
yMinus = -sqrt(x);
zPlusyPlus = sqrt(x);
zPlusyMinus = -sqrt(x);
zMinusyPlus = -sqrt(x);
zMinusyMinus = sqrt(x);

Then I tried to program it with vectors/arrays such that:

y.push_back( sqrt(x) ); 
y.push_back( -sqrt(x) );
for ( unsigned int i=0; i<y.size(); i++)
{
    z.push_back( y[i] );
    z.push_back( -y[i] );
}

to result in all the possible combinations.

I am not sure if this is an efficient way of coding however and was wondering if there is maybe a well-established way of dealing with equations that take a piecewise character like the ones given here. I've given the example here in terms of the sqrt(); function, but it equally applies to the trigonometric functions i.e., sin(), cos().

Anyway input would be greatly appreciated.

Thanks,

Kartik

I dont know about the - signs right infront of the sqrt, but when I usually deal with these mathematical operations dealing with negatives, I do some along the lines of :

(something that should be negative here) * -1

so in your case:

sqrt(x) * -1;

That is just my opinion, I have never actually tried putting a negative sign INFRONT of the sqrt before. Your choice in the end, whatever works :D

Comments
I suggest you read up on how c++ expressions are parsed. This is extremely basic and fundamental to any mathematical grammar
int sign = 1;
for ( .... ) {
  // do something
  sign = -sign;
}

Each time around the loop, sign alternates 1,-1,1,-1
Just multiply whatever your term is by sign, and you're done.

Can you give a specific example of the equation you have in mind.

Actually the sqrt function is an example. I actually have the sqrt function where I have to test +/- signs and also a sine function for which I have to test +/- signs. These functions are in series, i.e., one of them uses the other, thus when I propagate all the possible combinations, with a few other simple equations between, I end up with 16 possible combinations.

int sign = 1;
for ( .... ) {
  // do something
  sign = -sign;
}

Each time around the loop, sign alternates 1,-1,1,-1
Just multiply whatever your term is by sign, and you're done.

Yea this is what I was employing actually. Haven't found anything better now. Was just wondering if there was some intrinsic function in the standard library for instance, that is useful for dealing with testing +/- sign combinations for things like the sqrt and trigonometric functions.

Thanks,

Kartik

This article has been dead for over six months. Start a new discussion instead.