Raindex SDK Documentation
    Preparing search index...

    Class Float

    Index

    Methods

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

      Returns WasmEncodedResult<Float>

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

      Parameters

      Returns WasmEncodedResult<Float>

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

      Returns `0x${string}`

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

      Parameters

      Returns WasmEncodedResult<Float>

    • 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);

      Parameters

      Returns WasmEncodedResult<boolean>

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

      Returns WasmEncodedResult<Float>

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

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

      Parameters

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

      Parameters

      • scientific: boolean

      Returns WasmEncodedResult<String>

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

      Returns WasmEncodedResult<Float>

    • Returns void

    • 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);

      Parameters

      Returns WasmEncodedResult<boolean>

    • 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);

      Parameters

      Returns WasmEncodedResult<boolean>

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

      Returns WasmEncodedResult<Float>

    • 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);

      Returns WasmEncodedResult<boolean>

    • 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);

      Parameters

      Returns WasmEncodedResult<boolean>

    • 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);

      Parameters

      Returns WasmEncodedResult<boolean>

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

      Parameters

      Returns WasmEncodedResult<Float>

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

      Parameters

      Returns WasmEncodedResult<Float>

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

      Parameters

      Returns WasmEncodedResult<Float>

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

      Returns WasmEncodedResult<Float>

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

      Parameters

      Returns WasmEncodedResult<Float>

    • 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);

      Returns bigint

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

      Parameters

      • decimals: number

      Returns WasmEncodedResult<bigint>

    • 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);

      Returns WasmEncodedResult<bigint>

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

      Returns WasmEncodedResult<Float>

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

      Returns WasmEncodedResult<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");

      Parameters

      • value: bigint

      Returns Float

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

      Parameters

      • value: bigint
      • decimals: number

      Returns WasmEncodedResult<Float>

    • 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");
      }

      Parameters

      • value: bigint
      • decimals: number

      Returns FromFixedDecimalLossyResult

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

      Parameters

      • hex: `0x${string}`

      Returns WasmEncodedResult<Float>

    • 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);

      Returns WasmEncodedResult<Float>

    • 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);

      Returns WasmEncodedResult<Float>

    • 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);

      Returns WasmEncodedResult<Float>

    • 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);

      Returns WasmEncodedResult<Float>

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

      Parameters

      • str: string

      Returns WasmEncodedResult<Float>

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

      Parameters

      • value: bigint

      Returns WasmEncodedResult<Float>

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

      Returns WasmEncodedResult<Float>