xrpl_hooks/api/
float.rs

1use super::*;
2
3/// XFL floating point numbers
4#[derive(Clone, Copy)]
5pub struct XFL(pub(super) i64 /* enclosing number */);
6
7/// Create a float from an exponent and mantissa
8#[inline(always)]
9pub fn float_set(exponent: i32, mantissa: i64) -> Result<XFL> {
10    let res = unsafe { _c::float_set(exponent, mantissa) };
11
12    result_xfl(res)
13}
14
15/// Multiply two XFL numbers together
16#[inline(always)]
17pub fn float_multiply(float1: XFL, float2: XFL) -> Result<XFL> {
18    let res = unsafe { _c::float_multiply(float1.0, float2.0) };
19
20    result_xfl(res)
21}
22
23/// Multiply an XFL floating point by a non-XFL numerator and denominator
24#[inline(always)]
25pub fn float_mulratio(
26    float1: XFL,
27    round_up: bool,
28    numerator: u32,
29    denominator: u32,
30) -> Result<XFL> {
31    let res = unsafe { _c::float_mulratio(float1.0, round_up as _, numerator, denominator) };
32
33    result_xfl(res)
34}
35
36/// Negate an XFL floating point number
37#[inline(always)]
38pub fn float_negate(float: XFL) -> Result<XFL> {
39    let res = unsafe { _c::float_negate(float.0) };
40
41    result_xfl(res)
42}
43
44/// XFL compare mode
45#[allow(missing_docs)]
46#[derive(Clone, Copy)]
47pub enum XFLCompareMode {
48    Less,
49    Equal,
50    Greater,
51    NotEqual,
52    LessOrEqual,
53    GreaterOrEqual,
54}
55
56/// Perform a comparison on two XFL floating point numbers
57#[inline(always)]
58pub fn float_compare(float1: XFL, float2: XFL, mode: XFLCompareMode) -> Result<bool> {
59    let mode = match mode {
60        XFLCompareMode::Less => _c::COMPARE_LESS,
61        XFLCompareMode::Equal => _c::COMPARE_EQUAL,
62        XFLCompareMode::Greater => _c::COMPARE_GREATER,
63        XFLCompareMode::NotEqual => _c::COMPARE_LESS | _c::COMPARE_GREATER,
64        XFLCompareMode::LessOrEqual => _c::COMPARE_LESS | _c::COMPARE_EQUAL,
65        XFLCompareMode::GreaterOrEqual => _c::COMPARE_GREATER | _c::COMPARE_EQUAL,
66    };
67
68    let res = unsafe { _c::float_compare(float1.0, float2.0, mode) };
69
70    match res {
71        0 => Ok(false),
72        1 => Ok(true),
73        _ => Err(Error::from_code(res as _)),
74    }
75}
76
77/// Add two XFL numbers together
78#[inline(always)]
79pub fn float_sum(float1: XFL, float2: XFL) -> Result<XFL> {
80    let res = unsafe { _c::float_sum(float1.0, float2.0) };
81
82    result_xfl(res)
83}
84
85/// Output an XFL as a serialized object
86#[inline(always)]
87pub fn float_sto(
88    amount: &mut [u8],
89    currency_code: &[u8],
90    issuer_accid: &[u8],
91    float: XFL,
92    field_code: FieldId,
93) -> Result<u64> {
94    let res = unsafe {
95        _c::float_sto(
96            amount.as_mut_ptr() as _,
97            amount.len() as _,
98            currency_code.as_ptr() as _,
99            currency_code.len() as _,
100            issuer_accid.as_ptr() as _,
101            issuer_accid.len() as _,
102            float.0,
103            field_code as _,
104        )
105    };
106
107    result_u64(res)
108}
109
110/// Read a serialized amount into an XFL
111#[inline(always)]
112pub fn float_sto_set(sto_xfl: &[u8]) -> Result<XFL> {
113    let res = unsafe { _c::float_sto_set(sto_xfl.as_ptr() as _, sto_xfl.len() as _) };
114
115    result_xfl(res)
116}
117
118/// Divide one by an XFL floating point number
119#[inline(always)]
120pub fn float_invert(float: XFL) -> Result<XFL> {
121    let res = unsafe { _c::float_invert(float.0) };
122
123    result_xfl(res)
124}
125
126/// Divide an XFL by another XFL floating point number
127#[inline(always)]
128pub fn float_divide(float1: XFL, float2: XFL) -> Result<XFL> {
129    let res = unsafe { _c::float_divide(float1.0, float2.0) };
130
131    result_xfl(res)
132}
133
134/// Return the number 1 represented in an XFL enclosing number
135#[inline(always)]
136pub fn float_one() -> XFL {
137    XFL(unsafe { _c::float_one() })
138}
139
140/// Get the exponent of an XFL enclosing number
141#[inline(always)]
142pub fn float_exponent(float: XFL) -> i64 {
143    unsafe { _c::float_exponent(float.0) }
144}
145
146/// Get the mantissa of an XFL enclosing number
147#[inline(always)]
148pub fn float_mantissa(float: XFL) -> i64 {
149    unsafe { _c::float_mantissa(float.0) }
150}
151
152/// Get the sign of an XFL enclosing number
153#[inline(always)]
154pub fn float_sign(float: XFL) -> Result<bool> {
155    match unsafe { _c::float_sign(float.0) } {
156        0 => Ok(false),
157        1 => Ok(true),
158        res => Err(Error::from_code(res as _)),
159    }
160}
161
162/// Set the exponent of an XFL enclosing number
163#[inline(always)]
164pub fn float_exponent_set(float: XFL, exponent: i32) -> Result<XFL> {
165    let res = unsafe { _c::float_exponent_set(float.0, exponent) };
166
167    result_xfl(res)
168}
169
170/// Set the mantissa of an XFL enclosing number
171#[inline(always)]
172pub fn float_mantissa_set(float: XFL, mantissa: i64) -> Result<XFL> {
173    let res = unsafe { _c::float_mantissa_set(float.0, mantissa) };
174
175    result_xfl(res)
176}
177
178/// Set the sign of an XFL enclosing number
179#[inline(always)]
180pub fn float_sign_set(float: XFL, sign: bool) -> XFL {
181    XFL(unsafe { _c::float_sign_set(float.0, sign as _) })
182}
183
184/// Convert an XFL floating point into an integer (floor)
185#[inline(always)]
186pub fn float_int(float: XFL, decimal_places: u32, absolute: bool) -> Result<u64> {
187    let res = unsafe { _c::float_int(float.0, decimal_places, absolute as _) };
188
189    result_u64(res)
190}