Skip to main content

Float

Struct Float 

Source
pub struct Float(/* private fields */);

Implementations§

Source§

impl Float

Source

pub fn as_hex_js(&self) -> String

Returns the 32-byte hexadecimal string representation of the float.

§Returns
  • String - The 32-byte hex string.
§Example
const float = Float.fromHex("0x0000000000000000000000000000000000000000000000000000000000000005").value!;
assert(float.asHex() === "0x0000000000000000000000000000000000000000000000000000000000000005");
Source

pub fn to_bigint(&self) -> BigInt

Convert the float to a JS/TS bigint equivalent of asHex() returned hex string.

§Throws

if conversion fails.

§Example
const float = Float.fromHex("0xfffffffe0000000000000000000000000000000000000000000000000000013a");
const value = float.toBigInt();
assert(value === 115792089183396302089269705419353877679230723318366275194376439045705909141818n);
Source

pub fn from_bigint(value: BigInt) -> Float

Constructs a Float from a bigint equivalent of the fromHex() returned Float.

§Throws

if conversion fails.

§Example
const float = Float.fromBigint(115792089183396302089269705419353877679230723318366275194376439045705909141818n);
assert(float.asHex() === "0xfffffffe0000000000000000000000000000000000000000000000000000013a");
Source

pub fn from_fixed_decimal_lossy_js( value: BigInt, decimals: u8, ) -> Result<FromFixedDecimalLossyResult, JsValue>

Converts a fixed-point decimal value to a Float using the specified number of decimals, allowing lossy conversions and reporting whether precision was preserved.

This function attempts to convert a fixed-point decimal representation to a Float. Unlike fromFixedDecimal, this method will not fail if precision is lost during conversion, but instead reports the loss through the lossless flag in the result.

§Arguments
  • value - The fixed-point decimal value as a bigint (e.g., 12345n for 123.45 with 2 decimals).
  • decimals - The number of decimal places in the fixed-point representation (0-255).
§Returns

Returns a FromFixedDecimalLossyResult containing:

  • float - The resulting Float value.
  • lossless - Boolean flag indicating whether the conversion preserved all precision (true) or was lossy (false).
§Errors

Throws a JsValue error if:

  • The bigint value cannot be converted to a string.
  • The value string cannot be parsed as a valid U256.
  • The underlying EVM conversion fails.
§Example
// Lossless conversion
const result = Float.fromFixedDecimalLossy(12345n, 2);
const float = result.float;
const wasLossless = result.lossless;
assert(float.format()?.value === "123.45");
assert(wasLossless === true);

// Potentially lossy conversion
const result2 = Float.fromFixedDecimalLossy(123456789012345678901234567890n, 18);
if (!result2.lossless) {
  console.warn("Precision was lost during conversion");
}
Source§

impl Float

Source

pub fn try_to_bigint(&self) -> Result<BigInt, FloatError>

Tries to convert the float to a JS/TS bigint equivalent of asHex() returned hex string.

§Returns
  • Ok(bigint) - The resulting bigint value.
  • Err(FloatError) - If the conversion fails.
§Example
const float = Float.fromHex("0xfffffffe0000000000000000000000000000000000000000000000000000013a");
const bigintResult = float.tryToBigInt();
if (bigintResult.error) {
    console.error(bigintResult.error);
}
assert(bigintResult.value === 115792089183396302089269705419353877679230723318366275194376439045705909141818n);
Source

pub fn try_from_bigint(value: BigInt) -> Result<Float, FloatError>

Constructs a Float from a bigint equivalent of the fromHex() returned Float.

§Returns
  • Ok(Float) - The resulting Float value.
  • Err(FloatError) - If the conversion fails.
§Example
const floatResult = Float.tryFromBigint(115792089183396302089269705419353877679230723318366275194376439045705909141818n);
if (floatResult.error) {
  console.error(floatResult.error);
}
const value = float.value;
assert(value.asHex() === "0xfffffffe0000000000000000000000000000000000000000000000000000013a");
Source

pub fn from_fixed_decimal_js( value: BigInt, decimals: u8, ) -> Result<Float, FloatError>

Converts a fixed-point decimal value to a Float using the specified number of decimals.

§Arguments
  • value - The fixed-point decimal value as a string.
  • decimals - The number of decimals in the fixed-point representation.
§Returns
  • Ok(Float) - The resulting Float value.
  • Err(FloatError) - If the conversion fails.
§Example
const floatResult = Float.fromFixedDecimal("12345", 2);
if (floatResult.error) {
   console.error(floatResult.error);
}
const float = floatResult.value;
assert(float.format() === "123.45");
Source

pub fn to_fixed_decimal_js(&self, decimals: u8) -> Result<BigInt, FloatError>

Converts a Float to a fixed-point decimal value using the specified number of decimals.

§Arguments
  • decimals - The number of decimals in the fixed-point representation.
§Returns
  • Ok(String) - The resulting fixed-point decimal value as a string.
  • Err(FloatError) - If the conversion fails.
§Example
const float = Float.parse("123.45").value!;
const result = float.toFixedDecimal(2);
if (result.error) {
   console.error(result.error);
}
assert(result.value === "12345");
Source

pub fn to_fixed_decimal_lossy_js( &self, decimals: u8, ) -> Result<ToFixedDecimalLossyResult, FloatError>

Converts a Float to a fixed-point decimal value using the specified number of decimals lossy.

§Returns

ToFixedDecimalLossyResult containing the value and lossless flag.

Source

pub fn parse_js(str: String) -> Result<Float, FloatError>

Parses a decimal string into a Float.

§Arguments
  • str - The string to parse.
§Returns
  • Ok(Float) - The parsed float.
  • Err(FloatError) - If parsing fails.
§Example
const floatResult = Float.parse("3.1415");
if (floatResult.error) {
   console.error(floatResult.error);
}
const float = floatResult.value;
assert(float.format() === "3.1415");
Source

pub fn from_hex_js(hex: &str) -> Result<Float, FloatError>

Constructs a Float from a 32-byte hexadecimal string.

§Arguments
  • hex - The 32-byte hex string to parse.
§Returns
  • Ok(Float) - The float parsed from the hex string.
  • Err(FloatError) - If the hex string is not valid or not 32 bytes.
§Example
const floatResult = Float.fromHex("0x0000000000000000000000000000000000000000000000000000000000000005");
if (floatResult.error) {
   console.error(floatResult.error);
}
const float = floatResult.value;
assert(float.asHex() === "0x0000000000000000000000000000000000000000000000000000000000000005");
Source

