Skip to main content

rain_math_float/
js_api.rs

1use crate::{Float, FloatError};
2use revm::primitives::{B256, U256};
3use serde::{Deserialize, Serialize};
4use std::{
5    ops::{Add, Div, Mul, Neg, Sub},
6    str::FromStr,
7};
8use wasm_bindgen_utils::{
9    impl_wasm_traits,
10    prelude::{js_sys::BigInt, *},
11};
12
13#[wasm_bindgen]
14pub struct FromFixedDecimalLossyResult {
15    float: Float,
16    lossless: bool,
17}
18
19#[wasm_bindgen]
20impl FromFixedDecimalLossyResult {
21    #[wasm_bindgen(getter)]
22    pub fn float(&self) -> Float {
23        self.float
24    }
25
26    #[wasm_bindgen(getter)]
27    pub fn lossless(&self) -> bool {
28        self.lossless
29    }
30}
31
32#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Tsify)]
33pub struct ToFixedDecimalLossyResult {
34    pub value: String,
35    pub lossless: bool,
36}
37impl_wasm_traits!(ToFixedDecimalLossyResult);
38
39#[wasm_bindgen]
40impl Float {
41    /// Returns the 32-byte hexadecimal string representation of the float.
42    ///
43    /// # Returns
44    ///
45    /// * `String` - The 32-byte hex string.
46    ///
47    /// # Example
48    ///
49    /// ```typescript
50    /// const float = Float.fromHex("0x0000000000000000000000000000000000000000000000000000000000000005").value!;
51    /// assert(float.asHex() === "0x0000000000000000000000000000000000000000000000000000000000000005");
52    /// ```
53    #[wasm_bindgen(js_name = "asHex", unchecked_return_type = "`0x${string}`")]
54    pub fn as_hex_js(&self) -> String {
55        self.as_hex()
56    }
57
58    /// Convert the float to a JS/TS bigint equivalent of `asHex()` returned hex string.
59    ///
60    /// # Throws
61    /// if conversion fails.
62    ///
63    /// # Example
64    ///
65    /// ```typescript
66    /// const float = Float.fromHex("0xfffffffe0000000000000000000000000000000000000000000000000000013a");
67    /// const value = float.toBigInt();
68    /// assert(value === 115792089183396302089269705419353877679230723318366275194376439045705909141818n);
69    /// ```
70    #[wasm_bindgen(js_name = "toBigint", unchecked_return_type = "bigint")]
71    pub fn to_bigint(&self) -> BigInt {
72        self.try_to_bigint().unwrap_throw()
73    }
74
75    /// Constructs a `Float` from a bigint equivalent of the `fromHex()` returned Float.
76    ///
77    /// # Throws
78    /// if conversion fails.
79    ///
80    /// # Example
81    ///
82    /// ```typescript
83    /// const float = Float.fromBigint(115792089183396302089269705419353877679230723318366275194376439045705909141818n);
84    /// assert(float.asHex() === "0xfffffffe0000000000000000000000000000000000000000000000000000013a");
85    /// ```
86    #[wasm_bindgen(js_name = "fromBigint")]
87    pub fn from_bigint(value: BigInt) -> Float {
88        Self::try_from_bigint(value).unwrap_throw()
89    }
90
91    /// Converts a fixed-point decimal value to a `Float` using the specified number of decimals,
92    /// allowing lossy conversions and reporting whether precision was preserved.
93    ///
94    /// This function attempts to convert a fixed-point decimal representation to a `Float`.
95    /// Unlike `fromFixedDecimal`, this method will not fail if precision is lost during conversion,
96    /// but instead reports the loss through the `lossless` flag in the result.
97    ///
98    /// # Arguments
99    ///
100    /// * `value` - The fixed-point decimal value as a bigint (e.g., 12345n for 123.45 with 2 decimals).
101    /// * `decimals` - The number of decimal places in the fixed-point representation (0-255).
102    ///
103    /// # Returns
104    ///
105    /// Returns a `FromFixedDecimalLossyResult` containing:
106    /// * `float` - The resulting `Float` value.
107    /// * `lossless` - Boolean flag indicating whether the conversion preserved all precision (true) or was lossy (false).
108    ///
109    /// # Errors
110    ///
111    /// Throws a `JsValue` error if:
112    /// * The bigint value cannot be converted to a string.
113    /// * The value string cannot be parsed as a valid U256.
114    /// * The underlying EVM conversion fails.
115    ///
116    /// # Example
117    ///
118    /// ```typescript
119    /// // Lossless conversion
120    /// const result = Float.fromFixedDecimalLossy(12345n, 2);
121    /// const float = result.float;
122    /// const wasLossless = result.lossless;
123    /// assert(float.format()?.value === "123.45");
124    /// assert(wasLossless === true);
125    ///
126    /// // Potentially lossy conversion
127    /// const result2 = Float.fromFixedDecimalLossy(123456789012345678901234567890n, 18);
128    /// if (!result2.lossless) {
129    ///   console.warn("Precision was lost during conversion");
130    /// }
131    /// ```
132    #[wasm_bindgen(js_name = "fromFixedDecimalLossy")]
133    pub fn from_fixed_decimal_lossy_js(
134        value: BigInt,
135        decimals: u8,
136    ) -> Result<FromFixedDecimalLossyResult, JsValue> {
137        let value_str: String = value.to_string(10).map_err(|e| JsValue::from(&e))?.into();
138        let val = U256::from_str(&value_str).map_err(|e| JsValue::from_str(&e.to_string()))?;
139        let (float, lossless) = Float::from_fixed_decimal_lossy(val, decimals)
140            .map_err(|e| JsValue::from_str(&e.to_string()))?;
141        Ok(FromFixedDecimalLossyResult { float, lossless })
142    }
143}
144
145#[wasm_export]
146impl Float {
147    /// Tries to convert the float to a JS/TS bigint equivalent of `asHex()` returned hex string.
148    ///
149    /// # Returns
150    ///
151    /// * `Ok(bigint)` - The resulting `bigint` value.
152    /// * `Err(FloatError)` - If the conversion fails.
153    ///
154    /// # Example
155    ///
156    /// ```typescript
157    /// const float = Float.fromHex("0xfffffffe0000000000000000000000000000000000000000000000000000013a");
158    /// const bigintResult = float.tryToBigInt();
159    /// if (bigintResult.error) {
160    ///     console.error(bigintResult.error);
161    /// }
162    /// assert(bigintResult.value === 115792089183396302089269705419353877679230723318366275194376439045705909141818n);
163    /// ```
164    #[wasm_export(
165        js_name = "tryToBigint",
166        preserve_js_class,
167        unchecked_return_type = "bigint"
168    )]
169    pub fn try_to_bigint(&self) -> Result<BigInt, FloatError> {
170        Ok(BigInt::from_str(&self.as_hex())?)
171    }
172
173    /// Constructs a `Float` from a bigint equivalent of the `fromHex()` returned Float.
174    ///
175    /// # Returns
176    ///
177    /// * `Ok(Float)` - The resulting `Float` value.
178    /// * `Err(FloatError)` - If the conversion fails.
179    ///
180    /// # Example
181    ///
182    /// ```typescript
183    /// const floatResult = Float.tryFromBigint(115792089183396302089269705419353877679230723318366275194376439045705909141818n);
184    /// if (floatResult.error) {
185    ///   console.error(floatResult.error);
186    /// }
187    /// const value = float.value;
188    /// assert(value.asHex() === "0xfffffffe0000000000000000000000000000000000000000000000000000013a");
189    /// ```
190    #[wasm_export(js_name = "tryFromBigint", preserve_js_class)]
191    pub fn try_from_bigint(value: BigInt) -> Result<Float, FloatError> {
192        // convert to 16 radix string and append 0 if length is odd
193        let mut value: String = value.to_string(16)?.into();
194        if value.len() % 2 == 1 {
195            value = format!("0{}", value);
196        }
197        Ok(Float(B256::left_padding_from(&alloy::hex::decode(&value)?)))
198    }
199
200    /// Converts a fixed-point decimal value to a `Float` using the specified number of decimals.
201    ///
202    /// # Arguments
203    ///
204    /// * `value` - The fixed-point decimal value as a `string`.
205    /// * `decimals` - The number of decimals in the fixed-point representation.
206    ///
207    /// # Returns
208    ///
209    /// * `Ok(Float)` - The resulting `Float` value.
210    /// * `Err(FloatError)` - If the conversion fails.
211    ///
212    /// # Example
213    ///
214    /// ```typescript
215    /// const floatResult = Float.fromFixedDecimal("12345", 2);
216    /// if (floatResult.error) {
217    ///    console.error(floatResult.error);
218    /// }
219    /// const float = floatResult.value;
220    /// assert(float.format() === "123.45");
221    /// ```
222    #[wasm_export(js_name = "fromFixedDecimal", preserve_js_class)]
223    pub fn from_fixed_decimal_js(value: BigInt, decimals: u8) -> Result<Float, FloatError> {
224        let value_str: String = value.to_string(10)?.into();
225        let val = U256::from_str(&value_str)?;
226        Self::from_fixed_decimal(val, decimals)
227    }
228
229    /// Converts a `Float` to a fixed-point decimal value using the specified number of decimals.
230    ///
231    /// # Arguments
232    ///
233    /// * `decimals` - The number of decimals in the fixed-point representation.
234    ///
235    /// # Returns
236    ///
237    /// * `Ok(String)` - The resulting fixed-point decimal value as a string.
238    /// * `Err(FloatError)` - If the conversion fails.
239    ///
240    /// # Example
241    ///
242    /// ```typescript
243    /// const float = Float.parse("123.45").value!;
244    /// const result = float.toFixedDecimal(2);
245    /// if (result.error) {
246    ///    console.error(result.error);
247    /// }
248    /// assert(result.value === "12345");
249    /// ```
250    #[wasm_export(
251        js_name = "toFixedDecimal",
252        preserve_js_class,
253        unchecked_return_type = "bigint"
254    )]
255    pub fn to_fixed_decimal_js(&self, decimals: u8) -> Result<BigInt, FloatError> {
256        let fixed = self.to_fixed_decimal(decimals)?;
257        BigInt::from_str(&fixed.to_string())
258            .map_err(|e| FloatError::JsSysError(e.to_string().into()))
259    }
260
261    /// Converts a `Float` to a fixed-point decimal value using the specified number of decimals lossy.
262    ///
263    /// # Returns
264    ///
265    /// ToFixedDecimalLossyResult containing the value and lossless flag.
266    #[wasm_export(
267        js_name = "toFixedDecimalLossy",
268        preserve_js_class,
269        unchecked_return_type = "ToFixedDecimalLossyResult"
270    )]
271    pub fn to_fixed_decimal_lossy_js(
272        &self,
273        decimals: u8,
274    ) -> Result<ToFixedDecimalLossyResult, FloatError> {
275        let (fixed, lossless) = self.to_fixed_decimal_lossy(decimals)?;
276        Ok(ToFixedDecimalLossyResult {
277            value: fixed.to_string(),
278            lossless,
279        })
280    }
281
282    /// Parses a decimal string into a `Float`.
283    ///
284    /// # Arguments
285    ///
286    /// * `str` - The string to parse.
287    ///
288    /// # Returns
289    ///
290    /// * `Ok(Float)` - The parsed float.
291    /// * `Err(FloatError)` - If parsing fails.
292    ///
293    /// # Example
294    ///
295    /// ```typescript
296    /// const floatResult = Float.parse("3.1415");
297    /// if (floatResult.error) {
298    ///    console.error(floatResult.error);
299    /// }
300    /// const float = floatResult.value;
301    /// assert(float.format() === "3.1415");
302    /// ```
303    #[wasm_export(js_name = "parse", preserve_js_class)]
304    pub fn parse_js(str: String) -> Result<Float, FloatError> {
305        Self::parse(str)
306    }
307
308    /// Constructs a `Float` from a 32-byte hexadecimal string.
309    ///
310    /// # Arguments
311    ///
312    /// * `hex` - The 32-byte hex string to parse.
313    ///
314    /// # Returns
315    ///
316    /// * `Ok(Float)` - The float parsed from the hex string.
317    /// * `Err(FloatError)` - If the hex string is not valid or not 32 bytes.
318    ///
319    /// # Example
320    ///
321    /// ```typescript
322    /// const floatResult = Float.fromHex("0x0000000000000000000000000000000000000000000000000000000000000005");
323    /// if (floatResult.error) {
324    ///    console.error(floatResult.error);
325    /// }
326    /// const float = floatResult.value;
327    /// assert(float.asHex() === "0x0000000000000000000000000000000000000000000000000000000000000005");
328    /// ```
329    #[wasm_export(js_name = "fromHex", preserve_js_class)]
330    pub fn from_hex_js(
331        #[wasm_export(unchecked_param_type = "`0x${string}`")] hex: &str,
332    ) -> Result<Float, FloatError> {
333        Self::from_hex(hex)
334    }
335
336    /// Returns the maximum positive value that can be represented as a `Float`.
337    ///
338    /// # Returns
339    ///
340    /// * `Ok(Float)` - The maximum positive value.
341    /// * `Err(FloatError)` - If the EVM call fails.
342    ///
343    /// # Example
344    ///
345    /// ```typescript
346    /// const maxPosResult = Float.maxPositiveValue();
347    /// if (maxPosResult.error) {
348    ///    console.error(maxPosResult.error);
349    /// }
350    /// const maxPos = maxPosResult.value;
351    /// assert(!maxPos.format().error);
352    /// ```
353    #[wasm_export(js_name = "maxPositiveValue", preserve_js_class)]
354    pub fn max_positive_value_js() -> Result<Float, FloatError> {
355        Self::max_positive_value()
356    }
357
358    /// Returns the minimum positive value that can be represented as a `Float`.
359    ///
360    /// # Returns
361    ///
362    /// * `Ok(Float)` - The minimum positive value.
363    /// * `Err(FloatError)` - If the EVM call fails.
364    ///
365    /// # Example
366    ///
367    /// ```typescript
368    /// const minPosResult = Float.minPositiveValue();
369    /// if (minPosResult.error) {
370    ///    console.error(minPosResult.error);
371    /// }
372    /// const minPos = minPosResult.value;
373    /// assert(!minPos.format().error);
374    /// ```
375    #[wasm_export(js_name = "minPositiveValue", preserve_js_class)]
376    pub fn min_positive_value_js() -> Result<Float, FloatError> {
377        Self::min_positive_value()
378    }
379
380    /// Returns the maximum negative value that can be represented as a `Float`.
381    ///
382    /// # Returns
383    ///
384    /// * `Ok(Float)` - The maximum negative value (closest to zero).
385    /// * `Err(FloatError)` - If the EVM call fails.
386    ///
387    /// # Example
388    ///
389    /// ```typescript
390    /// const maxNegResult = Float.maxNegativeValue();
391    /// if (maxNegResult.error) {
392    ///    console.error(maxNegResult.error);
393    /// }
394    /// const maxNeg = maxNegResult.value;
395    /// assert(!maxNeg.format().error);
396    /// ```
397    #[wasm_export(js_name = "maxNegativeValue", preserve_js_class)]
398    pub fn max_negative_value_js() -> Result<Float, FloatError> {
399        Self::max_negative_value()
400    }
401
402    /// Returns the minimum negative value that can be represented as a `Float`.
403    ///
404    /// # Returns
405    ///
406    /// * `Ok(Float)` - The minimum negative value (furthest from zero).
407    /// * `Err(FloatError)` - If the EVM call fails.
408    ///
409    /// # Example
410    ///
411    /// ```typescript
412    /// const minNegResult = Float.minNegativeValue();
413    /// if (minNegResult.error) {
414    ///    console.error(minNegResult.error);
415    /// }
416    /// const minNeg = minNegResult.value;
417    /// assert(!minNeg.format().error);
418    /// ```
419    #[wasm_export(js_name = "minNegativeValue", preserve_js_class)]
420    pub fn min_negative_value_js() -> Result<Float, FloatError> {
421        Self::min_negative_value()
422    }
423
424    /// Returns the zero value of a `Float` in its maximized representation.
425    ///
426    /// # Returns
427    ///
428    /// * `Ok(Float)` - The zero value.
429    /// * `Err(FloatError)` - If the EVM call fails.
430    ///
431    /// # Example
432    ///
433    /// ```typescript
434    /// const zeroResult = Float.zero();
435    /// if (zeroResult.error) {
436    ///    console.error(zeroResult.error);
437    /// }
438    /// const zero = zeroResult.value;
439    /// assert(zero.isZero().value);
440    /// assert(zero.format().value === "0");
441    /// ```
442    #[wasm_export(js_name = "zero", preserve_js_class)]
443    pub fn zero_js() -> Result<Float, FloatError> {
444        Self::zero()
445    }
446
447    /// Returns the default minimum value for scientific notation formatting (1e-4).
448    ///
449    /// # Returns
450    ///
451    /// * `Ok(Float)` - The default minimum (1e-4).
452    /// * `Err(FloatError)` - If the EVM call fails.
453    ///
454    /// # Example
455    ///
456    /// ```typescript
457    /// const minResult = Float.formatDefaultScientificMin();
458    /// if (minResult.error) {
459    ///    console.error(minResult.error);
460    /// }
461    /// const min = minResult.value;
462    /// assert(min.format().value === "0.0001");
463    /// ```
464    #[wasm_export(js_name = "formatDefaultScientificMin", preserve_js_class)]
465    pub fn format_default_scientific_min_js() -> Result<Float, FloatError> {
466        Self::format_default_scientific_min()
467    }
468
469    /// Returns the default maximum value for scientific notation formatting (1e9).
470    ///
471    /// # Returns
472    ///
473    /// * `Ok(Float)` - The default maximum (1e9).
474    /// * `Err(FloatError)` - If the EVM call fails.
475    ///
476    /// # Example
477    ///
478    /// ```typescript
479    /// const maxResult = Float.formatDefaultScientificMax();
480    /// if (maxResult.error) {
481    ///    console.error(maxResult.error);
482    /// }
483    /// const max = maxResult.value;
484    /// assert(max.format().value === "1000000000");
485    /// ```
486    #[wasm_export(js_name = "formatDefaultScientificMax", preserve_js_class)]
487    pub fn format_default_scientific_max_js() -> Result<Float, FloatError> {
488        Self::format_default_scientific_max()
489    }
490
491    /// Formats the float as a decimal string using default scientific notation range (1e-4 to 1e9).
492    ///
493    /// # Returns
494    ///
495    /// * `Ok(String)` - The formatted string.
496    /// * `Err(FloatError)` - If formatting fails.
497    ///
498    /// # Example
499    ///
500    /// ```typescript
501    /// const floatResult = Float.parse("2.5");
502    /// if (floatResult.error) {
503    ///    console.error(floatResult.error);
504    /// }
505    /// const float = floatResult.value;
506    /// const formatResult = float.format();
507    /// assert(formatResult.value === "2.5");
508    /// ```
509    #[wasm_export(js_name = "format")]
510    pub fn format_js(&self) -> Result<String, FloatError> {
511        self.format()
512    }
513
514    /// Formats the float as a decimal string with explicit scientific notation control.
515    ///
516    /// # Arguments
517    ///
518    /// * `scientific` - If true, always use scientific notation. If false, use decimal notation.
519    ///
520    /// # Returns
521    ///
522    /// * `Ok(String)` - The formatted string.
523    /// * `Err(FloatError)` - If formatting fails.
524    ///
525    /// # Example
526    ///
527    /// ```typescript
528    /// const float = Float.parse("123.456").value!;
529    ///
530    /// const decResult = float.formatWithScientific(false);
531    /// assert(decResult.value === "123.456");
532    ///
533    /// const sciResult = float.formatWithScientific(true);
534    /// assert(sciResult.value === "1.23456e2");
535    /// ```
536    #[wasm_export(js_name = "formatWithScientific")]
537    pub fn format_with_scientific_js(&self, scientific: bool) -> Result<String, FloatError> {
538        self.format_with_scientific(scientific)
539    }
540
541    /// Formats the float as a decimal string with a custom scientific notation range.
542    ///
543    /// # Arguments
544    ///
545    /// * `scientific_min` - Values smaller than this (in absolute value) use scientific notation.
546    /// * `scientific_max` - Values larger than this (in absolute value) use scientific notation.
547    ///
548    /// # Returns
549    ///
550    /// * `Ok(String)` - The formatted string.
551    /// * `Err(FloatError)` - If formatting fails.
552    ///
553    /// # Example
554    ///
555    /// ```typescript
556    /// const float = Float.parse("0.5").value!;
557    /// const min = Float.parse("1").value!;
558    /// const max = Float.parse("100").value!;
559    /// const result = float.formatWithRange(min, max);
560    /// assert(result.value === "5e-1");
561    /// ```
562    #[wasm_export(js_name = "formatWithRange")]
563    pub fn format_with_range_js(
564        &self,
565        scientific_min: &Self,
566        scientific_max: &Self,
567    ) -> Result<String, FloatError> {
568        self.format_with_range(*scientific_min, *scientific_max)
569    }
570
571    /// Returns `true` if `self` is less than `b`.
572    ///
573    /// # Arguments
574    ///
575    /// * `b` - The `Float` value to compare with `self`.
576    ///
577    /// # Returns
578    ///
579    /// * `Ok(true)` if `self` is less than `b`.
580    /// * `Ok(false)` if `self` is not less than `b`.
581    /// * `Err(FloatError)` if the comparison fails due to an error in the underlying EVM call or decoding.
582    ///
583    /// # Example
584    ///
585    /// ```typescript
586    /// const a = Float.parse("1.0").value!;
587    /// const b = Float.parse("2.0").value!;
588    /// const result = a.lt(b);
589    /// if (result.error) {
590    ///    console.error(result.error);
591    /// }
592    /// assert(result.value);
593    /// ```
594    #[wasm_export(js_name = "lt", unchecked_return_type = "boolean")]
595    pub fn lt_js(&self, b: &Self) -> Result<bool, FloatError> {
596        self.lt(*b)
597    }
598
599    /// Returns `true` if `self` is equal to `b`.
600    ///
601    /// # Arguments
602    ///
603    /// * `b` - The `Float` value to compare with `self`.
604    ///
605    /// # Returns
606    ///
607    /// * `Ok(true)` if `self` is equal to `b`.
608    /// * `Ok(false)` if `self` is not equal to `b`.
609    /// * `Err(FloatError)` if the comparison fails due to an error in the underlying EVM call or decoding.
610    ///
611    /// # Example
612    ///
613    /// ```typescript
614    /// const a = Float.parse("2.0").value!;
615    /// const b = Float.parse("2.0").value!;
616    /// const result = a.eq(b);
617    /// if (result.error) {
618    ///    console.error(result.error);
619    /// }
620    /// assert(result.value);
621    /// ```
622    #[wasm_export(js_name = "eq", unchecked_return_type = "boolean")]
623    pub fn eq_js(&self, b: &Self) -> Result<bool, FloatError> {
624        self.eq(*b)
625    }
626
627    /// Returns `true` if `self` is less than or equal to `b`.
628    ///
629    /// # Arguments
630    ///
631    /// * `b` - The `Float` value to compare with `self`.
632    ///
633    /// # Returns
634    ///
635    /// * `Ok(true)` if `self` is less than or equal to `b`.
636    /// * `Ok(false)` if `self` is not less than or equal to `b`.
637    /// * `Err(FloatError)` if the comparison fails due to an error in the underlying EVM call or decoding.
638    ///
639    /// # Example
640    ///
641    /// ```typescript
642    /// const a = Float.parse("1.0").value!;
643    /// const b = Float.parse("2.0").value!;
644    /// const result = a.lte(b);
645    /// if (result.error) {
646    ///    console.error(result.error);
647    /// }
648    /// assert(result.value);
649    /// ```
650    #[wasm_export(js_name = "lte", unchecked_return_type = "boolean")]
651    pub fn lte_js(&self, b: &Self) -> Result<bool, FloatError> {
652        self.lte(*b)
653    }
654
655    /// Returns `true` if `self` is greater than or equal to `b`.
656    ///
657    /// # Arguments
658    ///
659    /// * `b` - The `Float` value to compare with `self`.
660    ///
661    /// # Returns
662    ///
663    /// * `Ok(true)` if `self` is greater than or equal to `b`.
664    /// * `Ok(false)` if `self` is not greater than or equal to `b`.
665    /// * `Err(FloatError)` if the comparison fails due to an error in the underlying EVM call or decoding.
666    ///
667    /// # Example
668    ///
669    /// ```typescript
670    /// const a = Float.parse("2.0").value!;
671    /// const b = Float.parse("1.0").value!;
672    /// const result = a.gte(b);
673    /// if (result.error) {
674    ///    console.error(result.error);
675    /// }
676    /// assert(result.value);
677    /// ```
678    #[wasm_export(js_name = "gte", unchecked_return_type = "boolean")]
679    pub fn gte_js(&self, b: &Self) -> Result<bool, FloatError> {
680        self.gte(*b)
681    }
682
683    /// Returns `true` if `self` is greater than `b`.
684    ///
685    /// # Arguments
686    ///
687    /// * `b` - The `Float` value to compare with `self`.
688    ///
689    /// # Returns
690    ///
691    /// * `Ok(true)` if `self` is greater than `b`.
692    /// * `Ok(false)` if `self` is not greater than `b`.
693    /// * `Err(FloatError)` if the comparison fails due to an error in the underlying EVM call or decoding.
694    ///
695    /// # Example
696    ///
697    /// ```typescript
698    /// const a = Float.parse("2.0").value!;
699    /// const b = Float.parse("1.0").value!;
700    /// const result = a.gt(b);
701    /// if (result.error) {
702    ///    console.error(result.error);
703    /// }
704    /// assert(result.value);
705    /// ```
706    #[wasm_export(js_name = "gt", unchecked_return_type = "boolean")]
707    pub fn gt_js(&self, b: &Self) -> Result<bool, FloatError> {
708        self.gt(*b)
709    }
710
711    /// Returns the multiplicative inverse of the float.
712    ///
713    /// # Returns
714    ///
715    /// * `Ok(Float)` - The inverse.
716    /// * `Err(FloatError)` - If inversion fails.
717    ///
718    /// # Example
719    ///
720    /// ```typescript
721    /// const x = Float.parse("2.0").value!;
722    /// const inv = x.inv();
723    /// if (inv.error) {
724    ///    console.error(inv.error);
725    /// }
726    /// assert(inv.value.format().startsWith("0.5"));
727    /// ```
728    #[wasm_export(js_name = "inv", preserve_js_class)]
729    pub fn inv_js(&self) -> Result<Float, FloatError> {
730        self.inv()
731    }
732
733    /// Returns the absolute value of the float.
734    ///
735    /// # Returns
736    ///
737    /// * `Ok(Float)` - The absolute value.
738    /// * `Err(FloatError)` - If the operation fails.
739    ///
740    /// # Example
741    ///
742    /// ```typescript
743    /// const x = Float.parse("-3.14").value!;
744    /// const abs = x.abs();
745    /// if (abs.error) {
746    ///    console.error(abs.error);
747    /// }
748    /// assert(abs.value.format() === "3.14");
749    /// ```
750    #[wasm_export(js_name = "abs", preserve_js_class)]
751    pub fn abs_js(&self) -> Result<Float, FloatError> {
752        self.abs()
753    }
754
755    /// Adds two floats.
756    ///
757    /// # Returns
758    ///
759    /// * `Ok(Float)` - The sum.
760    /// * `Err(FloatError)` - If addition fails.
761    ///
762    /// # Example
763    ///
764    /// ```typescript
765    /// const a = Float.parse("1.5").value!;
766    /// const b = Float.parse("2.5").value!;
767    /// const result = a.add(b);
768    /// if (result.error) {
769    ///    console.error(result.error);
770    /// }
771    /// assert(result.value.format() === "4");
772    /// ```
773    #[wasm_export(js_name = "add", preserve_js_class)]
774    pub fn add_js(&self, b: &Self) -> Result<Float, FloatError> {
775        self.add(*b)
776    }
777
778    /// Subtracts `b` from `self`.
779    ///
780    /// # Returns
781    ///
782    /// * `Ok(Float)` - The difference.
783    /// * `Err(FloatError)` - If subtraction fails.
784    ///
785    /// # Example
786    ///
787    /// ```typescript
788    /// const a = Float.parse("5.0").value!;
789    /// const b = Float.parse("2.0").value!;
790    /// const result = a.sub(b);
791    /// if (result.error) {
792    ///    console.error(result.error);
793    /// }
794    /// assert(result.value.format() === "3");
795    /// ```
796    #[wasm_export(js_name = "sub", preserve_js_class)]
797    pub fn sub_js(&self, b: &Self) -> Result<Float, FloatError> {
798        self.sub(*b)
799    }
800
801    /// Multiplies two floats.
802    ///
803    /// # Returns
804    ///
805    /// * `Ok(Float)` - The product.
806    /// * `Err(FloatError)` - If multiplication fails.
807    ///
808    /// # Example
809    ///
810    /// ```typescript
811    /// const a = Float.parse("2.0").value!;
812    /// const b = Float.parse("3.0").value!;
813    /// const result = a.mul(b);
814    /// if (result.error) {
815    ///    console.error(result.error);
816    /// }
817    /// assert(result.value.format() === "6");
818    /// ```
819    #[wasm_export(js_name = "mul", preserve_js_class)]
820    pub fn mul_js(&self, b: &Self) -> Result<Float, FloatError> {
821        self.mul(*b)
822    }
823
824    /// Divides `self` by `b`.
825    ///
826    /// # Returns
827    ///
828    /// * `Ok(Float)` - The quotient.
829    /// * `Err(FloatError)` - If division fails.
830    ///
831    /// # Example
832    ///
833    /// ```typescript
834    /// const a = Float.parse("6.0").value!;
835    /// const b = Float.parse("2.0").value!;
836    /// const result = a.div(b);
837    /// if (result.error) {
838    ///    console.error(result.error);
839    /// }
840    /// assert(result.value.format() === "3");
841    /// ```
842    #[wasm_export(js_name = "div", preserve_js_class)]
843    pub fn div_js(&self, b: &Self) -> Result<Float, FloatError> {
844        self.div(*b)
845    }
846
847    /// Returns the fractional part of the float.
848    ///
849    /// # Returns
850    ///
851    /// * `Ok(Float)` - The fractional part.
852    /// * `Err(FloatError)` - If the operation fails.
853    ///
854    /// # Example
855    ///
856    /// ```typescript
857    /// const x = Float.parse("3.75").value!;
858    /// const result = x.frac();
859    /// if (result.error) {
860    ///    console.error(result.error);
861    /// }
862    /// assert(result.value.format() === "0.75");
863    /// ```
864    #[wasm_export(js_name = "frac", preserve_js_class)]
865    pub fn frac_js(&self) -> Result<Float, FloatError> {
866        self.frac()
867    }
868
869    /// Returns the floor of the float.
870    ///
871    /// # Returns
872    ///
873    /// * `Ok(Float)` - The floored value.
874    /// * `Err(FloatError)` - If the operation fails.
875    ///
876    /// # Example
877    ///
878    /// ```typescript
879    /// const x = Float.parse("3.75").value!;
880    /// const result = x.floor();
881    /// if (result.error) {
882    ///    console.error(result.error);
883    /// }
884    /// assert(result.value.format() === "3");
885    /// ```
886    #[wasm_export(js_name = "floor", preserve_js_class)]
887    pub fn floor_js(&self) -> Result<Float, FloatError> {
888        self.floor()
889    }
890
891    /// Returns the minimum of `self` and `b`.
892    ///
893    /// # Arguments
894    ///
895    /// * `b` - The other `Float` to compare with.
896    ///
897    /// # Returns
898    ///
899    /// * `Ok(Float)` - The minimum value.
900    /// * `Err(FloatError)` - If the operation fails.
901    ///
902    /// # Example
903    ///
904    /// ```typescript
905    /// const a = Float.parse("1.0").value!;
906    /// const b = Float.parse("2.0").value!;
907    /// const result = a.min(b);
908    /// if (result.error) {
909    ///    console.error(result.error);
910    /// }
911    /// assert(result.value.format() === "1");
912    /// ```
913    #[wasm_export(js_name = "min", preserve_js_class)]
914    pub fn min_js(&self, b: &Self) -> Result<Float, FloatError> {
915        self.min(*b)
916    }
917
918    /// Returns the maximum of `self` and `b`.
919    ///
920    /// # Arguments
921    ///
922    /// * `b` - The other `Float` to compare with.
923    ///
924    /// # Returns
925    ///
926    /// * `Ok(Float)` - The maximum value.
927    /// * `Err(FloatError)` - If the operation fails.
928    ///
929    /// # Example
930    ///
931    /// ```typescript
932    /// const a = Float.parse("1.0").value!;
933    /// const b = Float.parse("2.0").value!;
934    /// const result = a.max(b);
935    /// if (result.error) {
936    ///    console.error(result.error);
937    /// }
938    /// assert(result.value.format() === "2");
939    /// ```
940    #[wasm_export(js_name = "max", preserve_js_class)]
941    pub fn max_js(&self, b: &Self) -> Result<Float, FloatError> {
942        self.max(*b)
943    }
944
945    /// Checks if the float is zero.
946    ///
947    /// # Returns
948    ///
949    /// * `Ok(true)` if the float is zero.
950    /// * `Ok(false)` if the float is not zero.
951    /// * `Err(FloatError)` if the operation fails.
952    ///
953    /// # Example
954    ///
955    /// ```typescript
956    /// const zero = Float.parse("0").value!;
957    /// const result = zero.isZero();
958    /// if (result.error) {
959    ///    console.error(result.error);
960    /// }
961    /// assert(result.value);
962    /// ```
963    #[wasm_export(js_name = "isZero", unchecked_return_type = "boolean")]
964    pub fn is_zero_js(&self) -> Result<bool, FloatError> {
965        self.is_zero()
966    }
967
968    /// Returns the negation of the float.
969    ///
970    /// # Returns
971    ///
972    /// * `Ok(Float)` - The negated value.
973    /// * `Err(FloatError)` - If the operation fails.
974    ///
975    /// # Example
976    ///
977    /// ```typescript
978    /// const x = Float.parse("3.14").value!;
979    /// const result = x.neg();
980    /// if (result.error) {
981    ///    console.error(result.error);
982    /// }
983    /// assert(result.value.format() === "-3.14");
984    /// ```
985    #[wasm_export(js_name = "neg", preserve_js_class)]
986    pub fn neg_js(&self) -> Result<Float, FloatError> {
987        self.neg()
988    }
989}