开发者

Mapping a BigInteger to a circle

开发者 https://www.devze.com 2022-12-28 05:55 出处:网络
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?

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 numbe开发者_StackOverflow社区r;
angle = (number / pow(2, 160)) * TwoPi;

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消