Skip to content

Commit

Permalink
this is a bust
Browse files Browse the repository at this point in the history
  • Loading branch information
EclecticGriffin committed Dec 4, 2024
1 parent 154da35 commit 44ca740
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
27 changes: 19 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/cider-data-converter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ thiserror = "1.0.59"
num-bigint = { version = "0.4.6" }
num-rational = { version = "0.4.2" }
num-traits = { version = "0.2.19" }
fraction = "0.15"
nom = "7.1.3"

[dev-dependencies]
Expand Down
21 changes: 19 additions & 2 deletions tools/cider-data-converter/src/converter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::json_data::*;
use fraction::{BigDecimal, BigFraction};
use interp::serialization::*;
use itertools::Itertools;
use num_bigint::{BigInt, BigUint, ToBigInt};
Expand Down Expand Up @@ -266,6 +267,21 @@ fn parse_bytes_fixed(
BigRational::new(int.clone(), BigInt::from(1) << frac_width)
}

fn big_rational_to_big_decimal(rat: BigRational) -> BigDecimal {
let is_neg = rat.is_negative();
let (numerator, denominator) = rat.into_raw();
let (_, numerator) = numerator.into_parts();
let (_, denominator) = denominator.into_parts();
let frac = if is_neg {
BigFraction::new_neg(numerator, denominator)
} else {
BigFraction::new(numerator, denominator)
};

let dec = BigDecimal::from_fraction(frac);
dec.set_precision(64)
}

fn format_data(declaration: &MemoryDeclaration, data: &[u8]) -> ParseVec {
let width = declaration.width();

Expand All @@ -285,9 +301,10 @@ fn format_data(declaration: &MemoryDeclaration, data: &[u8]) -> ParseVec {
} => {
let int =
parse_bytes_fixed(chunk, int_width, frac_width, signed);
let float = int.to_f64().unwrap();
let decimal = big_rational_to_big_decimal(int);


Number::from_f64(float).unwrap()
Number::from_str(&decimal.to_string()).unwrap()
}
interp::serialization::FormatInfo::IEEFloat {
width,
Expand Down

0 comments on commit 44ca740

Please sign in to comment.