我无法想象应该使用什么样的代码来解决这种问题。

huangapple go评论75阅读模式
英文:

I can't imagine what the code should be to solve these kinds of problems

问题

Given the limit of changing the value of variables, calculate the function according to the formula.

此函数的难度略高,之前我制作了一个普通函数,无法想象复杂函数的本质。

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:");
    }
}
英文:

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 &lt;= 1.4;
        Console.WriteLine(&quot;Enter the value of x:&quot;);
        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(&quot;The result of the function is: &quot;);
    }
}                                                                                                                                                                                                                                               

答案1

得分: 3

你可能应该为这个函数创建一个方法,如下所示:

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 ...
}

然后,你可以从你的 Main() 方法中调用这个方法,在这个方法中,你可以从用户输入中获取 x 的值,就像你已经在做的那样,然后调用 f(x) 来计算结果。

不过有一个问题。浮点数只能以近似方式表示。例如,值 1.4 实际上可能被存储为 1.399999999998...1.40000000000013...,所以 x == 1.4 的比较并不是真正正确的。

通常,浮点值与某个区间进行比较,类似于 Math.Abs(x - 1.4) < 1E-15

另一个选项是使用 decimal 类型(参见 https://stackoverflow.com/questions/618535/difference-between-decimal-float-and-double-in-net/618596#618596)。

英文:

Probably you should create a method for this function like:

public double f(double x)
{
    if (x &lt; 0.7 || x &gt; 2)
        throw new ArgumentOutOfRangeException(...);

    if (x &lt; 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) &lt; 1E-15.

Another option is to use decimal type (see https://stackoverflow.com/questions/618535/difference-between-decimal-float-and-double-in-net/618596#618596)

huangapple
  • 本文由 发表于 2023年2月6日 13:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定