1/252 = 0 in C#

1/252 = 0 in c#?

Yes - that calculation is being performed in integer arithmetic. Try this:

double num = 1.0 / 252.0;

Basically, the type of the variable that the result is being assigned to doesn't affect the arithmetic operation being performed. The result of dividing one integer by another is an integer; if you want floating point arithmetic to be used, you need to make one or other of the operands a floating point type. One simple way of doing that with literals is to stick ".0" on the end. Another alternative is to use the d suffix:

double num = 1d / 252d;

Note that you only really need to make one operand a floating point value for it to work, but for clarity I'd probably do both in this case.

That's easy to do with literals of course, but for other expressions (variables, the results of method calls etc) you'd need to use a cast:

int x = 1;
int y = 252;
double num = (double) x / (double) y;

Again, you only need to cast one of them, so this would work too:

int x = 1;
int y = 252;
double num = (double) x / y;

Note that this isn't specific to division - it affects the other arithmetic operators too.

Problem with Double and division

Because 1 and 2 are integers. The result is 0. If you cast that to a double, it's still 0. This question was asked just a couple of days ago.

why c# always say that 3/2 is 1?

You're using integer arithmetic. If you don't want to do that, make one or both of the operands floating point values, e.g.

decimal test = 3m / 2m;

or

double test = 3d / 2d;

(Only one of the operands has to be a non-integral type, but I believe it's clearer if you can easily make both of them the same.)

Calculate IP range by subnet mask

To determine the adress range follow these steps :

1) Take your subnet mask (here 255.255.255.0) and convert it in binary :

11111111.11111111.11111111.00000000

( 8 + 8 + 8 + 0 = 24 -> So you can write your ip adresse like this : 192.168.1.x/24 because you are in a /24 network)

2) You have in a /24 network 256-2=254 usable ip adresses for host (one is for the network adress (the first in your range) and the other one is for the broadcast adress (the last in your range)).

3) To get your range simply get your network adress (the first ip adress according to your subnet mask) and get the next 255 ip addresses and you'll have your range.

Your network adress:

In binary the last octet has to be null :

xxxxxxxx.xxxxxxxx.xxxxxxxx.00000000

Your broadcast adress:

In binary the last octet has to be equal to 1:

xxxxxxxx.xxxxxxxx.xxxxxxxx.11111111

Here your ip adress is 192.168.1.5.
In binary we get:

11000000.10101000.00000000.00000101
  1. Your network address: 11000000.10101000.00000000.00000000 <->
    192.168.1.0
  2. Your broadcast address: 11000000.10101000.000000000.11111111 <->
    192.168.1.255
  3. First usable ip address: 192.168.1.1

  4. Last usable ip address : 192.168.1.254

Hope you enjoyed reading bad english.
Tell me if you have any question,
Loris

Raising a decimal to a power of decimal?

To solve my problem I found some expansion series, and them I had them implemented to solve the equation X^n = e^(n * ln x).

// Adjust this to modify the precision
public const int ITERATIONS = 27;

// power series
public static decimal DecimalExp(decimal power)
{
int iteration = ITERATIONS;
decimal result = 1;
while (iteration > 0)
{
fatorial = Factorial(iteration);
result += Pow(power, iteration) / fatorial;
iteration--;
}
return result;
}

// natural logarithm series
public static decimal LogN(decimal number)
{
decimal aux = (number - 1);
decimal result = 0;
int iteration = ITERATIONS;
while (iteration > 0)
{
result += Pow(aux, iteration) / iteration;
iteration--;
}
return result;
}

// example
void main(string[] args)
{
decimal baseValue = 1.75M;
decimal expValue = 1/252M;
decimal result = DecimalExp(expValue * LogN(baseValue));
}

The Pow() and Factorial() functions are simple because the power is always an int (inside de power series).



Related Topics



Leave a reply



Submit