You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/stellar-contracts/utils/math/wad.mdx
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ of floating-point arithmetic in smart contracts.
44
44
45
45
## 1. Why 18 Decimals?
46
46
47
-
**18 decimals was chosen for several reasons:**
47
+
18 decimals was chosen for several reasons:
48
48
49
49
-**Battle-Tested Standard**: Most DeFi protocols on use 18 decimals
50
50
-**Sufficient Precision**: Provides precision down to 10^-18, far exceeding practical financial needs
@@ -83,7 +83,7 @@ pub struct Wad(i128);
83
83
84
84
## 3. No `From`/`Into` Traits
85
85
86
-
We deliberately **do not** implement `From<i128>` or `Into<i128>` because it's ambiguous:
86
+
We deliberately **DID NOT** implement `From<i128>` or `Into<i128>` because it's ambiguous:
87
87
88
88
```rust
89
89
// What should this mean?
@@ -127,10 +127,10 @@ Operator overloading is supported across WAD and native i128 types where unambig
127
127
128
128
**Overflow Behavior**
129
129
130
-
**Just like regular Rust**, operator overloading does not include overflow checks:
130
+
Just like regular Rust, operator overloading does not include overflow checks:
131
131
132
-
-**Use `checked_*` methods** (`checked_add()`, `checked_sub()`, `checked_mul()`, etc.) when handling user inputs or when overflow is possible. These return `Option<Wad>` for safe error handling.
133
-
-**Use operator overloads** (`+`, `-`, `*`, `/`) when you want to save gas by skipping overflow checks, or when you're confident the operation cannot overflow.
132
+
- Use `checked_*` methods (`checked_add()`, `checked_sub()`, `checked_mul()`, etc.) when handling user inputs or when overflow is possible. These return `Option<Wad>` for safe error handling.
133
+
- Use operator overloads (`+`, `-`, `*`, `/`) when you want to save gas by skipping overflow checks, or when you're confident the operation cannot overflow.
134
134
135
135
This design follows Rust's standard library pattern: operators for performance, checked methods for safety.
136
136
@@ -180,9 +180,9 @@ impl Div for Wad {
180
180
181
181
## Exponentiation
182
182
183
-
WAD supports raising a value to an **unsigned integer exponent** via `pow`.
183
+
WAD supports raising a value to an unsigned integer exponent via `pow`.
184
184
185
-
-`pow(&e, exponent)` is optimized using **exponentiation by squaring** (O(log n) multiplications).
185
+
-`pow(&e, exponent)` is optimized using exponentiation by squaring (O(log n) multiplications).
186
186
- Each multiplication keeps WAD semantics (fixed-point multiplication and truncation toward zero).
187
187
- Overflow is reported via Soroban errors.
188
188
@@ -223,7 +223,7 @@ let usdc_back: i128 = wad.to_token_amount(&e, 6);
223
223
224
224
## Understanding Fixed-Point Precision
225
225
226
-
**WAD is a fixed-point math library.** Like all fixed-point arithmetic systems,
226
+
WAD is a fixed-point math library. Like all fixed-point arithmetic systems,
227
227
precision loss is inherent and unavoidable. The goal is not to eliminate precision errors
228
228
—that's impossible— but to reduce them to a degree so minimal
229
229
that they become irrelevant in practical applications.
@@ -253,7 +253,7 @@ let result2 = a * (b * c); // Truncates after inner multiplication
253
253
- Errors are in the **10^-15 to 10^-18** range, far beyond practical significance
254
254
- Token precision (6-8 decimals) completely absorbs these errors when converting back
255
255
- Real-world financial systems round to 2-8 decimal places; WAD's 18 decimals provide a massive safety margin
256
-
- This is **orders of magnitude more precise** than needed for DeFi applications
256
+
- This is orders of magnitude more precise than needed for DeFi applications
0 commit comments