【问题标题】:Can't get my simple Lorentz factor calculator to work with decimals. What am I missing?无法让我的简单洛伦兹因子计算器处理小数。我错过了什么?
【发布时间】:2019-02-05 07:54:42
【问题描述】:

我正在尝试制作一个在 c# 中计算洛伦兹因子的计算器。但它似乎不适用于小数。我只完成了计算器的第一部分:

static void Main(string[] args)
        {

            int c = 299792458;

            Console.Write("speed: ");
            string speed = Console.ReadLine();
            Console.Write("Gammafaktor: ");
            string Gammafaktor = Console.ReadLine();


            {
            }
            int gamma1 = Convert.ToInt32(Gammafaktor);
            int speed1 = Convert.ToInt32(speed);

            if (gamma1 != 0)
                {
                Console.WriteLine(1 / (Math.Sqrt(1 - ((speed1 * speed1) / (1)))));

            }



        }

    }
    }

【问题讨论】:

  • 首先,请重新格式化您的代码,如果我将其粘贴到 vs... 中,我认为它不会起作用,如果您想用小数计算,用户 double 而不是 @ 987654323@.

标签: c# calculator


【解决方案1】:

如果要使用小数进行计算,则需要使用Double 数据类型而不是Integer

int 的范围从 -2,147,483,648 到 2,147,483,647,double 的范围从 +-5.0 x 10-324 到 +-1.7 x 10308。

您看到int 无法处理小数。

【讨论】:

    【解决方案2】:

    如果你的数字系数很高,我建议你使用Decimal

    小数的精度要高得多,通常用于需要高精度的货币(金融)应用程序中。 Reference

    除了洛伦兹因子使用1 / sqrt(1 - v*v)

    var calc = 1m / Convert.ToDecimal(Math.Sqrt(1 - speed1*speed1));
    

    【讨论】:

    • doublephysical 而不是 finance 世界
    • 感谢您的帮助,我现在可以输入小数,但是当我使用 var " calc = 1m / Convert.ToDecimal(Math.Sqrt(1 - speed1*speed1));"我得到错误无法将十进制转换为双精度
    • 知道了,应该是var calc = 1m / Convert.ToDecimal(Math.Sqrt((double)(1 - speed1 * speed1)));
    【解决方案3】:

    当您除以 1 而不是平方根下的 c 时,这意味着您以 c 为单位测量速度,而不是以 m/s 为单位。因此,速度的数值必须是 1 的分数。 但是,您的变量 speed1 是一个整数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-15
      • 2022-01-22
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多