pub fn max_positive_value_js() -> Result<Float, FloatError>

Returns the maximum positive value that can be represented as a Float.

§Returns
  • Ok(Float) - The maximum positive value.
  • Err(FloatError) - If the EVM call fails.
§Example
const maxPosResult = Float.maxPositiveValue();
if (maxPosResult.error) {
   console.error(maxPosResult.error);
}
const maxPos = maxPosResult.value;
assert(!maxPos.format().error);
Source

pub fn min_positive_value_js() -> Result<Float, FloatError>

Returns the minimum positive value that can be represented as a Float.

§Returns
  • Ok(Float) - The minimum positive value.
  • Err(FloatError) - If the EVM call fails.
§Example
const minPosResult = Float.minPositiveValue();
if (minPosResult.error) {
   console.error(minPosResult.error);
}
const minPos = minPosResult.value;
assert(!minPos.format().error);
Source

pub fn max_negative_value_js() -> Result<Float, FloatError>

Returns the maximum negative value that can be represented as a Float.

§Returns
  • Ok(Float) - The maximum negative value (closest to zero).
  • Err(FloatError) - If the EVM call fails.
§Example
const maxNegResult = Float.maxNegativeValue();
if (maxNegResult.error) {
   console.error(maxNegResult.error);
}
const maxNeg = maxNegResult.value;
assert(!maxNeg.format().error);
Source

pub fn min_negative_value_js() -> Result<Float, FloatError>

Returns the minimum negative value that can be represented as a Float.

§Returns
  • Ok(Float) - The minimum negative value (furthest from zero).
  • Err(FloatError) - If the EVM call fails.
§Example
const minNegResult = Float.minNegativeValue();
if (minNegResult.error) {
   console.error(minNegResult.error);
}
const minNeg = minNegResult.value;
assert(!minNeg.format().error);
Source

pub fn zero_js() -> Result<Float, FloatError>

Returns the zero value of a Float in its maximized representation.

§Returns
  • Ok(Float) - The zero value.
  • Err(FloatError) - If the EVM call fails.
§Example
const zeroResult = Float.zero();
if (zeroResult.error) {
   console.error(zeroResult.error);
}
const zero = zeroResult.value;
assert(zero.isZero().value);
assert(zero.format().value === "0");
Source

pub fn format_default_scientific_min_js() -> Result<Float, FloatError>

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
const minResult = Float.formatDefaultScientificMin();
if (minResult.error) {
   console.error(minResult.error);
}
const min = minResult.value;
assert(min.format().value === "0.0001");
Source

pub fn format_default_scientific_max_js() -> Result<Float, FloatError>

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
const maxResult = Float.formatDefaultScientificMax();
if (maxResult.error) {
   console.error(maxResult.error);
}
const max = maxResult.value;
assert(max.format().value === "1000000000");
Source

pub fn format_js(&self) -> Result<String, FloatError>

Formats the float as a decimal string using default scientific notation range (1e-4 to 1e9).

§Returns
  • Ok(String) - The formatted string.
  • Err(FloatError) - If formatting fails.
§Example
const floatResult = Float.parse("2.5");
if (floatResult.error) {
   console.error(floatResult.error);
}
const float = floatResult.value;
const formatResult = float.format();
assert(formatResult.value === "2.5");
Source

pub fn format_with_scientific_js( &self, scientific: bool, ) -> Result<String, FloatError>

Formats the float as a decimal string with explicit scientific notation control.

§Arguments
  • scientific - If true, always use scientific notation. If false, use decimal notation.
§Returns
  • Ok(String) - The formatted string.
  • Err(FloatError) - If formatting fails.
§Example
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");
Source

pub fn format_with_range_js( &self, scientific_min: &Self, scientific_max: &Self, ) -> Result<String, FloatError>

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
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");
Source

pub fn lt_js(&self, b: &Self) -> Result<bool, FloatError>

