Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
74 changes: 37 additions & 37 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
DecimalFloatAbsTest:testAbsDeployed(bytes32) (runs: 5096, μ: 3611684, ~: 3611622)
DecimalFloatAddTest:testAddDeployed(bytes32,bytes32) (runs: 5096, μ: 3617219, ~: 3617323)
DecimalFloatCeilTest:testCeilDeployed(bytes32) (runs: 5096, μ: 3611687, ~: 3611285)
DecimalFloatConstantsTest:testEDeployed() (gas: 3610815)
DecimalFloatConstantsTest:testMaxNegativeValueDeployed() (gas: 3610804)
DecimalFloatConstantsTest:testMaxPositiveValueDeployed() (gas: 3610805)
DecimalFloatConstantsTest:testMinNegativeValueDeployed() (gas: 3610780)
DecimalFloatConstantsTest:testMinPositiveValueDeployed() (gas: 3610781)
DecimalFloatConstantsTest:testZeroDeployed() (gas: 3610848)
DecimalFloatDivTest:testDivDeployed(bytes32,bytes32) (runs: 5096, μ: 3618767, ~: 3618901)
DecimalFloatEqTest:testEqDeployed(bytes32,bytes32) (runs: 5096, μ: 3612016, ~: 3611940)
DecimalFloatFloorTest:testFloorDeployed(bytes32) (runs: 5096, μ: 3611475, ~: 3611283)
DecimalFloatFormatTest:testFormatDeployed(bytes32,uint256) (runs: 5096, μ: 3637333, ~: 3641988)
DecimalFloatFracTest:testFracDeployed(bytes32) (runs: 5096, μ: 3611869, ~: 3611833)
DecimalFloatFromFixedDecimalLosslessTest:testFromFixedDecimalLosslessDeployed(uint256,uint8) (runs: 5096, μ: 3612455, ~: 3612401)
DecimalFloatFromFixedDecimalLossyTest:testFromFixedDecimalLossyDeployed(uint256,uint8) (runs: 5096, μ: 3612932, ~: 3612868)
DecimalFloatGtTest:testGtDeployed(bytes32,bytes32) (runs: 5096, μ: 3611954, ~: 3611882)
DecimalFloatGteTest:testGteDeployed(bytes32,bytes32) (runs: 5096, μ: 3611941, ~: 3611868)
DecimalFloatInvTest:testInvDeployed(bytes32) (runs: 5096, μ: 3617158, ~: 3617217)
DecimalFloatIsZeroTest:testIsZeroDeployed(bytes32) (runs: 5096, μ: 3611125, ~: 3611125)
DecimalFloatLtTest:testLtDeployed(bytes32,bytes32) (runs: 5096, μ: 3611931, ~: 3611859)
DecimalFloatLteTest:testLteDeployed(bytes32,bytes32) (runs: 5096, μ: 3611985, ~: 3611912)
DecimalFloatMaxTest:testMaxDeployed(bytes32,bytes32) (runs: 5096, μ: 3611994, ~: 3611934)
DecimalFloatMinTest:testMinDeployed(bytes32,bytes32) (runs: 5096, μ: 3611992, ~: 3611932)
DecimalFloatMinusTest:testMinusDeployed(bytes32) (runs: 5096, μ: 3611790, ~: 3611791)
DecimalFloatMulTest:testMulDeployed(bytes32,bytes32) (runs: 5096, μ: 3615714, ~: 3616500)
DecimalFloatAbsTest:testAbsDeployed(bytes32) (runs: 5096, μ: 3678691, ~: 3678629)
DecimalFloatAddTest:testAddDeployed(bytes32,bytes32) (runs: 5096, μ: 3684124, ~: 3684193)
DecimalFloatCeilTest:testCeilDeployed(bytes32) (runs: 5096, μ: 3678607, ~: 3678205)
DecimalFloatConstantsTest:testEDeployed() (gas: 3677779)
DecimalFloatConstantsTest:testMaxNegativeValueDeployed() (gas: 3677769)
DecimalFloatConstantsTest:testMaxPositiveValueDeployed() (gas: 3677770)
DecimalFloatConstantsTest:testMinNegativeValueDeployed() (gas: 3677745)
DecimalFloatConstantsTest:testMinPositiveValueDeployed() (gas: 3677768)
DecimalFloatConstantsTest:testZeroDeployed() (gas: 3677790)
DecimalFloatDivTest:testDivDeployed(bytes32,bytes32) (runs: 5096, μ: 3685746, ~: 3685875)
DecimalFloatEqTest:testEqDeployed(bytes32,bytes32) (runs: 5096, μ: 3678981, ~: 3678905)
DecimalFloatFloorTest:testFloorDeployed(bytes32) (runs: 5096, μ: 3678442, ~: 3678248)
DecimalFloatFormatTest:testFormatConstants() (gas: 3678706)
DecimalFloatFormatTest:testFormatDeployed(bytes32,bytes32,bytes32) (runs: 2549, μ: 3704560, ~: 3710763)
DecimalFloatFracTest:testFracDeployed(bytes32) (runs: 5096, μ: 3678812, ~: 3678775)
DecimalFloatFromFixedDecimalLosslessTest:testFromFixedDecimalLosslessDeployed(uint256,uint8) (runs: 5096, μ: 3679399, ~: 3679343)
DecimalFloatFromFixedDecimalLossyTest:testFromFixedDecimalLossyDeployed(uint256,uint8) (runs: 5096, μ: 3679904, ~: 3679832)
DecimalFloatGtTest:testGtDeployed(bytes32,bytes32) (runs: 5096, μ: 3678878, ~: 3678802)
DecimalFloatGteTest:testGteDeployed(bytes32,bytes32) (runs: 5096, μ: 3678952, ~: 3678875)
DecimalFloatInvTest:testInvDeployed(bytes32) (runs: 5096, μ: 3684106, ~: 3684163)
DecimalFloatIsZeroTest:testIsZeroDeployed(bytes32) (runs: 5096, μ: 3678112, ~: 3678112)
DecimalFloatLtTest:testLtDeployed(bytes32,bytes32) (runs: 5096, μ: 3678855, ~: 3678779)
DecimalFloatLteTest:testLteDeployed(bytes32,bytes32) (runs: 5096, μ: 3678909, ~: 3678832)
DecimalFloatMaxTest:testMaxDeployed(bytes32,bytes32) (runs: 5096, μ: 3678962, ~: 3678898)
DecimalFloatMinTest:testMinDeployed(bytes32,bytes32) (runs: 5096, μ: 3678937, ~: 3678874)
DecimalFloatMinusTest:testMinusDeployed(bytes32) (runs: 5096, μ: 3678732, ~: 3678733)
DecimalFloatMulTest:testMulDeployed(bytes32,bytes32) (runs: 5096, μ: 3682723, ~: 3683509)
DecimalFloatPackLosslessTest:testPackDeployed(int224,int32) (runs: 5096, μ: 170044, ~: 170045)
DecimalFloatParseTest:testParseDeployed(string) (runs: 5096, μ: 3614363, ~: 3614232)
DecimalFloatPowTest:testPowDeployed(bytes32,bytes32) (runs: 5096, μ: 3663866, ~: 3641157)
DecimalFloatSqrtTest:testSqrtDeployed(bytes32) (runs: 5096, μ: 3628101, ~: 3629825)
DecimalFloatSubTest:testSubDeployed(bytes32,bytes32) (runs: 5096, μ: 3617533, ~: 3617568)
DecimalFloatToFixedDecimalLosslessTest:testToFixedDecimalLosslessDeployed(bytes32,uint8) (runs: 5096, μ: 3613070, ~: 3612964)
DecimalFloatToFixedDecimalLossyTest:testToFixedDecimalLossyDeployed(bytes32,uint8) (runs: 5096, μ: 3613141, ~: 3613448)
DecimalFloatParseTest:testParseDeployed(string) (runs: 5096, μ: 3681394, ~: 3681262)
DecimalFloatPowTest:testPowDeployed(bytes32,bytes32) (runs: 5096, μ: 3727410, ~: 3708099)
DecimalFloatSqrtTest:testSqrtDeployed(bytes32) (runs: 5096, μ: 3695326, ~: 3696789)
DecimalFloatSubTest:testSubDeployed(bytes32,bytes32) (runs: 5096, μ: 3684442, ~: 3684518)
DecimalFloatToFixedDecimalLosslessTest:testToFixedDecimalLosslessDeployed(bytes32,uint8) (runs: 5096, μ: 3680040, ~: 3679950)
DecimalFloatToFixedDecimalLossyTest:testToFixedDecimalLossyDeployed(bytes32,uint8) (runs: 5096, μ: 3680113, ~: 3680412)
LibDecimalFloatAbsTest:testAbsMinValue(int32) (runs: 5038, μ: 5162, ~: 5162)
LibDecimalFloatAbsTest:testAbsNegative(int256,int32) (runs: 5096, μ: 10536, ~: 10754)
LibDecimalFloatAbsTest:testAbsNonNegative(int256,int32) (runs: 5096, μ: 9700, ~: 9316)
Expand Down Expand Up @@ -306,11 +307,10 @@ LibDecimalFloatSubTest:testSubPacked(bytes32,bytes32) (runs: 5096, μ: 12228, ~:
LibFormatDecimalFloatCountSigFigs:testCountSigFigsExamples() (gas: 83596)
LibFormatDecimalFloatCountSigFigs:testCountSigFigsOne(int256) (runs: 5096, μ: 31877, ~: 31719)
LibFormatDecimalFloatCountSigFigs:testCountSigFigsZero(int256) (runs: 5096, μ: 3764, ~: 3764)
LibFormatDecimalFloatToDecimalStringTest:testFormatDecimalCustomSigFigs() (gas: 26551)
LibFormatDecimalFloatToDecimalStringTest:testFormatDecimalExamples() (gas: 964702)
LibFormatDecimalFloatToDecimalStringTest:testFormatDecimalRoundTripExamples() (gas: 482274)
LibFormatDecimalFloatToDecimalStringTest:testFormatDecimalRoundTripNegative(int256,uint256) (runs: 5096, μ: 71852, ~: 74944)
LibFormatDecimalFloatToDecimalStringTest:testFormatDecimalRoundTripNonNegative(uint256,uint256) (runs: 5096, μ: 55582, ~: 47513)
LibFormatDecimalFloatToDecimalStringTest:testFormatDecimalExamples() (gas: 792935)
LibFormatDecimalFloatToDecimalStringTest:testFormatDecimalRoundTripExamples() (gas: 793404)
LibFormatDecimalFloatToDecimalStringTest:testFormatDecimalRoundTripNegative(int256,bool) (runs: 5096, μ: 66724, ~: 65001)
LibFormatDecimalFloatToDecimalStringTest:testFormatDecimalRoundTripNonNegative(uint256,bool) (runs: 5096, μ: 49144, ~: 50845)
LibLogTableBytesTest:testToBytesAntiLogTableDec() (gas: 158047)
LibLogTableBytesTest:testToBytesAntiLogTableDecSmall() (gas: 160663)
LibLogTableBytesTest:testToBytesLogTableDec() (gas: 141790)
Expand Down
102 changes: 89 additions & 13 deletions crates/float/src/js_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,51 @@ impl Float {
Self::zero()
}

/// Formats the float as a decimal string.
/// Returns the default minimum value for scientific notation formatting (1e-4).
///
/// # Returns
///
/// * `Ok(Float)` - The default minimum (1e-4).
/// * `Err(FloatError)` - If the EVM call fails.
///
/// # Example
///
/// ```typescript
/// const minResult = Float.formatDefaultScientificMin();
/// if (minResult.error) {
/// console.error(minResult.error);
/// }
/// const min = minResult.value;
/// assert(min.format().value === "0.0001");
/// ```
#[wasm_export(js_name = "formatDefaultScientificMin", preserve_js_class)]
pub fn format_default_scientific_min_js() -> Result<Float, FloatError> {
Self::format_default_scientific_min()
}

/// Returns the default maximum value for scientific notation formatting (1e9).
///
/// # Returns
///
/// * `Ok(Float)` - The default maximum (1e9).
/// * `Err(FloatError)` - If the EVM call fails.
///
/// # Example
///
/// ```typescript
/// const maxResult = Float.formatDefaultScientificMax();
/// if (maxResult.error) {
/// console.error(maxResult.error);
/// }
/// const max = maxResult.value;
/// assert(max.format().value === "1000000000");
/// ```
#[wasm_export(js_name = "formatDefaultScientificMax", preserve_js_class)]
pub fn format_default_scientific_max_js() -> Result<Float, FloatError> {
Self::format_default_scientific_max()
}

/// Formats the float as a decimal string using default scientific notation range (1e-4 to 1e9).
///
/// # Returns
///
Expand All @@ -416,18 +460,19 @@ impl Float {
/// console.error(floatResult.error);
/// }
/// const float = floatResult.value;
/// assert(float.format() === "2.5");
/// const formatResult = float.format();
/// assert(formatResult.value === "2.5");
/// ```
#[wasm_export(js_name = "format")]
pub fn format_js(&self) -> Result<String, FloatError> {
self.format()
}

/// Formats the float as a decimal string with a specified significant figures limit.
/// Formats the float as a decimal string with explicit scientific notation control.
///
/// # Arguments
///
/// * `sig_figs_limit` - The significant figures limit.
/// * `scientific` - If true, always use scientific notation. If false, use decimal notation.
///
/// # Returns
///
Expand All @@ -437,16 +482,47 @@ impl Float {
/// # Example
///
/// ```typescript
/// const floatResult = Float.parse("3.14159265359");
/// if (floatResult.error) {
/// console.error(floatResult.error);
/// }
/// const float = floatResult.value;
/// assert(float.formatWithLimit(5) === "3.1416");
/// const float = Float.parse("123.456").value!;
///
/// const decResult = float.formatWithScientific(false);
/// assert(decResult.value === "123.456");
///
/// const sciResult = float.formatWithScientific(true);
/// assert(sciResult.value === "1.23456e2");
/// ```
#[wasm_export(js_name = "formatWithScientific")]
pub fn format_with_scientific_js(&self, scientific: bool) -> Result<String, FloatError> {
self.format_with_scientific(scientific)
}

/// Formats the float as a decimal string with a custom scientific notation range.
///
/// # Arguments
///
/// * `scientific_min` - Values smaller than this (in absolute value) use scientific notation.
/// * `scientific_max` - Values larger than this (in absolute value) use scientific notation.
///
/// # Returns
///
/// * `Ok(String)` - The formatted string.
/// * `Err(FloatError)` - If formatting fails.
///
/// # Example
///
/// ```typescript
/// const float = Float.parse("0.5").value!;
/// const min = Float.parse("1").value!;
/// const max = Float.parse("100").value!;
/// const result = float.formatWithRange(min, max);
/// assert(result.value === "5e-1");
/// ```
#[wasm_export(js_name = "formatWithLimit")]
pub fn format_with_limit_js(&self, sig_figs_limit: u32) -> Result<String, FloatError> {
self.format_with_limit(sig_figs_limit)
#[wasm_export(js_name = "formatWithRange")]
pub fn format_with_range_js(
&self,
scientific_min: &Self,
scientific_max: &Self,
) -> Result<String, FloatError> {
self.format_with_range(*scientific_min, *scientific_max)
}

/// Returns `true` if `self` is less than `b`.
Expand Down
Loading