diff --git a/crates/api_clients/grok_client/src/requests/index_page/signature/mod.rs b/crates/api_clients/grok_client/src/requests/index_page/signature/mod.rs index 794da944a6..c7306e3774 100644 --- a/crates/api_clients/grok_client/src/requests/index_page/signature/mod.rs +++ b/crates/api_clients/grok_client/src/requests/index_page/signature/mod.rs @@ -3,4 +3,6 @@ pub mod generate_sign_for_new_conversation; mod signature_xa; mod signature_simulate_style; mod signature_h; -mod signature_cubic_bezier_eased; \ No newline at end of file +mod signature_cubic_bezier_eased; +mod signature_to_hex; +mod round_two_decimals; \ No newline at end of file diff --git a/crates/api_clients/grok_client/src/requests/index_page/signature/round_two_decimals.rs b/crates/api_clients/grok_client/src/requests/index_page/signature/round_two_decimals.rs new file mode 100644 index 0000000000..79ef4be60a --- /dev/null +++ b/crates/api_clients/grok_client/src/requests/index_page/signature/round_two_decimals.rs @@ -0,0 +1,19 @@ + +pub fn round_two_decimals(f: f64) -> f64 { + // rounded = round(float(f), 2) + let rounded = (f * 100.0).round() / 100.0; // NB: Round to two places like Python + rounded +} + +#[cfg(test)] +mod tests { + use crate::requests::index_page::signature::round_two_decimals::round_two_decimals; + + #[test] + fn test() { + assert_eq!(round_two_decimals(0.1234), 0.12); + assert_eq!(round_two_decimals(1.00001), 1.0); + assert_eq!(round_two_decimals(-5.0003), -5.0); + assert_eq!(round_two_decimals(1234.0001), 1234.0); + } +} \ No newline at end of file diff --git a/crates/api_clients/grok_client/src/requests/index_page/signature/signature_h.rs b/crates/api_clients/grok_client/src/requests/index_page/signature/signature_h.rs index e5be67d6db..e1e28be59d 100644 --- a/crates/api_clients/grok_client/src/requests/index_page/signature/signature_h.rs +++ b/crates/api_clients/grok_client/src/requests/index_page/signature/signature_h.rs @@ -1,5 +1,5 @@ use crate::error::grok_client_error::GrokClientError; - +use crate::requests::index_page::signature::round_two_decimals::round_two_decimals; /* @staticmethod def _h(x: float, _param: float, c: float, e: bool): @@ -32,7 +32,7 @@ pub fn signature_h(x: f64, param: f64, c: f64, e: bool) -> Result str: + rounded = round(float(num), 2) + if rounded == 0.0: + return "0" + sign = "-" if copysign(1.0, rounded) < 0 else "" + absval = abs(rounded) + intpart = int(floor(absval)) + frac = absval - intpart + if frac == 0.0: + return sign + format(intpart, "x") + frac_digits = [] + f = frac + for _ in range(20): + f *= 16 + digit = int(floor(f + 1e-12)) + frac_digits.append(format(digit, "x")) + f -= digit + if abs(f) < 1e-12: + break + frac_str = "".join(frac_digits).rstrip("0") + if frac_str == "": + return sign + format(intpart, "x") + return sign + format(intpart, "x") + "." + frac_str + */ + +pub fn signature_to_hex(num: f64) -> Result { + // rounded = round(float(num), 2) + let rounded = round_two_decimals(num); + + println!("rounded = {}", rounded); + + if rounded == 0.0 { + return Ok("0".to_string()); + } + + // sign = "-" if copysign(1.0, rounded) < 0 else "" + let test = 1.0f64.copysign(rounded); + let sign = if test < 0.0 { "-" } else { "" }; + + println!("sign = {}", sign); + + let absval = rounded.abs(); + let intpart = absval.floor() as u32; + + println!("absval = {}", absval); + println!("intpart = {}", intpart); + + let frac = absval - (intpart as f64); + + println!("frac = {}", frac); + + if frac == 0.0 { + // return sign + format(intpart, "x") -- this is hex formatting + let ret = format!("{sign}{intpart:x}"); + println!("return = {}", ret); + return Ok(ret); + } + + Ok("".to_string()) +} + +#[cfg(test)] +mod tests { + use crate::requests::index_page::signature::signature_to_hex::signature_to_hex; + + #[test] + fn test_1() { + let num = 67.0; + let output = signature_to_hex(num).unwrap(); + let expected = "43"; + + assert_eq!(&output, expected); + } +}