Returns true if self is less than b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is less than b.
  • Ok(false) if self is not less than b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.lt(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn eq_js(&self, b: &Self) -> Result<bool, FloatError>

Returns true if self is equal to b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is equal to b.
  • Ok(false) if self is not equal to b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
const a = Float.parse("2.0").value!;
const b = Float.parse("2.0").value!;
const result = a.eq(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn lte_js(&self, b: &Self) -> Result<bool, FloatError>

Returns true if self is less than or equal to b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is less than or equal to b.
  • Ok(false) if self is not less than or equal to b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.lte(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn gte_js(&self, b: &Self) -> Result<bool, FloatError>

Returns true if self is greater than or equal to b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is greater than or equal to b.
  • Ok(false) if self is not greater than or equal to b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
const a = Float.parse("2.0").value!;
const b = Float.parse("1.0").value!;
const result = a.gte(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn gt_js(&self, b: &Self) -> Result<bool, FloatError>

Returns true if self is greater than b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is greater than b.
  • Ok(false) if self is not greater than b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
const a = Float.parse("2.0").value!;
const b = Float.parse("1.0").value!;
const result = a.gt(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn inv_js(&self) -> Result<Float, FloatError>

Returns the multiplicative inverse of the float.

§Returns
  • Ok(Float) - The inverse.
  • Err(FloatError) - If inversion fails.
§Example
const x = Float.parse("2.0").value!;
const inv = x.inv();
if (inv.error) {
   console.error(inv.error);
}
assert(inv.value.format().startsWith("0.5"));
Source

pub fn abs_js(&self) -> Result<Float, FloatError>

Returns the absolute value of the float.

§Returns
  • Ok(Float) - The absolute value.
  • Err(FloatError) - If the operation fails.
§Example
const x = Float.parse("-3.14").value!;
const abs = x.abs();
if (abs.error) {
   console.error(abs.error);
}
assert(abs.value.format() === "3.14");
Source

pub fn add_js(&self, b: &Self) -> Result<Float, FloatError>

Adds two floats.

§Returns
  • Ok(Float) - The sum.
  • Err(FloatError) - If addition fails.
§Example
const a = Float.parse("1.5").value!;
const b = Float.parse("2.5").value!;
const result = a.add(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "4");
Source

pub fn sub_js(&self, b: &Self) -> Result<Float, FloatError>

Subtracts b from self.

§Returns
  • Ok(Float) - The difference.
  • Err(FloatError) - If subtraction fails.
§Example
const a = Float.parse("5.0").value!;
const b = Float.parse("2.0").value!;
const result = a.sub(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "3");
Source

pub fn mul_js(&self, b: &Self) -> Result<Float, FloatError>

Multiplies two floats.

§Returns
  • Ok(Float) - The product.
  • Err(FloatError) - If multiplication fails.
§Example
const a = Float.parse("2.0").value!;
const b = Float.parse("3.0").value!;
const result = a.mul(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "6");
Source

pub fn div_js(&self, b: &Self) -> Result<Float, FloatError>

Divides self by b.

§Returns
  • Ok(Float) - The quotient.
  • Err(FloatError) - If division fails.
§Example
const a = Float.parse("6.0").value!;
const b = Float.parse("2.0").value!;
const result = a.div(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "3");
Source

pub fn frac_js(&self) -> Result<Float, FloatError>

Returns the fractional part of the float.

§Returns
  • Ok(Float) - The fractional part.
  • Err(FloatError) - If the operation fails.
§Example
const x = Float.parse("3.75").value!;
const result = x.frac();
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "0.75");
Source

pub fn floor_js(&self) -> Result<Float, FloatError>

Returns the floor of the float.

§Returns
  • Ok(Float) - The floored value.
  • Err(FloatError) - If the operation fails.
§Example
const x = Float.parse("3.75").value!;
const result = x.floor();
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "3");
Source

pub fn min_js(&self, b: &Self) -> Result<Float, FloatError>

Returns the minimum of self and b.

§Arguments
  • b - The other Float to compare with.
§Returns
  • Ok(Float) - The minimum value.
  • Err(FloatError) - If the operation fails.
§Example
const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.min(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "1");
Source

pub fn max_js(&self, b: &Self) -> Result<Float, FloatError>

Returns the maximum of self and b.

§Arguments
  • b - The other Float to compare with.
§Returns
  • Ok(Float) - The maximum value.
  • Err(FloatError) - If the operation fails.
§Example
const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.max(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "2");
Source

pub fn is_zero_js(&self) -> Result<bool, FloatError>

Checks if the float is zero.

§Returns
  • Ok(true) if the float is zero.
  • Ok(false) if the float is not zero.
  • Err(FloatError) if the operation fails.
§Example
const zero = Float.parse("0").value!;
const result = zero.isZero();
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn neg_js(&self) -> Result<Float, FloatError>

Returns the negation of the float.

§Returns
  • Ok(Float) - The negated value.
  • Err(FloatError) - If the operation fails.
§Example
const x = Float.parse("3.14").value!;
const result = x.neg();
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "-3.14");
Source§

impl Float

Source

pub fn try_to_bigint__wasm_export(&self) -> JsValue

Tries to convert the float to a JS/TS bigint equivalent of asHex() returned hex string.

§Returns
  • Ok(bigint) - The resulting bigint value.
  • Err(FloatError) - If the conversion fails.
§Example
const float = Float.fromHex("0xfffffffe0000000000000000000000000000000000000000000000000000013a");
const bigintResult = float.tryToBigInt();
if (bigintResult.error) {
    console.error(bigintResult.error);
}
assert(bigintResult.value === 115792089183396302089269705419353877679230723318366275194376439045705909141818n);
Source

pub fn try_from_bigint__wasm_export(value: BigInt) -> JsValue

Constructs a Float from a bigint equivalent of the fromHex() returned Float.

§Returns
  • Ok(Float) - The resulting Float value.
  • Err(FloatError) - If the conversion fails.
§Example
const floatResult = Float.tryFromBigint(115792089183396302089269705419353877679230723318366275194376439045705909141818n);
if (floatResult.error) {
  console.error(floatResult.error);
}
const value = float.value;
assert(value.asHex() === "0xfffffffe0000000000000000000000000000000000000000000000000000013a");
Source

pub fn from_fixed_decimal_js__wasm_export( value: BigInt, decimals: u8, ) -> JsValue

Converts a fixed-point decimal value to a Float using the specified number of decimals.

§Arguments
  • value - The fixed-point decimal value as a string.
  • decimals - The number of decimals in the fixed-point representation.
§Returns
  • Ok(Float) - The resulting Float value.
  • Err(FloatError) - If the conversion fails.
§Example
const floatResult = Float.fromFixedDecimal("12345", 2);
if (floatResult.error) {
   console.error(floatResult.error);
}
const float = floatResult.value;
assert(float.format() === "123.45");
Source

pub fn to_fixed_decimal_js__wasm_export(&self, decimals: u8) -> JsValue

Converts a Float to a fixed-point decimal value using the specified number of decimals.

§Arguments
  • decimals - The number of decimals in the fixed-point representation.
§Returns
  • Ok(String) - The resulting fixed-point decimal value as a string.
  • Err(FloatError) - If the conversion fails.
§Example
const float = Float.parse("123.45").value!;
const result = float.toFixedDecimal(2);
if (result.error) {
   console.error(result.error);
}
assert(result.value === "12345");
Source

pub fn to_fixed_decimal_lossy_js__wasm_export(&self, decimals: u8) -> JsValue

Converts a Float to a fixed-point decimal value using the specified number of decimals lossy.

§Returns

ToFixedDecimalLossyResult containing the value and lossless flag.

Source

pub fn parse_js__wasm_export(str: String) -> JsValue

Parses a decimal string into a Float.

§Arguments
  • str - The string to parse.
§Returns
  • Ok(Float) - The parsed float.
  • Err(FloatError) - If parsing fails.
§Example
const floatResult = Float.parse("3.1415");
if (floatResult.error) {
   console.error(floatResult.error);
}
const float = floatResult.value;
assert(float.format() === "3.1415");
Source

pub fn from_hex_js__wasm_export(hex: &str) -> JsValue

Constructs a Float from a 32-byte hexadecimal string.

§Arguments
  • hex - The 32-byte hex string to parse.
§Returns
  • Ok(Float) - The float parsed from the hex string.
  • Err(FloatError) - If the hex string is not valid or not 32 bytes.
§Example
const floatResult = Float.fromHex("0x0000000000000000000000000000000000000000000000000000000000000005");
if (floatResult.error) {
   console.error(floatResult.error);
}
const float = floatResult.value;
assert(float.asHex() === "0x0000000000000000000000000000000000000000000000000000000000000005");
Source

pub fn max_positive_value_js__wasm_export() -> JsValue

Returns the maximum positive value that can be represented as a Float.

§Returns
  • Ok(Float) - The maximum positive value.
  • Err(FloatError) - If the EVM call fails.
§Example
const maxPosResult = Float.maxPositiveValue();
if (maxPosResult.error) {
   console.error(maxPosResult.error);
}
const maxPos = maxPosResult.value;
assert(!maxPos.format().error);
Source

pub fn min_positive_value_js__wasm_export() -> JsValue

Returns the minimum positive value that can be represented as a Float.

§Returns
  • Ok(Float) - The minimum positive value.
  • Err(FloatError) - If the EVM call fails.
§Example
const minPosResult = Float.minPositiveValue();
if (minPosResult.error) {
   console.error(minPosResult.error);
}
const minPos = minPosResult.value;
assert(!minPos.format().error);
Source

pub fn max_negative_value_js__wasm_export() -> JsValue

Returns the maximum negative value that can be represented as a Float.

§Returns
  • Ok(Float) - The maximum negative value (closest to zero).
  • Err(FloatError) - If the EVM call fails.
§Example
const maxNegResult = Float.maxNegativeValue();
if (maxNegResult.error) {
   console.error(maxNegResult.error);
}
const maxNeg = maxNegResult.value;
assert(!maxNeg.format().error);
Source

pub fn min_negative_value_js__wasm_export() -> JsValue

Returns the minimum negative value that can be represented as a Float.

§Returns
  • Ok(Float) - The minimum negative value (furthest from zero).
  • Err(FloatError) - If the EVM call fails.
§Example
const minNegResult = Float.minNegativeValue();
if (minNegResult.error) {
   console.error(minNegResult.error);
}
const minNeg = minNegResult.value;
assert(!minNeg.format().error);
Source

pub fn zero_js__wasm_export() -> JsValue

Returns the zero value of a Float in its maximized representation.

§Returns
  • Ok(Float) - The zero value.
  • Err(FloatError) - If the EVM call fails.
§Example
const zeroResult = Float.zero();
if (zeroResult.error) {
   console.error(zeroResult.error);
}
const zero = zeroResult.value;
assert(zero.isZero().value);
assert(zero.format().value === "0");
Source

pub fn format_default_scientific_min_js__wasm_export() -> JsValue

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
const minResult = Float.formatDefaultScientificMin();
if (minResult.error) {
   console.error(minResult.error);
}
const min = minResult.value;
assert(min.format().value === "0.0001");
Source

pub fn format_default_scientific_max_js__wasm_export() -> JsValue

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
const maxResult = Float.formatDefaultScientificMax();
if (maxResult.error) {
   console.error(maxResult.error);
}
const max = maxResult.value;
assert(max.format().value === "1000000000");
Source

pub fn format_js__wasm_export(&self) -> WasmEncodedResult<String>

Formats the float as a decimal string using default scientific notation range (1e-4 to 1e9).

§Returns
  • Ok(String) - The formatted string.
  • Err(FloatError) - If formatting fails.
§Example
const floatResult = Float.parse("2.5");
if (floatResult.error) {
   console.error(floatResult.error);
}
const float = floatResult.value;
const formatResult = float.format();
assert(formatResult.value === "2.5");
Source

pub fn format_with_scientific_js__wasm_export( &self, scientific: bool, ) -> WasmEncodedResult<String>

Formats the float as a decimal string with explicit scientific notation control.

§Arguments
  • scientific - If true, always use scientific notation. If false, use decimal notation.
§Returns
  • Ok(String) - The formatted string.
  • Err(FloatError) - If formatting fails.
§Example
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");
Source

pub fn format_with_range_js__wasm_export( &self, scientific_min: &Self, scientific_max: &Self, ) -> WasmEncodedResult<String>

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
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");
Source

pub fn lt_js__wasm_export(&self, b: &Self) -> WasmEncodedResult<bool>

Returns true if self is less than b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is less than b.
  • Ok(false) if self is not less than b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.lt(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn eq_js__wasm_export(&self, b: &Self) -> WasmEncodedResult<bool>

Returns true if self is equal to b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is equal to b.
  • Ok(false) if self is not equal to b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
const a = Float.parse("2.0").value!;
const b = Float.parse("2.0").value!;
const result = a.eq(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn lte_js__wasm_export(&self, b: &Self) -> WasmEncodedResult<bool>

Returns true if self is less than or equal to b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is less than or equal to b.
  • Ok(false) if self is not less than or equal to b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.lte(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn gte_js__wasm_export(&self, b: &Self) -> WasmEncodedResult<bool>

Returns true if self is greater than or equal to b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is greater than or equal to b.
  • Ok(false) if self is not greater than or equal to b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
const a = Float.parse("2.0").value!;
const b = Float.parse("1.0").value!;
const result = a.gte(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn gt_js__wasm_export(&self, b: &Self) -> WasmEncodedResult<bool>

Returns true if self is greater than b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is greater than b.
  • Ok(false) if self is not greater than b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
const a = Float.parse("2.0").value!;
const b = Float.parse("1.0").value!;
const result = a.gt(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn inv_js__wasm_export(&self) -> JsValue

Returns the multiplicative inverse of the float.

§Returns
  • Ok(Float) - The inverse.
  • Err(FloatError) - If inversion fails.
§Example
const x = Float.parse("2.0").value!;
const inv = x.inv();
if (inv.error) {
   console.error(inv.error);
}
assert(inv.value.format().startsWith("0.5"));
Source

pub fn abs_js__wasm_export(&self) -> JsValue

Returns the absolute value of the float.

§Returns
  • Ok(Float) - The absolute value.
  • Err(FloatError) - If the operation fails.
§Example
const x = Float.parse("-3.14").value!;
const abs = x.abs();
if (abs.error) {
   console.error(abs.error);
}
assert(abs.value.format() === "3.14");
Source

pub fn add_js__wasm_export(&self, b: &Self) -> JsValue

Adds two floats.

§Returns
  • Ok(Float) - The sum.
  • Err(FloatError) - If addition fails.
§Example
const a = Float.parse("1.5").value!;
const b = Float.parse("2.5").value!;
const result = a.add(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "4");
Source

pub fn sub_js__wasm_export(&self, b: &Self) -> JsValue

Subtracts b from self.

§Returns
  • Ok(Float) - The difference.
  • Err(FloatError) - If subtraction fails.
§Example
const a = Float.parse("5.0").value!;
const b = Float.parse("2.0").value!;
const result = a.sub(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "3");
Source

pub fn mul_js__wasm_export(&self, b: &Self) -> JsValue

Multiplies two floats.

§Returns
  • Ok(Float) - The product.
  • Err(FloatError) - If multiplication fails.
§Example
const a = Float.parse("2.0").value!;
const b = Float.parse("3.0").value!;
const result = a.mul(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "6");
Source

pub fn div_js__wasm_export(&self, b: &Self) -> JsValue

Divides self by b.

§Returns
  • Ok(Float) - The quotient.
  • Err(FloatError) - If division fails.
§Example
const a = Float.parse("6.0").value!;
const b = Float.parse("2.0").value!;
const result = a.div(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "3");
Source

pub fn frac_js__wasm_export(&self) -> JsValue

Returns the fractional part of the float.

§Returns
  • Ok(Float) - The fractional part.
  • Err(FloatError) - If the operation fails.
§Example
const x = Float.parse("3.75").value!;
const result = x.frac();
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "0.75");
Source

pub fn floor_js__wasm_export(&self) -> JsValue

Returns the floor of the float.

§Returns
  • Ok(Float) - The floored value.
  • Err(FloatError) - If the operation fails.
§Example
const x = Float.parse("3.75").value!;
const result = x.floor();
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "3");
Source

pub fn min_js__wasm_export(&self, b: &Self) -> JsValue

Returns the minimum of self and b.

§Arguments
  • b - The other Float to compare with.
§Returns
  • Ok(Float) - The minimum value.
  • Err(FloatError) - If the operation fails.
§Example
const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.min(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "1");
Source

pub fn max_js__wasm_export(&self, b: &Self) -> JsValue

Returns the maximum of self and b.

§Arguments
  • b - The other Float to compare with.
§Returns
  • Ok(Float) - The maximum value.
  • Err(FloatError) - If the operation fails.
§Example
const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.max(b);
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "2");
Source

pub fn is_zero_js__wasm_export(&self) -> WasmEncodedResult<bool>

Checks if the float is zero.

§Returns
  • Ok(true) if the float is zero.
  • Ok(false) if the float is not zero.
  • Err(FloatError) if the operation fails.
§Example
const zero = Float.parse("0").value!;
const result = zero.isZero();
if (result.error) {
   console.error(result.error);
}
assert(result.value);
Source

pub fn neg_js__wasm_export(&self) -> JsValue

Returns the negation of the float.

§Returns
  • Ok(Float) - The negated value.
  • Err(FloatError) - If the operation fails.
§Example
const x = Float.parse("3.14").value!;
const result = x.neg();
if (result.error) {
   console.error(result.error);
}
assert(result.value.format() === "-3.14");
Source§

impl Float

Source

pub const fn from_raw(value: B256) -> Self

Creates a new Float from the given 32-byte value B256.

Source

pub fn get_inner(&self) -> B256

Getter for inner 32-bytes value of this Float instance as B256.

Source

pub fn set_inner(&mut self, value: B256)

Sets the inner 32-byte value of this float from the given B256.

Source

pub fn from_fixed_decimal(value: U256, decimals: u8) -> Result<Self, FloatError>

Converts a fixed-point decimal value to a Float using the specified number of decimals.

§Arguments
  • value - The fixed-point decimal value as a U256.
  • decimals - The number of decimals in the fixed-point representation.
§Returns
  • Ok(Float) - The resulting Float value.
  • Err(FloatError) - If the conversion fails.
§Example
use rain_math_float::Float;
use alloy::primitives::U256;

// 123.45 with 2 decimals is represented as 12345
let value = U256::from(12345u64);
let decimals = 2u8;
let float = Float::from_fixed_decimal(value, decimals)?;
assert_eq!(float.format()?, "123.45");

anyhow::Ok(())
Source

pub fn to_fixed_decimal(self, decimals: u8) -> Result<U256, FloatError>

Converts a Float to a fixed-point decimal value using the specified number of decimals.

§Arguments
  • decimals - The number of decimals in the fixed-point representation.
§Returns
  • Ok(U256) - The resulting fixed-point decimal value.
  • Err(FloatError) - If the conversion fails.
§Example
use rain_math_float::Float;
use alloy::primitives::U256;

// 123.45 with 2 decimals becomes 12345
let float = Float::parse("123.45".to_string())?;
let fixed = float.to_fixed_decimal(2)?;
assert_eq!(fixed, U256::from(12345u64));

anyhow::Ok(())
Source

pub fn from_fixed_decimal_lossy( value: U256, decimals: u8, ) -> Result<(Self, bool), FloatError>

Converts a fixed-point decimal value to a Float using the specified number of decimals lossy.

§Arguments
  • value - The fixed-point decimal value as a U256.
  • decimals - The number of decimals in the fixed-point representation.
§Returns
  • Ok((Float, bool)) - The resulting Float value and a boolean indicating if the conversion was lossless.
  • Err(FloatError) - If the conversion fails.
§Example
use rain_math_float::Float;
use alloy::primitives::U256;

// 123.45 with 2 decimals is represented as 12345
let value = U256::from(12345u64);
let decimals = 2u8;
let (float, lossless) = Float::from_fixed_decimal_lossy(value, decimals)?;
assert_eq!(float.format()?, "123.45");
assert!(lossless);

anyhow::Ok(())
Source

pub fn to_fixed_decimal_lossy( self, decimals: u8, ) -> Result<(U256, bool), FloatError>

Converts a Float to a fixed-point decimal value using the specified number of decimals lossy.

§Arguments
  • decimals - The number of decimals in the fixed-point representation.
§Returns
  • Ok((U256, bool)) - The resulting fixed-point decimal value and a boolean indicating if the conversion was lossless.
  • Err(FloatError) - If the conversion fails.
§Example
use rain_math_float::Float;
use alloy::primitives::U256;

// 123.45 with 2 decimals becomes 12345
let float = Float::from_fixed_decimal(U256::from(12345), 3)?;
let (fixed, lossless) = float.to_fixed_decimal_lossy(2)?;
assert_eq!(fixed, U256::from(1234u64));
assert!(!lossless);

anyhow::Ok(())
Source

pub fn parse(str: String) -> Result<Self, FloatError>

Parses a decimal string into a Float.

§Arguments
  • str - The string to parse.
§Returns
  • Ok(Float) - The parsed float.
  • Err(FloatError) - If parsing fails.
§Example
use rain_math_float::Float;

let float = Float::parse("3.1415".to_string())?;
assert_eq!(float.format()?, "3.1415");

anyhow::Ok(())
Source

pub fn as_hex(self) -> String

Returns the 32-byte hexadecimal string representation of the float.

§Returns
  • String - The 32-byte hex string.
§Example
use rain_math_float::Float;
let float = Float::from_hex("0x0000000000000000000000000000000000000000000000000000000000000005").unwrap();
assert_eq!(float.as_hex(), "0x0000000000000000000000000000000000000000000000000000000000000005");
Source

pub fn from_hex(hex: &str) -> Result<Self, FloatError>

Constructs a Float from a 32-byte hexadecimal string.

§Arguments
  • hex - The 32-byte hex string to parse.
§Returns
  • Ok(Float) - The float parsed from the hex string.
  • Err(FloatError) - If the hex string is not valid or not 32 bytes.
§Example
use rain_math_float::Float;
let float = Float::from_hex("0x0000000000000000000000000000000000000000000000000000000000000005")?;
assert_eq!(float.as_hex(), "0x0000000000000000000000000000000000000000000000000000000000000005");
anyhow::Ok(())
Source

pub fn max_positive_value() -> Result<Self, FloatError>

Returns the maximum positive value that can be represented as a Float.

§Returns
  • Ok(Float) - The maximum positive value.
  • Err(FloatError) - If the EVM call fails.
§Example
use rain_math_float::Float;

let max_pos = Float::max_positive_value()?;
let zero = Float::parse("0".to_string())?;

// Max positive is greater than zero
assert!(max_pos.gt(zero)?);

// Max positive is greater than any normal large number
let big_number = Float::parse("999999999999999999999".to_string())?;
assert!(max_pos.gt(big_number)?);

anyhow::Ok(())
Source

pub fn min_positive_value() -> Result<Self, FloatError>

Returns the minimum positive value that can be represented as a Float.

§Returns
  • Ok(Float) - The minimum positive value.
  • Err(FloatError) - If the EVM call fails.
§Example
use rain_math_float::Float;

let min_pos = Float::min_positive_value()?;
let zero = Float::parse("0".to_string())?;

// Min positive is greater than zero but smaller than any other positive number
assert!(min_pos.gt(zero)?);

let small_number = Float::parse("0.000000000000000001".to_string())?;
assert!(min_pos.lt(small_number)?);

anyhow::Ok(())
Source

pub fn max_negative_value() -> Result<Self, FloatError>

Returns the maximum negative value that can be represented as a Float.

§Returns
  • Ok(Float) - The maximum negative value (closest to zero).
  • Err(FloatError) - If the EVM call fails.
§Example
use rain_math_float::Float;

let max_neg = Float::max_negative_value()?;
let zero = Float::parse("0".to_string())?;

// Max negative is less than zero but greater than any other negative number
assert!(max_neg.lt(zero)?);

let small_negative = Float::parse("-0.000000000000000001".to_string())?;
assert!(max_neg.gt(small_negative)?);

anyhow::Ok(())
Source

pub fn min_negative_value() -> Result<Self, FloatError>

Returns the minimum negative value that can be represented as a Float.

§Returns
  • Ok(Float) - The minimum negative value (furthest from zero).
  • Err(FloatError) - If the EVM call fails.
§Example
use rain_math_float::Float;

let min_neg = Float::min_negative_value()?;
let zero = Float::parse("0".to_string())?;

// Min negative is less than zero
assert!(min_neg.lt(zero)?);

// Min negative is less than any normal negative number
let big_negative = Float::parse("-999999999999999999999".to_string())?;
assert!(min_neg.lt(big_negative)?);

anyhow::Ok(())
Source

pub fn zero() -> Result<Self, FloatError>

Returns the zero value of a Float in its maximized representation.

§Returns
  • Ok(Float) - The zero value.
  • Err(FloatError) - If the EVM call fails.
§Example
use rain_math_float::Float;

let zero = Float::zero()?;
assert!(zero.is_zero()?);
assert_eq!(zero.format()?, "0");

// Should be equal to parsed zero
let parsed_zero = Float::parse("0".to_string())?;
assert!(zero.eq(parsed_zero)?);

anyhow::Ok(())
Source

pub fn format_default_scientific_min() -> Result<Self, FloatError>

Returns the default minimum value for scientific notation formatting (1e-4).

Values smaller than this (in absolute value) will be formatted in scientific notation.

§Returns
  • Ok(Float) - The default minimum (1e-4).
  • Err(FloatError) - If the EVM call fails.
§Example
use rain_math_float::Float;

let min = Float::format_default_scientific_min()?;
assert_eq!(min.format()?, "0.0001");

anyhow::Ok(())
Source

pub fn format_default_scientific_max() -> Result<Self, FloatError>

Returns the default maximum value for scientific notation formatting (1e9).

Values larger than this (in absolute value) will be formatted in scientific notation.

§Returns
  • Ok(Float) - The default maximum (1e9).
  • Err(FloatError) - If the EVM call fails.
§Example
use rain_math_float::Float;

let max = Float::format_default_scientific_max()?;
assert_eq!(max.format()?, "1000000000");

anyhow::Ok(())
Source

pub fn format(self) -> Result<String, FloatError>

Formats the float as a decimal string using default scientific notation range (1e-4 to 1e9).

Values within the range [1e-4, 1e9] will use decimal notation. Values outside this range will use scientific notation.

§Returns
  • Ok(String) - The formatted string.
  • Err(FloatError) - If formatting fails.
§Examples

Values within the default range use decimal notation:

use rain_math_float::Float;

// At the boundaries (inclusive)
assert_eq!(Float::parse("0.0001".to_string())?.format()?, "0.0001");  // 1e-4
assert_eq!(Float::parse("1000000000".to_string())?.format()?, "1000000000");  // 1e9

// Within range
assert_eq!(Float::parse("2.5".to_string())?.format()?, "2.5");
assert_eq!(Float::parse("123.456".to_string())?.format()?, "123.456");
assert_eq!(Float::parse("0.001".to_string())?.format()?, "0.001");
assert_eq!(Float::parse("1000000".to_string())?.format()?, "1000000");

anyhow::Ok(())

Values outside the default range use scientific notation:

use rain_math_float::Float;

// Smaller than 1e-4
assert_eq!(Float::parse("0.00001".to_string())?.format()?, "1e-5");
assert_eq!(Float::parse("0.000001".to_string())?.format()?, "1e-6");

// Larger than 1e9
assert_eq!(Float::parse("10000000000".to_string())?.format()?, "1e10");
assert_eq!(Float::parse("123000000000".to_string())?.format()?, "1.23e11");

anyhow::Ok(())
Source

pub fn format_with_scientific( self, scientific: bool, ) -> Result<String, FloatError>

Formats the float as a decimal string with explicit scientific notation control.

§Arguments
  • scientific - If true, always use scientific notation. If false, use decimal notation.
§Returns
  • Ok(String) - The formatted string.
  • Err(FloatError) - If formatting fails.
§Example
use rain_math_float::Float;

let float = Float::parse("3.14".to_string())?;
assert_eq!(float.format_with_scientific(false)?, "3.14");
assert_eq!(float.format_with_scientific(true)?, "3.14");

anyhow::Ok(())
Source

pub fn format_with_range( self, scientific_min: Self, scientific_max: Self, ) -> Result<String, FloatError>

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
use rain_math_float::Float;

let float = Float::parse("0.001".to_string())?;
let min = Float::parse("0.01".to_string())?;
let max = Float::parse("100".to_string())?;
assert_eq!(float.format_with_range(min, max)?, "1e-3");

anyhow::Ok(())
Source

pub fn lt(self, b: Self) -> Result<bool, FloatError>

Returns true if self is less than b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is less than b.
  • Ok(false) if self is not less than b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
use rain_math_float::Float;

let a = Float::parse("1.0".to_string())?;
let b = Float::parse("2.0".to_string())?;
assert!(a.lt(b)?);

anyhow::Ok(())
Source

pub fn eq(self, b: Self) -> Result<bool, FloatError>

Returns true if self is equal to b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is equal to b.
  • Ok(false) if self is not equal to b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
use rain_math_float::Float;

let a = Float::parse("3.14".to_string())?;
let b = Float::parse("3.14".to_string())?;
assert!(a.eq(b)?);

anyhow::Ok(())
Source

pub fn gt(self, b: Self) -> Result<bool, FloatError>

Returns true if self is greater than b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is greater than b.
  • Ok(false) if self is not greater than b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
use rain_math_float::Float;

let a = Float::parse("5.0".to_string())?;
let b = Float::parse("2.0".to_string())?;
assert!(a.gt(b)?);

anyhow::Ok(())
Source

pub fn inv(self) -> Result<Self, FloatError>

Returns the multiplicative inverse of the float.

§Returns
  • Ok(Float) - The inverse.
  • Err(FloatError) - If inversion fails.
§Example
use rain_math_float::Float;

let x = Float::parse("2.0".to_string())?;
let inv = x.inv()?;
assert!(inv.format()?.starts_with("0.5"));

anyhow::Ok(())
Source

pub fn abs(self) -> Result<Float, FloatError>

Returns the absolute value of the float.

§Returns
  • Ok(Float) - The absolute value.
  • Err(FloatError) - If the operation fails.
§Example
use rain_math_float::Float;

let x = Float::parse("-3.14".to_string())?;
let abs = x.abs()?;
assert_eq!(abs.format()?, "3.14");

anyhow::Ok(())
Source

pub fn lte(self, b: Self) -> Result<bool, FloatError>

Returns true if self is less than or equal to b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is less than or equal to b.
  • Ok(false) if self is not less than or equal to b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
use rain_math_float::Float;

let a = Float::parse("1.0".to_string())?;
let b = Float::parse("2.0".to_string())?;
assert!(a.lte(b)?);

anyhow::Ok(())
Source

pub fn gte(self, b: Self) -> Result<bool, FloatError>

Returns true if self is greater than or equal to b.

§Arguments
  • b - The Float value to compare with self.
§Returns
  • Ok(true) if self is greater than or equal to b.
  • Ok(false) if self is not greater than or equal to b.
  • Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.
§Example
use rain_math_float::Float;

let a = Float::parse("2.0".to_string())?;
let b = Float::parse("1.0".to_string())?;
assert!(a.gte(b)?);

anyhow::Ok(())
Source§

impl Float

Source

pub fn integer(self) -> Result<Float, FloatError>

Returns the integer part of the float (truncation toward zero).

§Returns
  • Ok(Float) - The integer part.
  • Err(FloatError) - If the operation fails.
§Example
use rain_math_float::Float;

let x = Float::parse("3.75".to_string())?;
let int = x.integer()?;
assert_eq!(int.format()?, "3");

let y = Float::parse("-3.75".to_string())?;
let int_y = y.integer()?;
assert_eq!(int_y.format()?, "-3");

anyhow::Ok(())
Source

pub fn frac(self) -> Result<Float, FloatError>

Returns the fractional part of the float.

§Returns
  • Ok(Float) - The fractional part.
  • Err(FloatError) - If the operation fails.
§Example
use rain_math_float::Float;

let x = Float::parse("3.75".to_string())?;
let frac = x.frac()?;
assert_eq!(frac.format()?, "0.75");

anyhow::Ok(())
Source

pub fn floor(self) -> Result<Float, FloatError>

Returns the floor of the float.

§Returns
  • Ok(Float) - The floored value.
  • Err(FloatError) - If the operation fails.
§Example
use rain_math_float::Float;

let x = Float::parse("3.75".to_string())?;
let floor = x.floor()?;
assert_eq!(floor.format()?, "3");

anyhow::Ok(())
Source

pub fn min(self, b: Self) -> Result<Self, FloatError>

Returns the minimum of self and b.

§Arguments
  • b - The other Float to compare with.
§Returns
  • Ok(Float) - The minimum value.
  • Err(FloatError) - If the operation fails.
§Example
use rain_math_float::Float;

let a = Float::parse("1.0".to_string())?;
let b = Float::parse("2.0".to_string())?;
let min = a.min(b)?;
assert_eq!(min.format()?, "1");

anyhow::Ok(())
Source

pub fn max(self, b: Self) -> Result<Self, FloatError>

Returns the maximum of self and b.

§Arguments
  • b - The other Float to compare with.
§Returns
  • Ok(Float) - The maximum value.
  • Err(FloatError) - If the operation fails.
§Example
use rain_math_float::Float;

let a = Float::parse("1.0".to_string())?;
let b = Float::parse("2.0".to_string())?;
let max = a.max(b)?;
assert_eq!(max.format()?, "2");

anyhow::Ok(())
Source

pub fn is_zero(self) -> Result<bool, FloatError>

Checks if the float is zero.

§Returns
  • Ok(true) if the float is zero.
  • Ok(false) if the float is not zero.
  • Err(FloatError) if the operation fails.
§Example
use rain_math_float::Float;

let zero = Float::parse("0".to_string())?;
assert!(zero.is_zero()?);
let nonzero = Float::parse("1.23".to_string())?;
assert!(!nonzero.is_zero()?);

anyhow::Ok(())

Trait Implementations§

Source§

impl Add for Float

Source§

fn add(self, b: Self) -> Self::Output

Adds two floats.

§Returns
  • Ok(Float) - The sum.
  • Err(FloatError) - If addition fails.
§Example
use rain_math_float::Float;

let a = Float::parse("1.5".to_string())?;
let b = Float::parse("2.5".to_string())?;
let sum = (a + b)?;
assert_eq!(sum.format()?, "4");

anyhow::Ok(())
Source§

type Output = Result<Float, FloatError>

The resulting type after applying the + operator.
Source§

impl Clone for Float

Source§

fn clone(&self) -> Float

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Float

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Float

Source§

fn default() -> Float

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Float

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Div for Float

Source§

fn div(self, b: Self) -> Self::Output

Divides self by b.

§Returns
  • Ok(Float) - The quotient.
  • Err(FloatError) - If division fails.
§Example
use rain_math_float::Float;

let a = Float::parse("6.0".to_string())?;
let b = Float::parse("2.0".to_string())?;
let quotient = (a / b)?;
assert_eq!(quotient.format()?, "3");

anyhow::Ok(())
Source§

type Output = Result<Float, FloatError>

The resulting type after applying the / operator.
Source§

impl From<FixedBytes<32>> for Float

Source§

fn from(value: B256) -> Self

Converts to this type from the input type.
Source§

impl From<Float> for B256

Source§

fn from(value: Float) -> Self

Converts to this type from the input type.
Source§

impl From<Float> for JsValue

Source§

fn from(value: Float) -> Self

Converts to this type from the input type.
Source§

impl FromWasmAbi for Float

Source§

type Abi = u32

The Wasm ABI type that this converts from when coming back out from the ABI boundary.
Source§

unsafe fn from_abi(js: u32) -> Self

Recover a Self from Self::Abi. Read more
Source§

impl Hash for Float

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IntoWasmAbi for Float

Source§

type Abi = u32

The Wasm ABI type that this converts into when crossing the ABI boundary.
Source§

fn into_abi(self) -> u32

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
Source§

impl LongRefFromWasmAbi for Float

Source§

type Abi = u32

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRef<Float>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl Mul for Float

Source§

fn mul(self, b: Self) -> Self::Output

Multiplies two floats.

§Returns
  • Ok(Float) - The product.
  • Err(FloatError) - If multiplication fails.
§Example
use rain_math_float::Float;

let a = Float::parse("2.0".to_string())?;
let b = Float::parse("3.0".to_string())?;
let product = (a * b)?;
assert_eq!(product.format()?, "6");

anyhow::Ok(())
Source§

type Output = Result<Float, FloatError>

The resulting type after applying the * operator.
Source§

impl Neg for Float

Source§

fn neg(self) -> Self::Output

Returns the negation of the float.

§Returns
  • Ok(Float) - The negated value.
  • Err(FloatError) - If the operation fails.
§Example
use rain_math_float::Float;

let x = Float::parse("3.14".to_string())?;
let neg = (-x)?;
assert_eq!(neg.format()?, "-3.14");

anyhow::Ok(())
Source§

type Output = Result<Float, FloatError>

The resulting type after applying the - operator.
Source§

impl OptionFromWasmAbi for Float

Source§

fn is_none(abi: &Self::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
Source§

impl OptionIntoWasmAbi for Float

Source§

fn none() -> Self::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
Source§

impl RefFromWasmAbi for Float

Source§

type Abi = u32

The Wasm ABI type references to Self are recovered from.
Source§

type Anchor = RcRef<Float>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
Source§

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

Recover a Self::Anchor from Self::Abi. Read more
Source§

impl RefMutFromWasmAbi for Float

Source§

type Abi = u32

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRefMut<Float>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl Serialize for Float

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub for Float

Source§

fn sub(self, b: Self) -> Self::Output

Subtracts b from self.

§Returns
  • Ok(Float) - The difference.
  • Err(FloatError) - If subtraction fails.
§Example
use rain_math_float::Float;

let a = Float::parse("5.0".to_string())?;
let b = Float::parse("2.0".to_string())?;
let diff = (a - b)?;
assert_eq!(diff.format()?, "3");

anyhow::Ok(())
Source§

type Output = Result<Float, FloatError>

The resulting type after applying the - operator.
Source§

impl TryFromJsValue for Float

Source§

type Error = JsValue

The type returned in the event of a conversion error.
Source§

fn try_from_js_value(value: JsValue) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl VectorFromWasmAbi for Float

Source§

type Abi = <Box<[JsValue]> as FromWasmAbi>::Abi

Source§

unsafe fn vector_from_abi(js: Self::Abi) -> Box<[Float]>

Source§

impl VectorIntoJsValue for Float

Source§

impl VectorIntoWasmAbi for Float

Source§

type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi

Source§

fn vector_into_abi(vector: Box<[Float]>) -> Self::Abi

Source§

impl WasmDescribe for Float

Source§

impl WasmDescribeVector for Float

Source§

impl Copy for Float

Source§

impl SupportsConstructor for Float

Source§

impl SupportsInstanceProperty for Float

Source§

impl SupportsStaticProperty for Float

Auto Trait Implementations§

§

impl Freeze for Float

§

impl RefUnwindSafe for Float

§

impl Send for Float

§

impl Sync for Float

§

impl Unpin for Float

§

impl UnsafeUnpin for Float

§

impl UnwindSafe for Float

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pipe for T
where T: ?Sized,

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
Source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
Source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ReturnWasmAbi for T
where T: IntoWasmAbi,

Source§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
Source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<'de, T> BorrowedRpcObject<'de> for T
where T: RpcBorrow<'de> + RpcSend,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<'de, T> RpcBorrow<'de> for T
where T: Deserialize<'de> + Debug + Send + Sync + Unpin,

Source§

impl<T> RpcObject for T
where T: RpcSend + RpcRecv,

Source§

impl<T> RpcRecv for T
where T: DeserializeOwned + Debug + Send + Sync + Unpin + 'static,

Source§

impl<T> RpcSend for T
where T: Serialize + Clone + Debug + Send + Sync + Unpin,