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

无法显示所有十进制数字C#,c#,parsing,math,double,C#,Parsing,Math,Double,我在C#中有一个小方法,目的是解这个基本方程: 我手动给出n和x值 我们将假设X值为3,n值为1。如果我对方程进行评估,我得到了以下结果: 我的问题是输出是0,我也尝试分析结果,但仍然是0 实际结果是0.8888888,但在程序输出中我刚刚得到0 这是我的密码: using System; namespace Polinomio { class Program { static void Main(string[] args) {

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

我手动给出n和x值

我们将假设X值为3n值为1。如果我对方程进行评估,我得到了以下结果:

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

实际结果是0.8888888,但在程序输出中我刚刚得到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);
        }
    }
}
使用系统;
名称空间Polinomio
{
班级计划
{
静态void Main(字符串[]参数)
{
int x=3;
int n=1;
双结果=0;

对于(inti=0;i,只需将变量的数据类型更改为double即可

 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);
double x=3;
int n=1;
双结果=0;

对于(inti=0;i,只需将变量的数据类型更改为double即可

 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);
double x=3;
int n=1;
双结果=0;

对于(int i=0;我非常感谢!这激起了我的好奇心……你知道为什么会发生这种情况吗?@weirzetig29你在做整数运算,然后转换为双精度运算。但是你首先需要做浮点运算。@Fildor真的很有趣。@weirzetig29更新了你可能感兴趣的链接以最终确定答案。可能会的即使你写了
result+=(Math.Pow((x-1.0)/3,Math.Pow(2,i))/Math.Pow(2,i),你也能工作
非常感谢!这唤起了我的好奇心……你知道为什么会发生这种情况吗?@weirzetig29你在做整数运算,然后转换为双精度运算。但是你首先需要做浮点运算。@Fildor真的很有趣。@weirzetig29更新了你可能感兴趣的链接以最终确定答案。可能会有w即使你写了
result+=(Math.Pow((x-1.0)/3,Math.Pow(2,i))/Math.Pow(2,i);