### 1 unstable release

0.4.3 | Aug 18, 2023 |
---|

#**1754** in Math

Used in **2** crates
(via astroport-liquidity-helpe…)

**MIT/Apache**

270KB

7K
SLoC

`cw-bigint`

`cw-bigint`

This is a forked version of rust-num/num-bigint with float operations removed, so that it can be used in smart contracts.

###
`lib.rs`

:

A Big integer (signed version:

, unsigned version: `BigInt`

).`BigUint`

A

is represented as a vector of `BigUint`

s.
A `BigDigit`

is a combination of `BigInt`

and `BigUint`

.`Sign`

Common numerical operations are overloaded, so we can treat them the same way we treat other numbers.

## Example

`use` `num_bigint``::`BigUint`;`
`use` `num_traits``::``{`Zero`,` One`}``;`
`use` `std``::``mem``::`replace`;`
`//` Calculate large fibonacci numbers.
`fn` `fib``(``n``:` `usize``)`` ``->` BigUint `{`
`let` `mut` f0`:` BigUint `=` `Zero``::`zero`(``)``;`
`let` `mut` f1`:` BigUint `=` `One``::`one`(``)``;`
`for` `_` `in` `0``..`n `{`
`let` f2 `=` f0 `+` `&`f1`;`
`//` This is a low cost way of swapping f0 with f1 and f1 with f2.
f0 `=` `replace``(``&``mut` f1`,` f2`)``;`
`}`
f0
`}`
`//` This is a very large number.
`println!``(``"`fib(1000) = `{}``"``,` `fib``(``1000``)``)``;`

It's easy to generate large random numbers:

`use` `num_bigint``::``{`ToBigInt`,` RandBigInt`}``;`
`let` `mut` rng `=` `rand``::`thread_rng`(``)``;`
`let` a `=` rng`.``gen_bigint``(``1000``)``;`
`let` low `=` `-``10000.``to_bigint``(``)``.``unwrap``(``)``;`
`let` high `=` `10000.``to_bigint``(``)``.``unwrap``(``)``;`
`let` b `=` rng`.``gen_bigint_range``(``&`low`,` `&`high`)``;`
`//` Probably an even larger number.
`println!``(``"``{}``"``,` a `*` b`)``;`

See the "Features" section for instructions for enabling random number generation.

## Features

The

crate feature is enabled by default, and is mandatory before Rust
1.36 and the stabilized `std`

crate. If you depend on `alloc`

with
`num-bigint`

, you must manually enable the `default -features = false`

`std`

feature yourself
if your compiler is not new enough.### Random Generation

supports the generation of random big integers when the `num-bigint`

feature is enabled. To enable it include rand as`rand`

`rand = "0.8"
num-bigint = { version = "0.4", features = ["rand"] }
`

Note that you must use the version of

that `rand`

is compatible
with: `num-bigint`

.`0.``8`

## Compatibility

The

crate is tested for rustc 1.31 and greater.`num-bigint`

#### Dependencies

~140–600KB

~11K SLoC