简体   繁体   English

您如何将googol存储在数据库中(以及其他非常大的数字)?

[英]How do you store a googol in the database (and other very large numbers)?

How do you efficiently store very large and very small numbers from say 10^-100 to 10^100 , so that you can use them to calculate values in a programming language like JavaScript. 您如何有效地存储从10^-10010^100非常大和非常小的数字,以便您可以使用它们来以JavaScript之类的编程语言来计算值。

JavaScript stores 10^100 as 1e+101 , is there a way to do that in the database? JavaScript将10^100存储为1e+101 ,是否可以在数据库中执行此操作? The numbers would not often be that large, but I would like to do calculations with data such as 10^-34 * 2^16 or whatever, so the database should (I think) be storing these as numbers... 数字通常不会那么大,但是我想用10^-34 * 2^16数据进行计算,因此数据库(我认为)应该将它们存储为数字...

How does this work? 这是如何运作的? How do you store numbers of this scale such that you can run computations with them? 您如何存储这种规模的数字,以便可以使用它们进行计算?

By "the database", I'm thinking in general. 通过“数据库”,我总体上在考虑。 I am messing around with MongoDB and Neo4j currently. 我现在正在搞弄MongoDB和Neo4j。

Databases themselves don't support numbers of arbitrary size in a native numeric format. 数据库本身不支持本机数字格式的任意大小的数字。 Your general upper limit on numeric types is usually 8 bytes, which isn't anywhere near a googol. 您对数字类型的一般上限通常为8个字节,这与googol差不多。

You'll have to store the number either as a string (least efficient, easiest to work with, can be as precise as needed), as a byte array of arbitrary length (more efficient, harder to work with, still arbitrary precision), or in scientific notation (most efficient, harder to work with, and limited precision). 您必须将数字存储为字符串(效率最低,最容易使用,可以根据需要精确设置),任意长度的字节数组(效率更高,更难使用,精度仍然任意),或以科学计数法表示(效率最高,更难使用且精度有限)。

The first two, unfortunately, do eliminate the possibility of doing any server-side computation, since there wouldn't be a native numeric type that could support the range of valid values. 不幸的是,前两种方法确实消除了进行任何服务器端计算的可能性,因为不会有本机数字类型可以支持有效值范围。 All of the computation would have to be done client-side using a suitable numeric type. 所有计算都必须使用适当的数字类型在客户端进行。

If I were you, I'd separate the numerical value from the exponent. 如果我是你,我会将数值与指数分开。 I personally don't have experience with MongoDB or Neo4j, but in MySQL (I'm sure they have similar terms) I'd create a table with an VARCHAR (text) column with whatever precision you'd like in your program (or how many unique numbers), and another VARCHAR column with length 3 (for max exponent 999). 我个人没有使用MongoDB或Neo4j的经验,但是在MySQL中(我敢肯定它们具有类似的术语),我将创建一个带有VARCHAR(文本)列的表,并以您希望在程序中使用的精度(或多少个唯一数字),以及另一个长度为3(最大指数999)的VARCHAR列。 You can tinker with the values as you see fit, but that's all I can think of. 您可以随意调整值,但这就是我能想到的。 If you want more flexible size values, I'd store the numbers on the server's file system using PHP rather than use databases. 如果您想要更灵活的大小值,我会使用PHP而不是使用数据库将数字存储在服​​务器的文件系统上。

You could use the double type. 您可以使用double类型。

The MySQL DOUBLE[(M,D)] MySQL DOUBLE [(M,D)]

A normal-size (double-precision) floating-point number. 普通大小(双精度)浮点数。 Permissible values are -1.7976931348623157E+308 to -2.2250738585072014E-308, 0, and 2.2250738585072014E-308 to 1.7976931348623157E+308. 允许值为-1.7976931348623157E + 308至-2.2250738585072014E-308、0和2.2250738585072014E-308至1.7976931348623157E + 308。 These are the theoretical limits, based on the IEEE standard. 这些是基于IEEE标准的理论限制。 The actual range might be slightly smaller depending on your hardware or operating system. 实际范围可能会略小,具体取决于您的硬件或操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM