0

Given the limit of changing the value of variables, calculate the function according to the formula. enter image description here

This function is a little more difficult to imagine, before that I made a regular function and I can not imagine the essence of complex functions.

class Program
{
    static void Main(string[] args)
    {
        double x <= 1.4;
        Console.WriteLine("Enter the value of x:");
        double x = Convert.ToDouble(Console.ReadLine());
        double result = Math.sqr(x)-(7 / x);
        double result2 = a * Math.pow(3, x) + 7 * Math.Sqrt(x);
        double result3 = Math.log10(x + 7);
        Console.WriteLine("The result of the function is: ");
    }
}                                                                                                                                                                                                                                               
  • Does your code compile, in particular `double x <= 1.4;`? If I was going to model a _function_ in a programming language like C#, I'd use a _function_ (aka a _method_). You don't, by the way, ask a question (an important step on a question and answer site). Important side question: does the comma in `1,4` indicate a comma (as in the US, or a decimal separator as in much of Europe)? – Flydog57 Feb 06 '23 at 05:36
  • ouch my bad, I just need to method or idea about realization of this task. Just i don't have any idea – Antagonist Feb 06 '23 at 07:27
  • I suspect this question might be better asked in [computer science SE](https://cs.stackexchange.com/) – John Wu Feb 06 '23 at 08:39

1 Answers1

3

Probably you should create a method for this function like:

public double f(double x)
{
    if (x < 0.7 || x > 2)
        throw new ArgumentOutOfRangeException(...);

    if (x < 1.4)
        return ...
    else if (x == 1.4)
        return ...
    else
        return ...
}

And then you can call this method from your Main() method, where you can get the value of x from user input, as you already do, and call f(x) to calculate the result.

There is an issue though. Floating point numbers are represented only approximately. E.g. value 1.4 can be actually stored as 1.399999999998... or 1.40000000000013..., so x == 1.4 comparison is not really correct.

Usually floating point values are checked against some interval, something like Math.Abs(x - 1.4) < 1E-15.

Another option is to use decimal type (see Difference between decimal, float and double in .NET?)

Mike Mozhaev
  • 2,367
  • 14
  • 13
  • `Decimal` may help on the comparisons, but all those calculations use the `Math` class which will convert things to `double`. Rather than using an arbitrary epsilon value (`1E-15`) consider using `double.Epsilon` or `decimal.Epsilon` for the comparisons – Flydog57 Feb 06 '23 at 19:51
  • That's correct about conversions to double. But the critical thing here is equality condition to choose the expression, and for this decimal will work. As for the calculations then precision of double should be enough. Epsilon is the minimal non-zero value of double and decimal and I'm not sure it will be wide enough interval for these comparisons. My first thought was also about Epsilon, but I haven't found any confirmation that it can be safely used for such comparisons. – Mike Mozhaev Feb 06 '23 at 21:46