无法使用简单的洛伦兹因子计算器来处理小数。我想念什么?

时间:2019-02-05 07:54:43

标签: c# calculator

我正在尝试制作一个可在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)))));

            }



        }

    }
    }

3 个答案:

答案 0 :(得分:1)

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

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

您看到int无法处理小数。

答案 1 :(得分:0)

如果您的数字高,我建议您使用@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id;

  

小数位数具有更高的精度,通常用于要求高度准确性的货币(金融)应用程序中。   Reference

除了洛伦兹因子使用@GetMapping("/users/{id}") public ResponseEntity<User> getUserFromId(@PathVariable int id) { System.out.println(id); // should display the id // ... }

Decimal

答案 2 :(得分:0)

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