https://github.com/kotet/bigfixed
Arbitrary-precision fixed point number that using std.bigint internally
https://github.com/kotet/bigfixed
dlang dub fixed-point
Last synced: 4 months ago
JSON representation
Arbitrary-precision fixed point number that using std.bigint internally
- Host: GitHub
- URL: https://github.com/kotet/bigfixed
- Owner: kotet
- License: mit
- Created: 2017-05-28T05:51:53.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-13T21:19:53.000Z (almost 9 years ago)
- Last Synced: 2025-03-05T01:32:25.537Z (over 1 year ago)
- Topics: dlang, dub, fixed-point
- Language: D
- Size: 23.4 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bigfixed
[](https://travis-ci.org/kotet/bigfixed)
[](https://coveralls.io/github/kotet/bigfixed)
[](https://code.dlang.org/packages/bigfixed)
[](https://code.dlang.org/packages/bigfixed)
This module provides arbitrary precision fixed-point arithmetic.
```d
// Return the square root of `n` to `prec` decimal places by a method of bisection.
string sqrt(ulong n, size_t prec)
{
import std.conv : to;
import std.math : ceil, log10;
immutable size_t q = (prec / log10(2.0)).ceil.to!size_t() + 1;
auto low = BigFixed(0, q);
auto high = BigFixed(n, q);
while ((high - low) != high.resolution) // BigFixed is integer internally.
{
immutable BigFixed mid = (high + low) >> 1; // Shift Expressions can be used.
immutable bool isLess = (mid * mid) < n;
if (isLess)
{
low = mid;
}
else
{
high = mid;
}
}
return low.toDecimalString(prec);
}
// 10 digits before the 1,000th digit. See http://www.h2.dion.ne.jp/~dra/suu/chi2/heihoukon/2.html
immutable sqrt2_tail = "9518488472";
assert(sqrt(2, 1000)[$ - 10 .. $] == sqrt2_tail);
```
### Documentation
Run `dub fetch bigfixed && dub build bigfixed -b docs`.