Skip to content
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

docs: update wad to ray conversion #42

Merged
merged 6 commits into from
Oct 18, 2024
Merged
Changes from 5 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
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ where both `a` and `b` are of the same type.

### Conversion

#### Type Casting

Any type that can be converted to a `u128` via the `Into` trait can similarly be converted to a `Wad` or `Ray` via the `Into` trait.

Additionally, the following conversions from integer types are supported for `SignedWad` and `SignedRay` via the `Into` trait`:
Expand All @@ -94,18 +96,37 @@ The following conversions from this library's types can also be performed via th
- `Wad` -> `felt252`: Convert a `Wad` to a `felt252` without modifying the value
- `Wad` -> `u256`: Convert a `Wad` to a `u256` without modifying the value
- `Wad` -> `SignedWad`: Convert a `Wad` to a `SignedWad` without modifying the value, with the `sign` set to `false`
- `Wad` -> `SignedRay`: Multiply the `Wad` by 10<sup>9</sup> and return a `SignedRay` with the `sign` set to `false`
- `Ray` -> `Wad`: Divide the `Ray` value by 10<sup>9</sup> and return a `Wad`
- `Ray` -> `SignedRay`: Convert a `Ray` to a `SignedRay` without modifying the value, with the `sign` set to `false`
- `SignedWad` -> `felt252`: Convert a `SignedWad` to a `felt252` without modifying the value
- `SignedRay` -> `felt252`: Convert a `SignedRay` to a `felt252` without modifying the value

The following conversions can be performed via the `TryInto` trait:
- `Wad` -> `Option::<Ray>`: Multiply the `Wad` value by 10<sup>9</sup> and return `Option::Some<Ray>` if there is no overflow or `Option::None` otherwise
- `u256` -> `Option::<Wad>`: Returns `Option::Some<Wad>` if the value is within bounds of `u128` or `Option::None` otherwise
- `SignedWad` -> `Option::<Wad>`: Returns `Option::Some<Wad>` if `sign` is false or `Option::None` otherwise
- `SignedRay` -> `Option::<Ray>`: Returns `Option::Some<Ray>` if `sign` is false or `Option::None` otherwise

#### Scaling
tserg marked this conversation as resolved.
Show resolved Hide resolved

The following functions can be used to scale between `Wad` and `Ray`:
- `fn ray_to_wad(x: Ray) -> Wad`: Divide the `Ray` value by 10<sup>9</sup> and return a `Wad`
- `fn wad_to_ray(x: Wad) -> Option::<Ray>`: Multiply the `Wad` value by 10<sup>9</sup> and return `Option::Some<Ray>` if there is no overflow or `Option::None` otherwise

#### Additional notes on conversion between `Wad` and `Ray`

##### Overview

Starting from `v0.4.1` of this library, we have made significant changes to how `Wad` values are converted to `Ray` values and vice versa. This aims to improve type safety and align with the semantics of Rust's `Into` trait.
tserg marked this conversation as resolved.
Show resolved Hide resolved

##### Key Changes

1. Previously, `Into<Wad, Ray>` scaled the value by 10<sup>9</sup> while `Into<Ray, Wad>` scaled the value by 10<sup>-9</sup>. Both now perform direct type cast without value modification
2. Introduced `wad_to_ray()` and `ray_to_wad()` functions for value-preserving conversions

##### Recommended Usage

1. Prefer `wad_to_ray()` and `ray_to_wad()` for most conversions between `Wad` and `Ray`.
2. Use the `Into` trait only when a simple type cast is required (expected to be rare).

### Signed

The following functions are available for `SignedWad` and `SignedRay` via the `Signed` trait:
Expand All @@ -118,7 +139,7 @@ To use this library, add the repository as a dependency in your `Scarb.toml`:

```
[dependencies]
wadray = { git = "https://github.com/lindy-labs/wadray.git" }
wadray = "0.4.1"
```
then add the following line in your `.cairo` file
```cairo
Expand Down