处理巨数 C、Java、Informix
我们面临的情况是处理最多 15 位数字的数字。我们需要从文本文件中解析这个值,通过C,将其存储在Informix表中。还有另一个 Java 组件读取这些值、执行数学运算并计算结果。
我对此进行了一些研究,发现 Informix 提供的 int8 数据类型将是 C 的合适候选者。
对于 Java,我计划使用 BigInteger 类。
采取这种方法有什么陷阱吗?任何想法表示赞赏。
仅供您参考,这是一个旧的应用程序,到目前为止它一直在使用原语。此外,它只能处理基元范围内的数字。
谢谢。
We are in a situation to handle numbers with a maximum of 15 digits. We need to parse this value from a text file, through C, store it in Informix table. There is another Java component that reads these values, does mathematical operations and computes a result.
I have been doing some research on this and found that the int8 datatype provided by Informix will be a suitable candidate for C.
With regard to Java, I plan to use the BigInteger class.
Are there any pitfalls in taking this approach. Any thoughts are appreciated.
Just for your information, this is an old application and it has been using the primitives so far. Moreover it has only been able to handle numbers within the range of the primitives.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只要所有数字(包括计算)保持在 15 位以下,长基元就是一个完全有效的选择,并且它具有性能和运算符的优势。 BigInteger 的缺点实际上是做数学的冗长/困难,你必须一直使用方法(Java 中没有运算符重载,唯一适用于对象的运算符是用于字符串连接的 +)。
就性能而言,在不了解更多有关您的应用程序的情况下,我不能说,但第一个假设应该是使用 BigInteger 是可以的,除非您进行其他测量。
As long as all of your numbers (including calculations) remain under 15 digits, a long primitive is a perfectly valid choice, and it has the advantage of performance and operators. The disadvantage of BigInteger really is the verbosity/difficulty of doing math where you have to use methods all the time (there is no operator overloading in Java and the only operator that works on an object is + for string concatenation).
In terms of performance, without knowing more about your application I can't say, but the first assumption should be that it is fine to use BigInteger until you measure otherwise.
如果您的“巨大”数字最多为 15 位十进制数字,那么
long
可能是一个选择。 Javalong
类型的范围为-2**63 到 +2**63 - 1
。 2**63 是 19 位十进制数字……如果我能算的话:-)。当然,如果计算的任何中间结果是 19 位或更多,
long
将不起作用,您可能需要使用 BigInteger。使用 BigInteger 没有特别的缺陷,只是它们比原始整数类型慢得多……而且更冗长。事实上,它们的优点是您不必再担心整数溢出。
If your "huge" numbers are 15 decimal digits at most, then
long
may be an option. The Javalong
type has range-2**63 to +2**63 - 1
. And 2**63 is 19 decimal digits ... if I can count :-).If course, if any of the intermediate results of your computations are 19 digits or more,
long
won't work and you will probably need to use BigInteger.There are no particular pitfalls with using BigInteger, except that they are significantly slower than primitive integer types ... and more verbose. Indeed, they have the advantage that you don't have to worry about integer overflow any more.
如果您的 Informix 版本支持 BIGINT 和 BIGSERIAL,请优先使用它们而不是 INT8 和 SERIAL8。由于各种复杂的原因,INT8和SERIAL8实际上在磁盘上占用了10个字节; BIGINT 和 BIGSERIAL 支持相同的值范围,但仅占用磁盘上的 8 个字节。
如果您的 Informix 版本不支持 BIGINT 和 BIGSERIAL,请考虑升级到 IDS 11.50。
如果 Informix JDBC 驱动程序仅支持 INT8,那么无论如何都使用 INT8。
If your version of Informix supports BIGINT and BIGSERIAL, use them in preference to INT8 and SERIAL8. For various complex reasons, INT8 and SERIAL8 actually occupy 10 bytes on disk; BIGINT and BIGSERIAL support the same range of values but only occupy 8 bytes on disk.
If your version of Informix does not support BIGINT and BIGSERIAL, consider upgrading to IDS 11.50.
If the Informix JDBC driver only support INT8, then use INT8 anyway.
如果我理解正确,您会读取“小”值(适合 INT8 整数),然后在 Java 中进行计算,得到“大”值作为结果;对吗?
只要您不尝试将 BigInteger 值压缩为 INT8 数据类型,对我来说就很好。
正如 Stephen C 已经指出的,Java 的 long 类型也有 64 位宽度,因此这可能也合适。
If I understand you correctly, you read "small" values (fit into INT8 integers) and then make calculations in Java where you get "big" values as results; right?
As long as you don't try to squeeze the BigInteger values into the INT8 data type, it looks fine to me.
As Stephen C already noted, Java's long type also has 64bit width so this might be suitable, too.
在 C(具体来说是 C99)中,
long long
类型是 64 位有符号二进制整数,因此最多可以容纳 18 位数字。在 Java 中,
long
类型是等效类型,即 64 位有符号二进制整数。In C (C99, to be specific), the
long long
type is a 64 bit signed binary integer, so it can hold up to 18 digits.In Java, the
long
type is the equivalent type, a 64-bit signed binary integer.如果您只是使用 C 从文本文件中解析这些值,然后将它们发送到其他地方,那么您应该能够将它们保留为字符串。如果不自己动手,您将无法进行任何类似数学的运算,但根据您的描述,您不必这样做。
如果 C 程序只是 15 位数字的字符串,那么花时间寻找二进制表示形式或可以对这些数字进行数学运算的库是没有意义的。
If you're just using C to parse these values from a text file, and then ship them somewhere else, you should be able to keep them as strings. You won't be able to do any math-like operations without rolling your own, but from your description you don't have to.
There's no point in spending time finding a binary representation, or a library that can do maths on these numbers, if all they are to the C program are 15-digit strings.