提出一种在 C# 中获得高精度数字的好方法

发布于 2024-08-09 15:36:14 字数 343 浏览 3 评论 0原文

我正在对一个项目进行编程,我应该将用户的密钥存储到机器的初始配置中,我想用 C# 编写它。

我有一个初始配置,由两个数字 R 和 X0 组成,R = 3.9988 且 X0 = 0.5。我想将用户密钥添加到这些数字中。例如:

密钥:hos110 =>

  • R = 3.9988104111115049049048
  • X0 = 0.5104111115049049048

104111115049049048 是连接的密钥的 ASCII 码。

我如何存储这些数字?

有更好的方法吗?

更新:MATLAB 怎么样?

I am programming on a project which I should store the key of the user to the initial configuration of a machine, I want to write it in C#.

I have an initial configuration which consists of two number R and X0, R = 3.9988 and X0 = 0.5. I want to add the user key to these numbers. for example:

Key: hos110 =>

  • R = 3.9988104111115049049048
  • X0 = 0.5104111115049049048

104111115049049048 are ASCII codes of the key which are concatenated.

How can I store these numbers?

Is there a better method for doing this?

Update: How about MATLAB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

再见回来 2024-08-16 15:36:14

您并不是真正在“添加”数字。您正在连接字符串。

将它们存储为字符串。没有比这更精确的了。

如果您需要执行任何算术运算,很容易将它们即时转换为十进制数。

You're not really "adding" numbers. You are concatenating strings.

Store them as strings. You can't get much more precise than that.

If you need to perform any arithmetic operations, it is easy enough to convert them to a decimal number on the fly.

影子是时光的心 2024-08-16 15:36:14

我不太明白为什么你使用键作为数字的一部分,但把它放在一边...... System.Decimal (又名 decimal)似乎是适合这里工作的工具。

I don't really follow why you're using a key as part of a number, but leaving that aside... System.Decimal (aka decimal) seems like the right tool for the job here.

缺⑴份安定 2024-08-16 15:36:14

如果您需要无限精度,则需要称为 BigInteger 的东西。然而,这些类通常仅用于科学计算(通常不适合存储数据),这似乎与您的代码示例并不匹配。如果您只需要进行一般计算,请使用字符串,然后将它们转换为十进制进行计算。

但是,如果您正在寻找这样的 BigInterger 类,您可以这里找到一个< /a>.

.Net 4.0 将在名为 System.Numerics.BigInteger 的类库中拥有一个 BigInteger 内置类。

If you need infinite precision you need something that is called BigInteger. However these classes are usually only used for scientific calculations (and usually unsuited for stroring the data) which doesn't really seem to match your code sample. If you need to do only general calculations use Strings and then convert them to Decimal for the calculations.

However if you are looking for such a BigInterger Class you can find one here.

.Net 4.0 will have a BigInteger built-in-class in the class libraries named System.Numerics.BigInteger.

不再让梦枯萎 2024-08-16 15:36:14

好吧,根据您想要达到的精度,您可以将它们保存为一对 十进制值。

但是,如果这是 ASCII 代码,您可能只想将它们直接保存为字符串。这将避免数值精度问题,特别是如果您要在使用此信息之前获取 104111...。

Well, depending on the precision you are trying to achieve, you can probably save these as a pair of decimal values.

However, if this is an ASCII code, you may just want to save these as a string directly. This will avoid the numerical precision issues, especially if you're going to pull off the 104111... prior to using this information.

⊕婉儿 2024-08-16 15:36:14

看起来你正在存储一个“密钥”,那么为什么不使用字符串呢?

It seems that you are storing a "key", so why not use a String then?

背叛残局 2024-08-16 15:36:14

浮点数本质上是不精确的。我不确定这个“初始配置”是什么,或者为什么它是一个浮动,但您将无法添加“用户密钥”(无论是什么)并在以后恢复它。将用户密钥单独存储在字符串或其他内容中。

Floating point numbers are inherently imprecise. I'm not sure what this 'initial configuration' is or why it's a float, but you're not going to be able to tack on a 'user key' (whatever that may be) and recover it later. Store the user key separately, in a string or something.

娇俏 2024-08-16 15:36:14

如果这些“数字”没有数值,即您不会将它们用于数学计算,则无需将它们存储在数字数据类型中。您可以将它们存储为字符串。

If these 'numbers' have no numeric value, i.e. you will not use them for mathematical computation then there is no need to store them in a numeric datatype. You can store them as strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文