-
Notifications
You must be signed in to change notification settings - Fork 23
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a7e9b19
Barebones docs for math primitives
aumetra 0890c14
Update src/pages/core/standard-library/math/integers.mdx
aumetra 02aece2
Add links to items in table
aumetra 6c2d7c9
List signed versions, add links, add disclaimer
aumetra 5109580
Mention jq and the general issue
aumetra fd14d41
Remove filler word
aumetra 9c7fb69
Move callout one sentence up
aumetra 1979640
Add note to integer page
aumetra 92345e1
native -> primitive
aumetra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"decimals": "Decimals", | ||
"integers": "Integers" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
aumetra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
)) | ||
} | ||
</tbody> | ||
</table> | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.