存储大量数字的好方法是什么? (例如572e6561)在C#中 [英] What's a good way to store extremely large numbers? (Eg. 572e6561) in C#

查看:64
本文介绍了存储大量数字的好方法是什么? (例如572e6561)在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个想法是实现一个将系数存储为double值并将10 ^ X存储为int/short值的类.

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.

我知道System.Numerics.BigInteger存在,但是非常慢.我真的想在移动设备上执行许多计算时要有很高的速度. 我只想将数字存储为工程符号.

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.

有什么想法吗?

推荐答案

如果您不需要很高的精度,只需要很大的幅度,那么我会说使用两个值来表示尾数和指数.两者都使用整数将更接近于内部如何处理浮点数.但是,至少对于尾数使用浮点数可能会使得更容易实现许多运算,但要花费一些尾数精度.

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具有类似 frexp

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.

如果您也对指数使用double,则理论上您会确实获得大数.但是,如果由于四舍五入而导致该指数不精确,那么您根本就不会知道该指数,这会使尾数一文不值.因此,如果您需要足够大的数字来考虑这些事情,则要么需要为指数使用某个大整数,要么可以完全舍弃尾数,然后将这些大数字表示为具有近似指数的2的幂.

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.

这篇关于存储大量数字的好方法是什么? (例如572e6561)在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