无法显示所有十进制数字 C#

Cant show all decimal numbers C#

我在 C# 中有一个小方法,目的是解决这个 基本方程:

我手动给出 nx

我们假设 X 值为 3 并且 n 值为 1。如果我计算方程式,我得到这个结果:

我的问题是输出是 0,我也尝试解析结果,但仍然是 0。

真正的结果是0.88888888但是在程序输出中我只是得到了0.

这是我的代码:

using System;

namespace Polinomio
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 3;
            int n = 1;

            double result = 0;

            for (int i = 0; i <= n; i++) {
                result += (double)(Math.Pow((x - 1) / 3, Math.Pow(2, i))) / Math.Pow(2, i);           
            }

            Console.WriteLine(result);
        }
    }
}

我不知道我做错了什么或遗漏了什么,我会感谢任何帮助。

只需将变量的数据类型更改为双精度。

 double x = 3;
 int n = 1;

 double result = 0;

 for (int i = 0; i <= n; i++)
 {
      result += (Math.Pow((x - 1) / 3, Math.Pow(2, i))) / Math.Pow(2, i);
 }

 Console.WriteLine(result);

这样就可以了。

查看此处:Implicitly converting int to double C# 代码中的隐式转换优先级。