Skip to content

Commit c7c478d

Browse files
committed
expose zero
1 parent 76b6850 commit c7c478d

3 files changed

Lines changed: 88 additions & 0 deletions

File tree

crates/float/src/js_api.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,29 @@ impl Float {
378378
Self::min_negative_value()
379379
}
380380

381+
/// Returns the zero value of a `Float` in its maximized representation.
382+
///
383+
/// # Returns
384+
///
385+
/// * `Ok(Float)` - The zero value.
386+
/// * `Err(FloatError)` - If the EVM call fails.
387+
///
388+
/// # Example
389+
///
390+
/// ```typescript
391+
/// const zeroResult = Float.zero();
392+
/// if (zeroResult.error) {
393+
/// console.error(zeroResult.error);
394+
/// }
395+
/// const zero = zeroResult.value;
396+
/// assert(zero.isZero().value);
397+
/// assert(zero.format().value === "0");
398+
/// ```
399+
#[wasm_export(js_name = "zero", preserve_js_class)]
400+
pub fn zero_js() -> Result<Float, FloatError> {
401+
Self::zero()
402+
}
403+
381404
/// Formats the float as a decimal string.
382405
///
383406
/// # Returns

crates/float/src/lib.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,37 @@ impl Float {
465465
})
466466
}
467467

468+
/// Returns the zero value of a `Float` in its maximized representation.
469+
///
470+
/// # Returns
471+
///
472+
/// * `Ok(Float)` - The zero value.
473+
/// * `Err(FloatError)` - If the EVM call fails.
474+
///
475+
/// # Example
476+
///
477+
/// ```
478+
/// use rain_math_float::Float;
479+
///
480+
/// let zero = Float::zero()?;
481+
/// assert!(zero.is_zero()?);
482+
/// assert_eq!(zero.format()?, "0");
483+
///
484+
/// // Should be equal to parsed zero
485+
/// let parsed_zero = Float::parse("0".to_string())?;
486+
/// assert!(zero.eq(parsed_zero)?);
487+
///
488+
/// anyhow::Ok(())
489+
/// ```
490+
pub fn zero() -> Result<Self, FloatError> {
491+
let calldata = DecimalFloat::zeroCall {}.abi_encode();
492+
493+
execute_call(Bytes::from(calldata), |output| {
494+
let decoded = DecimalFloat::zeroCall::abi_decode_returns(output.as_ref())?;
495+
Ok(Float(decoded))
496+
})
497+
}
498+
468499
/// Formats the float as a decimal string with a default significant figures limit of 18.
469500
///
470501
/// # Returns
@@ -1099,6 +1130,20 @@ mod tests {
10991130
assert!(zero.eq(Float::default()).unwrap());
11001131
}
11011132

1133+
#[test]
1134+
fn test_zero() {
1135+
let zero = Float::zero().unwrap();
1136+
assert!(zero.is_zero().unwrap());
1137+
assert_eq!(zero.format().unwrap(), "0");
1138+
1139+
// Test that zero equals parsed zero
1140+
let parsed_zero = Float::parse("0".to_string()).unwrap();
1141+
assert!(zero.eq(parsed_zero).unwrap());
1142+
1143+
// Test that zero equals default
1144+
assert!(zero.eq(Float::default()).unwrap());
1145+
}
1146+
11021147
prop_compose! {
11031148
fn arb_float()(
11041149
coefficient in any::<I224>(),

test_js/float.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,26 @@ describe('Test Float Bindings', () => {
110110
expect(a.add(b)?.value!.format()?.value!).toBe('0');
111111
});
112112

113+
it('should test zero constant', () => {
114+
// Test the zero function
115+
const zeroResult = Float.zero();
116+
expect(zeroResult.error).toBeUndefined();
117+
118+
const zero = zeroResult.value!;
119+
120+
// Test that zero is actually zero
121+
expect(zero.isZero()?.value!).toBe(true);
122+
expect(zero.format()?.value!).toBe('0');
123+
124+
// Test that zero equals parsed zero
125+
const parsedZero = Float.parse('0')?.value!;
126+
expect(zero.eq(parsedZero)?.value!).toBe(true);
127+
128+
// Test that zero equals bigint zero
129+
const bigintZero = Float.fromBigint(0n);
130+
expect(zero.eq(bigintZero)?.value!).toBe(true);
131+
});
132+
113133
it('should test float constants', () => {
114134
// Test that all constant methods return valid floats
115135
const maxPosResult = Float.maxPositiveValue();

0 commit comments

Comments
 (0)