Skip to content

Barebones documentation for math types #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/lib/_decimals.js
Original file line number Diff line number Diff line change
@@ -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 };
6 changes: 5 additions & 1 deletion src/pages/core/standard-library/math.mdx
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions src/pages/core/standard-library/math/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"decimals": "Decimals",
"integers": "Integers"
}
67 changes: 67 additions & 0 deletions src/pages/core/standard-library/math/decimals.mdx
Original file line number Diff line number Diff line change
@@ -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.

<Callout>
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.

</Callout>

Decimal types come in the following variants:

import { prepareName, prepareLink } from "@/lib/_decimals";

{ // Beware: Overengineering ahead!

<table>
<thead>
<tr>
<th></th>
<th>Unsigned</th>
<th>Signed</th>
</tr>
</thead>
<tbody>
{

[ '128', '256' ]
.map(item => (
<tr key={item}>
<td>{item} bit</td>
<td>
<a href={prepareLink(false, item)}>
<code>{prepareName(false, item)}</code>
</a>
</td>
<td>
<a href={prepareLink(true, item)}>
<code>{prepareName(true, item)}</code>
</a>
</td>
</tr>
))
}
</tbody>
</table>

}

Choose one of the types according to your needs and off you go!
65 changes: 65 additions & 0 deletions src/pages/core/standard-library/math/integers.mdx
Original file line number Diff line number Diff line change
@@ -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.

<Callout>
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.
</Callout>

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!

<table>
<thead>
<tr>
<th></th>
<th>Unsigned</th>
<th>Signed</th>
</tr>
</thead>
<tbody>
{
[ '64', '128', '256', '512' ]
.map(item => (
<tr key={item}>
<td>{item} bit</td>
<td>
<a href={`https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.Uint${item}.html`}>
<code>Uint{item}</code>
</a>
</td>
<td>
<a href={`https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.Int${item}.html`}>
<code>Int{item}</code>
</a>
</td>
</tr>
))
}
</tbody>
</table>

}