-2

I need to complete a task - to implement a solution to a formula in C#, but the result in the console is not what I need. The result should be -11.84361, but the console outputs 15.1676628055975. What's wrong with the code? What to fix?

namespace Exercise10
{
class Program
{
    static void Main()
    {
        Number number1 = new Number();
        number1.Z();
        Console.ReadKey();
    }
}
class Number
{
    double x, y, znamenatel, znamenatelh, virazhenie;
    public void Z()
    {
        M:
        Console.Write("Введите число x: ");
        x = double.Parse(Console.ReadLine());
        Console.Write("Введите число y: ");
        y = double.Parse(Console.ReadLine());
        znamenatelh = Math.Pow(Math.E, 2) - Math.Pow(x + y, 3 / 4);
        if (znamenatelh < 0)
        {
            Console.WriteLine("Нельзя извлечь корень из отрицательного числа. Введите числа заново.");
            goto M;
        }
        znamenatel = Math.Pow(znamenatelh, 1 / 3);
        if (znamenatel == 0)
        {
            Console.WriteLine("Нельзя делить на нуль. Введите числа заново.");
            goto M;
        }
        void Z_()
        {
            virazhenie = (x / znamenatel) + Math.Pow(Math.Pow(x - y, Math.Sin(x * y)) / znamenatel, 7 / 3);
            Console.Write($"Z({x}, {y}) = {virazhenie}");
        }
        Z_();
    }
}
} // <---- for some reason leaves the block

Here is all the information about the program and what should happen.

  • `3 / 4 == 0` - integer division; put it as `3.0 / 4.0` to get `0.75`; same for `1 / 3`, `7 / 3` which sould be `1.0 / 3.0` and `7.0 / 3.0` – Dmitry Bychenko Feb 10 '21 at 16:07

1 Answers1

0

When you divide integer by integer the result is integer as well; in your case

3 / 4 == 0
1 / 3 == 0
7 / 3 == 2

In order to get floating point results (0.75, 0.33333, 2.33333) you should divide floating point values: 3.0 / 4.0, 1.0 / 3.0, 7.0 / 3.0:

// znamenatel' is Russian for "denominator"
znamenatelh = Math.Pow(Math.E, 2) - Math.Pow(x + y, 3.0 / 4.0);

...

// znamenatel' is Russian for "denominator" 
znamenatel = Math.Pow(znamenatelh, 1.0 / 3.0);

...

// virazhenie is Russian for "formula" or "expression" 
virazhenie = (x / znamenatel) + Math.Pow(Math.Pow(x - y, Math.Sin(x * y)) / znamenatel, 7.0 / 3.0);

Side note: goto M; is evil, turn

 label M;

 ...

 if (condition)
   goto M;

construction into loop, e.g.

 while (true) {
   ...

   if (condition)
     continue;

   break;
 }

Edit: Let's extract computation:

private static double Compute(double x, double y) {
  if (x < y)
    return double.NaN;

  double denominator = Math.Exp(2) - Math.Pow(x + y, 3.0 / 4.0);

  if (denominator == 0)
    return x < 0                ? double.NegativeInfinity : 
           (x == 0) || (x == y) ? double.NaN : 
                                  double.PositiveInfinity;

  denominator = Math.Pow(Math.Abs(denominator), 1.0 / 3.0) * Math.Sign(denominator);

  double result = x / denominator;

  double fraction = Math.Pow(x - y, Math.Sin(x * y)) / denominator;

  result += Math.Pow(Math.Abs(fraction), 7.0 / 3.0) * Math.Sign(fraction);

  return result;
}

UI (Simplified)

 public void Z() {
   double z = 0.0;

   while (true) {
     Console.WriteLine("Enter x: ");
     double x = double.Parse(Console.ReadLine());
     Console.WriteLine("Enter y: ");
     double y = double.Parse(Console.ReadLine());
     
     z = Compute(x, y); 

     if (!double.IsNaN(z) && !double.IsInfinity(z))
       break;

     Console.WriteLine("Invalid x and (or) y. Please, try again.");
   }
   
   Console.WriteLine(z);
 }

Demo:

Console.Write(Compute(15, 5));

Outcome:

-11.8436193845787
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Unfortunately, it now displays an error message that is specified in the condition and returns to the input of numbers. For the same x and y. – Екатерина Самойлова Feb 10 '21 at 16:21
  • @Екатерина Самойлова: please, provide relevant `x` and `y` (say, `x = 1` and `y = 2`) and the expected result to test the routine – Dmitry Bychenko Feb 10 '21 at 16:30
  • The console at x = 1 and y = 2 gives "Z(1, 2) = не число" (Translation: Z(1, 2) = not a number) as the result. With such values, the calculator does not give an approximate value, it only abbreviates the expression. – Екатерина Самойлова Feb 10 '21 at 16:42
  • @Екатерина Самойлова: sure, you have `double.NaN` ("Not a Number") since `Math.Pow(x - y, Math.Sin(x * y)) == Math.Pow(-1, 0.90930...)` ~ some *complex value* (`-0.95968 + 0.28111i` as Octave puts it) – Dmitry Bychenko Feb 10 '21 at 16:49
  • I entered the exponent value into the calculator, and not the natural logarithm, which is needed in the task (I confused Math.E and the e symbol in the calculator, which, as it turned out, meant the exponent). – Екатерина Самойлова Feb 10 '21 at 17:06