简体   繁体   中英

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.

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. 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. I don't know any C# or .NET, but perhaps similar functions are available there as well.

If you use double for the exponent as well, you'd theoretically really big numbers. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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