No need to Round off [closed] - asp.net

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/

Related

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.

Obfuscate multiplication by 2 [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 11 years ago.
Help me come up with an obfuscated way to multiply a number by 2, accurate to the second decimal.
Ideas:
use the Russian multiplication technique
trig / other mathematical identities
monte carlo methods
but of course bonus points for CS trickery
edit:
Just remembered that it's probably more appropriate to think of this in terms of significant figures, not accurate decimal places. So go for 4 matching leading digits.
The following perl one-liner doubles the first command-line argument:
perl -e '$/=$\=shift;map$\+=$//(++$|+$|)**$_,(++$...$=);print'
You may say that using perl is cheating because everything is obfuscated in perl. You would not be entirely wrong.
Here's a slightly different approach in (unobfuscated) python:
import math
def double(n) :
if n == 0 :
return 0
a = b = n
for i in range(1,100) :
a = 2 + 1.0/a
a = a - 1
for i in range(1,100) :
b = a * b
a = math.sqrt(a)
return b
If the goal is obfuscation for the sake of it, there is nothing like some red herrings and useless object structure to distract whoever is reading the code from your true goals. For example, instead of using any number directly, you could pull it from a dictionary, or get it from the length of another object (say a list of size two), or even better, hide the number 2 in some string, and then regex it out with an awkward-to-read pattern.
Since you want to make the simple complex, you could do some goofy things with complex numbers. Assuming you have any libraries available for complex arithmetic, you could, for example, leverage the most beautiful equation in mathematics: e^(pi*i) + 1 = 0. For instance in Java using Apache Commons Math (of course you would obfuscate the variable names):
Complex i = new Complex(0, 1);
double two = i.multiply(Math.PI).exp().getReal() + 3 + i.multiply(Math.PI).exp().getImaginary()*5;
The real part is -1, so adding 3 gives us 2. The imaginary part is 0, so multiplying it by 5 and adding it is a red herring that doesn't do anything.*
As long as this is for fun, you can try other variants using other similar identifies. However, I don't recommend relying on this type of thing to truly obfuscate code within a real product. There are packages that obfuscate code for you, and automatically changing variable names to gibberish goes a long way to deterring humans (while still letting the code stay readable for the sanity of developers).
*In floating point arithmetic the imaginary part might not be exactly 0, but you said you were interested in accuracy to two decimal places.
Since this is homework I don't want to just give you the answer but consider the number as it is represented in binary and what sort of binary operands are at your disposal that might help doing in doing multiplication.

Math opposite sign function? [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 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 :)

Resources