Skip to content

Commit b7c1037

Browse files
authored
Merge pull request #113 from lpbak/issue_107
Closes #107
2 parents 834c13d + c18f503 commit b7c1037

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/util/print_dec.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub unsafe fn write<W: io::Write>(wr: &mut W, positive: bool, mut n: u64, expone
7474
)
7575
);
7676
} else if exponent < 0 {
77-
let mut e = -exponent as u16;
77+
let mut e = safe_abs(exponent);
7878

7979
// Decimal number with a fraction that's fully printable
8080
if e < 18 {
@@ -215,3 +215,11 @@ pub unsafe fn write<W: io::Write>(wr: &mut W, positive: bool, mut n: u64, expone
215215
try!(wr.write_all(b"e"));
216216
write(wr, true, e, 0)
217217
}
218+
219+
fn safe_abs(x : i16) -> u16 {
220+
if let Some(y) = x.checked_abs() {
221+
y as u16
222+
} else {
223+
i16::max_value() as u16 + 1u16
224+
}
225+
}

tests/number.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,9 @@ fn as_fixed_point_i64() {
126126
fn convert_f64_precision() {
127127
assert_eq!(Number::from_parts(true, 4750000000000001, -18), 0.004750000000000001);
128128
}
129+
130+
#[test]
131+
fn issue_107() {
132+
let n = Number::from_parts(true, 1, -32768);
133+
assert_eq!(format!("{}", n), "1e-32768");
134+
}

0 commit comments

Comments
 (0)