Math opposite sign function? [closed] - math

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
Does such function exist? I created my own but would like to use an official one:
private function opposite(number:Number):Number
{
if (number < 0)
{
number = Math.abs(number);
}
else
{
number = -(number);
}
return number;
}
So, -5 becomes 5 and 3 becomes -3.
Edit:
Forgive me for being stupid. I'm human. :)

yes it does...
return num*-1;
or simply
return -num;

How about:
return -(number)
as -(-5) == 5.

Simply putting a negative sign in front of the variable or number will do the trick, even if it's already negative:
-(-5) => 5
$foo = 3; -$foo => -3

This a trick question? why a function? just do number * -1, multiply with -1 that is.

try something like number = number * (-1)

You could use
number *= -1;
3 becomes -3 and -5 becomes 5 :)

Related

No need to Round off [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
How can I get??
If my answer is 1.1
then output should be 1.1 (Actual)
My code :-
decimal totalUnusedAmount = 0;
decimal.TryParse(txtTotalUnusedAmount.Text, out totalUnusedAmount);
lblAmountDue.Text = ((totalUnusedAmount * Convert.ToDecimal(ViewState["FundedAmount"])) / 100).ToString();
but from the above code, getting answer 1.10. When I debug the code, at the time of debugging it gives 1.1.
Try this
lblAmountDue.Text = ((totalUnusedAmount * Convert.ToDecimal(ViewState["FundedAmount"])) / 100).ToString("G29");`
Just note that this displays in powers of e if figures are too long.
Edit
Works for me
decimal tmp = 1.2000000000000M;
Response.Write(tmp.ToString("G29"));
You just need to specify a format to display the number in:
lblAmountDue.Text = ((totalUnusedAmount * Convert.ToDecimal(ViewState["FundedAmount"])) / 100).ToString("0.0");
Docs: http://msdn.microsoft.com/en-us/library/system.string.format.aspx
If you are encountering a rounding error, you'll need to use a decimal literal for your 100. Try this:
lblAmountDue.Text = ((totalUnusedAmount * Convert.ToDecimal(ViewState["FundedAmount"])) / 100M).ToString("0.0");
The M on the end of the number tells the compiler it should be interpreted as a decimal not an Int32 which is the default literal type for numbers with no decimal places.
Docs: http://msdn.microsoft.com/en-us/library/364x0z75(v=vs.110).aspx
To display as the exact answer how you see it in the debugger, you want the "Round-trip" option:
lblAmountDue.Text = ((totalUnusedAmount * Convert.ToDecimal(ViewState["FundedAmount"])) / 100M).ToString("R");
Docs: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx (at the bottom of the table)
I tried this....its working
lblAmountDue.Text = ((totalUnusedAmount * Convert.ToDecimal(ViewState["FundedAmount"])) / 100).ToString("0.##");
http://www.csharp-examples.net/string-format-double/

Order of operation for subtraction? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
This may be an uneducated question - or an odd one of the kind.
My question is why this code doesn't work:
if (up == true)
{
SDL_Delay(pause_t);
player.setY(player.velocity - player.getY());
}
if (left == true)
{
SDL_Delay(pause_t);
player.setX(player.velocity - player.getX());
}
However, this code does work:
if (up == true)
{
SDL_Delay(pause_t);
player.setY(player.getY() - player.velocity);
}
if (left == true)
{
SDL_Delay(pause_t);
player.setX(player.getX() - player.velocity);
}
The difference between the two codes is that in the first example, I am subtracting a part firstly (the velocity of the object) before calling the position function.
What happens when I run this code is that it doesn't move 1 on the axis, but rather 10.
The size of the Y axis is above 10, so I am not actually subtracting its own coordinates.
However, in the second piece of code, I am subtracting the velocity in the end, after having called my position function.
What happens with the second piece of code is that the object moves one pixel (velocity's integer value).
To those who should wonder what I am trying to do, these are controls for my openGL game. It's for moving an object.
And for the record, this is not an important question. My code does work. I am simply curious why it works the way it does.
in the first example, I am subtracting a part firstly (the velocity of the object) before calling the position function.
No, in the first version, you're subtracting the old location, not subtracting the velocity.
The code for what you describe is:
player.setY((-player.velocity) + player.getY());
and that will work.

Understanding the recursion here [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm looking at a recursive problem where a child can hop a staircase of steps n in 1,2 or 3 steps at one time.
The code I'm looking at is a function similar to the likes of fibonacci. However, what I don't get is if n==0 why does it return 1. If the total number of steps are 0, shouldn't there be zero ways of climbing it ? Why is there one way to climb it ?
int f(int n)
{
if(n<0)
return 0;
else if(n==0)
return 1;
else
return f(n-1) + f(n-2) + f(n-3);
}
This is more of a logic question. Suppose you stand there and do nothing. How many steps did you climb? The answer is zero. So, did you successfully climb zero steps? Yes.
How many ways are there to climb zero stairs? Just one way: you have to stand there and not climb any steps.
That isn't really a valid question.
Because this is a recursive implementation, you'll always have to provide a boundary case for f(nmin) where nmin is 1 less than the lowest valid n.
So the case n = 0 is a boundary condition that serves to ensure the correct result for all values where n > 0.
In other words, it (probably) doesn't mean anything, or, it probably means something different to what you think it means. All it has to do is ensure a correct result for f(1).
No, there is not 0 ways to go up 0 stairs, in the same way that 0/0 does not equal 0. It's an indeterminate result.

What does the ? mean in this equation? (x ? y) % 255 [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
does anyone with a maths background know what is meant by the question mark in the following equation, as found in the PDF here: http://ivizlab.sfu.ca/media/GenProg2009.pdf ?
(x ? y) % 255
Obviously % 255 is the modulus function but I can't think what they mean by the question mark. All my experience of programming tells me that ? is conditional such as x > y ? 1 : 2 but I don't think that is the case here.
I am implementing a set of graphical functions that generate random images and the functions are called for each x and y coord in the image.
Other functions listed in the source document use the same notation such as:
(x ? y) / 2
Any ideas? thanks
Executive summary: The paper you link to ("DiPaola & Gabora") has has some formatting problems, which prevent comprehension. You should search for actual code associated with the paper, or contact the authors.
Note that on page 4, they say
Our work is based on Ashmore and Miller’s [2] CGP application
Chasing reference 2 leads eventually to this paper: http://www.emoware.org/work/ashmore-miller-evomusart2004.zip (zip containing PDF) ("Ashmore & Miller"), in which a similar set of functions are defined, however in this second paper, it can be seen that the PDF of DiPaola & Gabora has lost superscript formatting: where it has
13: abs (sqrt (x - param2 ? y - param2) % 255);
it should actually have
13: abs (sqrt (x - param2 ? y - param2) % 255);
(note, param squared rather than param2)
Simlarly, where Ashmore & Miller has
3: (input1 * input2) % 255;
4: (input1 + input2) % 255;
5: if(input1>input2) input1 - input2; else input2 - input1;
DiPaola & Gabora ends up with
3: (x ? y) % 255;
4: if (x[y) x - y; else y - x;
It seems clear to me that some non-ASCII symbol was intended where the ? appears. This may have been a unicode PLUS or TIMES, but I can't be sure. Also note that the > from the original has become a [, which is nonsense.
In the face of these formatting errors, the thing to do is either try a few things and see what works; search for reference source code by the authors; or (as a last resort) contact DiPaola & Gabora and ask them what was intended.

Programming Theory - Proving Invariants [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have defined this pseudo-code for recognising strings using a relaxed-trie where the function Next[u,x] gives the set of nodes with u-edges from u ( basically the set of nodes such that (u,x,v) is an edge in T).
Here it is:
U := {1};
while s ≠ λ and U ≠ Ø do
U:= U in u Union Next [u, head(s)];
s:= tail(s)
od;
if U ≠ Ø then
if Final[u] for some u in U then
Accept
else reject
fi
else
reject
fi
Basically I have defined a postcondition for the loop, and given a loop invariant ( I think I have these elements covered, but if you think it will help to explain it go for it).
So I need to give a short argument stating why the invariant is invariant, (ie how it is preserved by the loop body, when the loop condition holds).
I then need to extend this pseudocode such that it can move to a new node without advancing the input :
(I think I would do this by adding another array (say Null) where Null[u] is the set of states it can move to from u without advancing the input)
It should also be changed such that each iteration before looking at the input all states can be reached from a state in U without advancing the input.
Thanks for all your help, am finding these two steps quite difficult, but think my psuedo-code for the first part is fine

Resources