将 BigInteger 映射到圆

发布于 2024-08-30 23:28:48 字数 233 浏览 6 评论 0原文

我有一个使用 160 位数字的 C# 系统,存储在 BigInteger 中。我想将这些东西显示在一个圆圈上,这意味着将 0->2^160 范围映射到 0->2Pi 范围。我该怎么做?

然而,这种方法

BigInteger number;
angle = (number / pow(2, 160)) * TwoPi;

很复杂,因为除法会将结果截断为整数。

I have a C# system using 160 bit numbers, stored in a BigInteger. I want to display these things on a circle, which means mapping the 0->2^160 range into the 0->2Pi range. How would I do this?

The approach that jumps instantly to mind is

BigInteger number;
angle = (number / pow(2, 160)) * TwoPi;

However, that has complexities because the division will truncate the result into an integer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高速公鹿 2024-09-06 23:28:48

好吧,再说一遍,从头开始。

由于您的 BigInteger 是从 0 -> 2^160 它比 double 小,double 可以包含 10^(-308) 到 10^(+308)。

有一个从 BigInteger 到 double 的显式转换

所以你这样做:

BigInteger number;
var angle = ((double)number / Math.Pow(2, 160)) * TwoPi;

我确实知道你会失去精度,但这在圆上应该不重要。

Ok, again, from the start.

Since your BigInteger is from 0 -> 2^160 it's smaller than a double, which can contain from 10^(-308) to 10^(+308).

There is an explicit conversion from BigInteger to double.

So you do this:

BigInteger number;
var angle = ((double)number / Math.Pow(2, 160)) * TwoPi;

I do know that you'll lose precision, but that shouldn't matter on the circle.

幸福不弃 2024-09-06 23:28:48

我对 C# 或其大整数一无所知,所以这里是一个黑暗中的刺:

除非你的显示器大约有一个(圆形)足球场的大小,否则你将不得不接受你的显示器的精度将远低于需要显示仅相距 1 的数字之间的任何分隔(或 10 或 100 或 10000000 甚至 10^40,但你必须弄清楚)。

我会简单地截断我的大整数,取最高阶 32 位并将它们视为无符号整数,然后将其除以 2^32 将其带入范围 [0,1) (将其转换为到我除法时的浮点数)并将其绘制在圆的远处。

我猜想截断大整数以获得最左边的 32 位相当于将其除以 2^128 但可能有更好的位移方法,或者您可以直接获取这些位。

I know nothing of C# or its big integers, so here's a stab in the dark:

Unless your display is about the size of a (round) football field you are going to have to accept that the precision of your display will be much less than is required to show any separation between numbers which are only 1 apart (or 10 or 100 or 10000000 or even 10^40 but you have to figure that out).

I would simply truncate my big integer, take the highest order 32 bits and treat them as an unsigned integer, then divide it by 2^32 to bring it into the range [0,1) (converting it to floating-point as I divide) and plot it that far round the circle.

I guess truncating the big integer to get the leftmost 32 bits is equivalent to dividing it by 2^128 but there might be a better bit-shifting approach or you may be able to simply grab the bits directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文