[英]What's a good way to store extremely large numbers? (Eg. 572e6561) in C#
My first thought was to implement a class which stores the coefficient as a double, and the 10^X as an int/short. 我的第一个想法是实现一个将系数存储为双精度值并将10 ^ X存储为int / short的类。
I want to use engineering notation over scientific notation because it's simpler for end-users. 我想使用工程表示法而不是科学表示法,因为对于最终用户而言,它更简单。
If there are projects for doing this, I can't seem to find them. 如果有用于执行此操作的项目,我似乎找不到它们。
I know System.Numerics.BigInteger
exists, but it's pretty slow. 我知道System.Numerics.BigInteger
存在,但是速度很慢。 I really want high speed for performing many calculations on mobile devices. 我真的想在移动设备上执行许多计算时要有很高的速度。 I just want to store numbers as engineering notation. 我只想将数字存储为工程符号。
Any ideas? 有任何想法吗?
If you don't need a lot of precision, just large magnitudes, then I'd say use two values to represent mantissa and exponent. 如果您不需要很高的精度(仅大幅度),那么我想说两个值代表尾数和指数。 Using integers for both would be closer to how floating point numbers are handled internally. 两者都使用整数将更接近于内部如何处理浮点数。 But using floating point at least for the mantissa would probably make it easier to implement many of the operations, at the cost of a few bits of mantissa precision. 但是,至少对尾数使用浮点数可能会使得更容易实现许多操作,但要花费一些尾数精度。
C has functions like frexp and ldexp to separately deal with mantissa and exponent of a floating point number. C具有frexp和ldexp之类的功能 ,可分别处理尾数和浮点数的指数。 I don't know any C# or .NET, but perhaps similar functions are available there as well. 我不知道任何C#或.NET,但也许那里也有类似的功能。
If you use double for the exponent as well, you'd theoretically really big numbers. 如果您也对指数使用double,那么从理论上讲您将是一个很大的数字。 But if that exponent isn't exact, due to rounding, then you don't know the exponent at all, which makes the mantissa worthless. 但是,如果由于舍入而导致该指数不精确,那么您根本就不会知道该指数,这会使尾数一文不值。 So if you need numbers big enough to consider such things, you either need to use some big integer for the exponent, or can drop the mantissa completely and just express those big numbers as powers of two with an approximate exponent. 因此,如果您需要足够大的数字来考虑这样的事情,则要么需要对指数使用某个大整数,要么可以完全舍弃尾数,然后将这些大数字表示为具有近似指数的2的幂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.