如何表示大于最大值的数字

发布于 2024-10-04 02:13:06 字数 152 浏览 0 评论 0原文

如何表示超过任何特定数据类型(int、long)最大值的值?

我正在考虑再设置一个像柜台一样的存储空间。一旦超过最大值,计数器就会更新,表示变量已超出限制“x”次。还有其他有效的方法吗?

我们如何显示准确的值?

PS:这只是一个假设性问题。

How to represent values that exceed the max values of any particular data type (int, long ) ?

I am thinking of having another storage space acting like a counter. Once the max value is crossed, the counter updates to say , the variable has exceeded the limit for "x" number of times. Is there some other efficient way of doing it ?

How do we display the exact value ?

P.S : Just a hypothetical question.

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

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

发布评论

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

评论(3

回忆那么伤 2024-10-11 02:13:06

一种方法是为此目的实际制定一个价值观。

例如,如果您有一个 16 位整数类型,可以表示从 0 到 65535(含)的值,请将范围缩小到 0 到 65534,并使用 65535 来表示“太大了”的值。

您必须小心控制操作,以便它们不会在正常事件过程中产生该值,但如果您的语言提供类功能,那么这相当容易。

或者,您可以使用下一个最大的数据类型,例如 long 代表 intlong long 代表 long 并使用存储信息的额外范围。

而且,如果您需要更多,您可以编写一个 bignum 库(或使用已经存在的库),这样您的数字就不会受到人为限制。

One way is to actually carve out one of the values for this purpose.

For example, if you have a 16-bit integral type that can represent the values from 0 through 65535 inclusive, reduce the range to 0 through 65534 and use 65535 to represent the value "too darned big".

You would have to be careful to control the operations so that they wouldn't produce that value in the normal course of events but that's reasonably easy if your language provides class capabilities.

Alternatively, you can use the next biggest data type such as long for int or long long for long and use the extra range to store information.

And, if you need more than that, you can code up a bignum library (or use one that already exists) so that there are no artificial limits placed on your numbers.

耶耶耶 2024-10-11 02:13:06

我不确定,但我认为您必须创建自己的数据类型(或类)。这以前已经做过了。事实上,一些语言有自己的实现:

http:// /groups.csail.mit.edu/mac/users/adams/BigInt/BigInt.html

I'm not certain but I think you'll have to create your own data type (or class). This has been done before. In fact, some languages have their own implementation of this:

http://groups.csail.mit.edu/mac/users/adams/BigInt/BigInt.html

幽梦紫曦~ 2024-10-11 02:13:06

考虑标准 IEEE-754 浮点和“Infinity”位模式。人们可以在固定的 int/long 内保留类似的位模式来表示“无穷大”。然而,CPU 不会像大多数 FPU 那样帮助您解决数学/溢出状态。

一些语言,如 Ruby 或 elisp 已经在整数中“保留”位(例如,Ruby 中的固定数限制为 [-2^29,2^29-1],因为 2 位用于对象管理)。使用特定的位模式与位只会删除一个潜在值。

如果您谈论的是更高级的语言,例如 C#,那么定义自定义类型很容易:

struct IntWithStuff {
   int value;
   bool isTooBig;
}

您还可以重载各种运算符并实现一些(显式)强制转换,但我离题了……

Consider an standard IEEE-754 float and the "Infinity" bit-patterns. One could reserve a similar bit-pattern inside a fixed int/long to mean "Infinity". However, the CPU won't help you out on the math/overflow states like most FPUs will.

Some languages like Ruby or elisp already "reserve" bits in integers (e.g. a fixnum in Ruby is limited to [-2^29,2^29-1] because 2 bits are used for object housekeeping). Using a specific bit-pattern vs. bit would only remove one potential value.

If you are talking about higher-level languages, say, C#, then it's easy to define a custom type:

struct IntWithStuff {
   int value;
   bool isTooBig;
}

You could also overload the various operators and implement some (explicit) casts, but I digress...

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