Skip to content

Commit

Permalink
Update basic-arithmetic.mdx
Browse files Browse the repository at this point in the history
voice/grammar tweaks
  • Loading branch information
max-crawford authored Aug 19, 2024
1 parent 8588b7e commit 8a8a64e
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions content/docs/stacks/clarity/basic-arithmetic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Smart contracts often need to perform calculations, whether it's for token balan
## Why these functions matter
Clarity's arithmetic functions are designed with blockchain-specific considerations in mind:

1. Overflow Protection: Unlike some languages, Clarity prevents integer overflow by default, enhancing contract security.
1. Overflow protection: Unlike some languages, Clarity prevents integer overflow by default, enhancing contract security.
2. Precision: Clarity uses 128-bit integers, allowing for high-precision calculations crucial in financial applications.
3. Determinism: The behavior of these functions is consistent across all nodes, ensuring blockchain consensus.

Expand All @@ -22,11 +22,11 @@ Clarity's arithmetic functions are designed with blockchain-specific considerati

**When**: Use when you need to increase values, combine quantities, or perform any additive calculation.

**Best Practices**:
**Best practices**:
- Consider overflow protection
- Use with uint for non-negative values like token amounts

**Example Use Case**: Calculating total rewards in a stacking system.
**Example use case**: Calculating total rewards in a stacking system.

```clarity
(define-map StackingRewards principal uint)
Expand All @@ -50,11 +50,11 @@ Clarity's arithmetic functions are designed with blockchain-specific considerati

**When**: Use when you need to decrease values, calculate differences, or perform any subtractive operation.

**Best Practices**:
**Best practices**:
- Guard against underflow
- Consider using uint for values that shouldn't go negative

**Example Use Case**: Updating user points in a rewards system.
**Example use case**: Updating user points in a rewards system.

```clarity
(define-map UserPoints principal uint)
Expand Down Expand Up @@ -83,11 +83,11 @@ Clarity's arithmetic functions are designed with blockchain-specific considerati

**When**: Use when you need to scale values, calculate rates, or perform any multiplicative operation.

**Best Practices**:
**Best practices**:
- Consider overflow protection
- Use with uint for non-negative values like token amounts

**Example Use Case**: Calculating rewards based on stacking amount and duration.
**Example use case**: Calculating rewards based on stacking amount and duration.

```clarity
(define-public (calculate-rewards (amount uint) (days uint))
Expand All @@ -109,11 +109,11 @@ Clarity's arithmetic functions are designed with blockchain-specific considerati

**When**: Use when you need to divide values, calculate rates, or perform any division operation.

**Best Practices**:
**Best practices**:
- Guard against division by zero
- Consider using uint for non-negative values like token amounts

**Example Use Case**: Calculating price per item when buying in bulk.
**Example use case**: Calculating price per item when buying in bulk.

```clarity
(define-read-only (calculate-price-per-item (totalPrice uint) (itemCount uint))
Expand All @@ -124,19 +124,19 @@ Clarity's arithmetic functions are designed with blockchain-specific considerati
)
```

## Best Practices and Considerations
## Best practices and considerations

1. **Order of Operations**: Clarity doesn't have operator precedence. Use parentheses to explicitly define the order of operations.
1. **Order of operations**: Clarity doesn't have operator precedence. Use parentheses to explicitly define the order of operations.

2. **Handling Remainders**: When using division, consider how to handle remainders. You might need to use combination of division and modulo.
2. **Handling remainders**: When using division, consider how to handle remainders. You might need to use combination of division and modulo.

3. **Scaling for Precision**: When dealing with percentages or fractions, consider scaling up your numbers to maintain precision.
3. **Scaling for precision**: When dealing with percentages or fractions, consider scaling up your numbers to maintain precision.

4. **Guarding Against Division by Zero**: Always check for zero before performing division to avoid runtime errors.
4. **Guarding against division by zero**: Always check for zero before performing division to avoid runtime errors.

5. **Using uint vs int**: Choose `uint` for values that can't be negative (like token amounts) and `int` when negative values are possible.

## Practical Example: Simple Interest Calculator
## Practical example: Simple interest calculator

Let's combine these functions to create a simple interest calculator:

Expand All @@ -157,4 +157,4 @@ This example demonstrates how to combine multiple arithmetic operations while ha

## Conclusion

Mastering Clarity's arithmetic functions is essential for building robust smart contracts. By understanding these operations and their nuances, you can implement complex financial logic, manage token economics, and create secure, efficient blockchain applications.
Mastering Clarity's arithmetic functions is essential for building robust smart contracts. By understanding these operations and their nuances, you can implement complex financial logic, manage token economics, and create secure, efficient blockchain applications.

0 comments on commit 8a8a64e

Please sign in to comment.