GeezerVison is showing github.com — [fxp] Round half-even values below one unit to ±1, not 0. · feldera/feldera@c9ce238
File treeExpand file treeCollapse file tree Open diff view settings sql-to-dbsp-compiler/SQL-compiler/src/test/java/org/dbsp/sqlCompiler/compiler/sql/simple
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: crates/fxp/src/fixed.rs +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1799,6 +1799,34 @@ mod test { | 1799 | 1799 | assert_eq!(b.convert_round_even::<10, 10>(), None); | 1800 | 1800 | } | 1801 | 1801 |
| | 1802 | + /// Half-even rounding of a value with magnitude below one unit of the | | 1803 | + /// target scale (https://github.com/feldera/feldera/issues/7717). | | 1804 | + #[test] | | 1805 | + fn round_even_below_one() { | | 1806 | + let s = |x: Fixed<5, 0>| x.to_string(); | | 1807 | + assert_eq!(s(Fixed::from_str("0.6").unwrap()), "1"); | | 1808 | + assert_eq!(s(Fixed::from_str("-0.6").unwrap()), "-1"); | | 1809 | + assert_eq!(s(Fixed::from_str("0.5").unwrap()), "0"); | | 1810 | + assert_eq!(s(Fixed::from_str("-0.5").unwrap()), "0"); | | 1811 | + assert_eq!(s(Fixed::from_str("0.4").unwrap()), "0"); | | 1812 | + assert_eq!(s(Fixed::from_str("1.6").unwrap()), "2"); | | 1813 | + assert_eq!(Fixed::<5, 1>::from_str("0.06").unwrap().to_string(), "0.1"); | | 1814 | + | | 1815 | + let ms = Fixed::<10, 4>::from_str("0.0006").unwrap(); | | 1816 | + assert_eq!( | | 1817 | + ms.convert_round_even::<38, 3>().unwrap().to_string(), | | 1818 | + "0.001" | | 1819 | + ); | | 1820 | + let ms = Fixed::<10, 4>::from_str("-0.0006").unwrap(); | | 1821 | + assert_eq!( | | 1822 | + ms.convert_round_even::<38, 3>().unwrap().to_string(), | | 1823 | + "-0.001" | | 1824 | + ); | | 1825 | + | | 1826 | + assert_eq!(s(Fixed::new_round_even(6, 1).unwrap()), "1"); | | 1827 | + assert_eq!(s(Fixed::new_round_even(-6, 1).unwrap()), "-1"); | | 1828 | + } | | 1829 | + | 1802 | 1830 | #[test] | 1803 | 1831 | fn constants() { | 1804 | 1832 | assert_eq!(Fixed::<5, 0>::MAX, Fixed::<5, 0>(99999)); | |
Collapse file Copy file name to clipboardExpand all lines: crates/fxp/src/lib.rs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -387,7 +387,7 @@ fn i128_mul_pow10_round_even(value: i128, exponent: i32) -> Option<i128> { | 387 | 387 | Ordering::Greater => true, | 388 | 388 | }; | 389 | 389 | if round_away_from_zero { | 390 | | - quotient + quotient.signum() | | 390 | + quotient + value.signum() | 391 | 391 | } else { | 392 | 392 | quotient | 393 | 393 | } | |
Collapse file Copy file name to clipboardExpand all lines: sql-to-dbsp-compiler/SQL-compiler/src/test/java/org/dbsp/sqlCompiler/compiler/sql/simple/CastTests.java +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -952,6 +952,22 @@ public void issue7395() { | 952 | 952 | 0.12345 | 0.12345600000000000000 | 0.10000000149011612000 | 1"""); | 953 | 953 | } | 954 | 954 |
| | 955 | + @Test | | 956 | + public void issue7717() { | | 957 | + // Half-even rounding of a value below one unit of the target scale. | | 958 | + // DECIMAL to TIMESTAMP reads the value as milliseconds, so 0.0006 is 0.6 microseconds. | | 959 | + var ccs = this.getCCS(""" | | 960 | + CREATE TABLE R(s VARCHAR, d DECIMAL(10, 4)); | | 961 | + CREATE VIEW V AS SELECT CAST(s AS DECIMAL(5, 0)) AS a, CAST(d AS TIMESTAMP) AS b FROM R;"""); | | 962 | + ccs.step("INSERT INTO R VALUES ('0.6', 0.0006), ('-0.6', -0.0006), ('0.5', 0.0005), ('1.5', 0.0015)", """ | | 963 | + a | b | weight | | 964 | + ---------------- | | 965 | + 1 | 1970-01-01 00:00:00.000001 | 1 | | 966 | + -1 | 1969-12-31 23:59:59.999999 | 1 | | 967 | + 0 | 1970-01-01 00:00:00 | 1 | | 968 | + 2 | 1970-01-01 00:00:00.000002 | 1"""); | | 969 | + } | | 970 | + | 955 | 971 | @Test | 956 | 972 | public void issue6257() { | 957 | 973 | // Calcite rounds by truncating, so we are tied to that behavior | |
|