diff --git a/src/lib/_decimals.js b/src/lib/_decimals.js new file mode 100644 index 00000000..32ad8671 --- /dev/null +++ b/src/lib/_decimals.js @@ -0,0 +1,18 @@ +function prepareName(isSigned, bits) { + let name = "Decimal"; + if (bits != "128") { + name += bits; + } + + if (isSigned) { + name = `Signed${name}`; + } + + return name; +} + +function prepareLink(isSigned, bits) { + return `https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.${prepareName(isSigned, bits)}.html`; +} + +export { prepareName, prepareLink }; diff --git a/src/pages/core/standard-library/math.mdx b/src/pages/core/standard-library/math.mdx index 53c3ecca..15c7d595 100644 --- a/src/pages/core/standard-library/math.mdx +++ b/src/pages/core/standard-library/math.mdx @@ -1,7 +1,11 @@ --- -tags: ["core"] +tags: ["core", "math"] --- import { Callout } from "nextra/components"; # Math + +CosmWasm offers mathematical primitives for, you guessed it, mathematical +operations. In contrast to the Rust standard library, which is limited to 128 +bit integers, we offer integers that exceed that precision. diff --git a/src/pages/core/standard-library/math/_meta.json b/src/pages/core/standard-library/math/_meta.json new file mode 100644 index 00000000..fbd733e6 --- /dev/null +++ b/src/pages/core/standard-library/math/_meta.json @@ -0,0 +1,4 @@ +{ + "decimals": "Decimals", + "integers": "Integers" +} diff --git a/src/pages/core/standard-library/math/decimals.mdx b/src/pages/core/standard-library/math/decimals.mdx new file mode 100644 index 00000000..92cb15ce --- /dev/null +++ b/src/pages/core/standard-library/math/decimals.mdx @@ -0,0 +1,67 @@ +--- +tags: ["core", "math"] +--- + +import { Callout } from "nextra/components"; + +# Decimals + +CosmWasm offers decimal types for handling fractional numbers. + +In contrast to floating point numbers, these datatypes do not suffer from the +same precision issues. This means that these decimal types are safe for use in +financial calculations. + +Instead of storing numbers in the floating point format, which gives it a +_floating_ amount of decimal places, these decimal types have a fixed precision +of 18 decimal places. + + + Note that, due to how the decimal types are structured, the value ranges can seem a little weird. + + For example, 1.0 is represented by `Decimal(1_000_000_000_000_000_000)` and the maximal value of the 128 bit variant is `340282366920938463463.374607431768211455` (which is `(2^128 - 1) / 10^18`) + + In practice, you don't really have to think about it, but it's something you should be aware of. + + + +Decimal types come in the following variants: + +import { prepareName, prepareLink } from "@/lib/_decimals"; + +{ // Beware: Overengineering ahead! + + + + + + + + + + + { + + [ '128', '256' ] + .map(item => ( + + + + + + )) + } + +
UnsignedSigned
{item} bit + + {prepareName(false, item)} + + + + {prepareName(true, item)} + +
+ +} + +Choose one of the types according to your needs and off you go! diff --git a/src/pages/core/standard-library/math/integers.mdx b/src/pages/core/standard-library/math/integers.mdx new file mode 100644 index 00000000..b3755b07 --- /dev/null +++ b/src/pages/core/standard-library/math/integers.mdx @@ -0,0 +1,65 @@ +--- +tags: ["core", "math"] +--- + +import { Callout } from "nextra/components"; + +# Integers + +CosmWasm offers integer types starting at 64 bit precision, all the way up to +512 bits. + +Reasoning behind wrapping the primitive 64 bit type is the interaction with, for +example, JavaScript (same goes for parsers like `jq` or any parser that parses +numbers into double precision floats). A `u64` would normally serialize into a +regular integer field in JSON. The issue here is that, due to representing +integers in form of double precision floats, JavaScript can only handle up to +~53 bit numbers without potentially losing information. + + + There is nothing wrong with using the primitive 64- and 128-bit types in your + contract for calculations. The issues mentioned here can only happen when you + de-/serialize these values. + + +Our wrapper fixes this by serializing numbers as strings, letting JavaScript +clients deserialize these numbers into their respective BigInteger types. + +The other integer types, which reach even higher amounts of precision, are +serialized in the same way. + +Integers come in the following variants: + +{ // Beware: Overengineering ahead! + + + + + + + + + + + { + [ '64', '128', '256', '512' ] + .map(item => ( + + + + + + )) + } + +
UnsignedSigned
{item} bit + + Uint{item} + + + + Int{item} + +
+ +}