Multiplying large values results to negative value [duplicate] - c#

This question already has answers here:
Multiplying two positive Int32 returns incorrect, negative answer?
(3 answers)
Closed 3 years ago.
double result = 0.0;
result = 7777 * 7777 * 7777 *7777 ;
Instead of getting the result 3658039542829441, I am getting -1283042943. Can someone suggest how to avoid this? I understand the calculation is still under double type's range.

Each 7777 is an integer literal. So it is performing integer multiplication. You will need to make these double literals using the d suffix. Then it will perform double multiplication.
double result = 0.0;
result = 7777d * 7777d * 7777d * 7777d ;

Related

is this normal in .NET 6 [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
How can I divide two integers to get a double?
(9 answers)
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
I feel it's a bug, is it?
decimal s = 30 / 9;
double w = 20 / 9;
Console.WriteLine(s);
Console.WriteLine(w);
and the result is
Is this normal, yes. Is this a bug, no.
You're doing integer math on 30 / 9 which is 3 and then assigning to a decimal which then does the conversion. Same with the second line.
Try this instead:
decimal s = 30m / 9m;
double w = 20.0 / 9.0;
Console.WriteLine(s);
Console.WriteLine(w);
That gives:
3.3333333333333333333333333333
2.2222222222222223
You need to declare your constants in your expression with the correct type. Suffix an m for decimal. And either provide a decimal place or suffix with a d for double.

Calculate Power of Double with Double exponent in C# [duplicate]

This question already has answers here:
How can I divide two integers to get a double?
(9 answers)
Closed 3 years ago.
I need to calculate x^y where both x and y are Doubles.
I tried using Math.Pow:
Double result = Math.Pow(24.69, 2/3);
The value of result is 1 where it should be 8.4790 ...
Any idea why?
For the exponent you are passing in 2 ints which is do integer division. So it is doing:
Math.Pow(24.69, 0)
To fix this use doubles like this:
Double result = Math.Pow(24.69, 2.0/3.0);

Converting division of int to int to a decimal is giving 0 [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 4 years ago.
I came up with a formula to keep numbers a simple int and was surprised to see that the following was producing a 0.
var x = 45;
var y = 100;
var z = Convert.ToDecimal(x / y * 100);
In the above example, it almost seems ridicilous that I'm dividing by 100, then multiplying by 100 but in most cases, the x and y values are not nice integers.
When my x and y values end up being integers, the above conversion produces a 0. Why is that?
X / Y = 0.45
Int conversion of 0.45 is 0.
Therefore 0 * 100 = 0.
Therefore Convert.ToDecimal(0) is 0.
This does look a bit odd, but the problem there is with the order the expressions are evaluated: first the arguments of Convert are evaluated, and that is done inintegers. That’s why the result is truncated.
You should replace the conversation with an expression that forces evaluation in decimals:
var z = 100M * x / z;

floats being saved as 0 after calculation [duplicate]

This question already has answers here:
Why do these division equations result in zero?
(10 answers)
Closed 5 years ago.
Very confused here, I have this code:
float temp = (1/10)*100;
Label.Text = Convert.ToString(temp);
for some reason my temp variable is being saved as 0 which means my label text is changed to 0 when I'm expecting 10, I have the same issue when using doubles instead. What has gone wrong?
Since 1, 10 and 100 are all integer values, the division is also an int value, rounded down. In this case 1/10 = 0 so (1/10)*100 = 0
If you do not want this try using floats:
(1.0f/10)*100
In case you're working with integer variables you have to convert them first. This can be achieved by casting, like so:
int a=1;
...
float b = ((float) a)/10; // b will be 0.1
Another quick way of doing this in a line with multiple operations is multiplying by 1.0f:
int x = 100;
float c = (a*1.0f)/x; // c will be 0.01f

Division in C# to get exact value [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 9 years ago.
If I divide 150 by 100, I should get 1.5. But I am getting 1.0 when I divided like I did below:
double result = 150 / 100;
Can anyone tell me how to get 1.5?
try:
double result = (double)150/100;
When you are performing the division as before:
double result = 150/100;
The devision is first done as an Int and then it gets cast as a double hence you get 1.0, you need to have a double in the equation for it to divide as a double.
Cast one of the ints to a floating point type. You should look into the difference between decimal and double and decide which you want, but to use double:
double result = (double)150 / 100;
Make the number float
var result = 150/100f
or you can make any of number to float by adding .0:
double result=150.0/100
or
double result=150/100.0
double result = (150.0/100.0)
One or both numbers should be a float/double on the right hand side of =
If you're just using literal values like 150 and 100, C# is going to treat them as integers, and integer math always "rounds down". You can add a flag like "f" for float or "m" for decimal to not get integer math. So for example result = 150m/100m will give you a different answer than result = 150/100.

Categories

Resources