Skip to content

Commit acf4d2a

Browse files
committed
0.7.2 print out floats down to 1e-15 #25
1 parent c4ec529 commit acf4d2a

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/codegen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub trait Generator {
5454
self.write_digits_from_u64(num as u64, &mut length);
5555

5656
let mut fract = num.fract();
57-
if fract < 1e-10 {
57+
if fract < 1e-16 {
5858
return;
5959
}
6060

@@ -64,7 +64,7 @@ pub trait Generator {
6464
fract = fract.fract();
6565
length += 2;
6666

67-
while length < 17 && fract > 0.01 {
67+
while length < 17 && fract > 1e-15 {
6868
fract *= 10.0;
6969
self.write_char((fract as u8) + b'0');
7070
fract = fract.fract();

src/value.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ impl JsonValue {
107107
/// - empty object (`object!{}`)
108108
pub fn is_empty(&self) -> bool {
109109
match *self {
110-
JsonValue::String(ref string) => string.is_empty(),
111-
JsonValue::Number(ref float) => !float.is_normal(),
112-
JsonValue::Boolean(ref boolean) => !boolean,
113-
JsonValue::Null => true,
114-
JsonValue::Array(ref vec) => vec.is_empty(),
115-
JsonValue::Object(ref btree) => btree.is_empty(),
110+
JsonValue::String(ref value) => value.is_empty(),
111+
JsonValue::Number(ref value) => !value.is_normal(),
112+
JsonValue::Boolean(ref value) => !value,
113+
JsonValue::Null => true,
114+
JsonValue::Array(ref value) => value.is_empty(),
115+
JsonValue::Object(ref value) => value.is_empty(),
116116
}
117117
}
118118

@@ -462,7 +462,7 @@ impl IndexMut<usize> for JsonValue {
462462
_ => {
463463
*self = JsonValue::new_array();
464464
self.push(JsonValue::Null).unwrap();
465-
&mut self[0]
465+
self.index_mut(index)
466466
}
467467
}
468468
}
@@ -521,7 +521,7 @@ impl<'a> IndexMut<&'a str> for JsonValue {
521521
},
522522
_ => {
523523
*self = JsonValue::new_object();
524-
&mut self[index]
524+
self.index_mut(index)
525525
}
526526
}
527527
}

tests/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ fn stringify_integer() {
135135
assert_eq!(stringify(42), "42");
136136
}
137137

138+
#[test]
139+
fn stringify_small_number() {
140+
assert_eq!(stringify(0.000000000000001), "0.000000000000001");
141+
}
142+
138143
#[test]
139144
fn stringify_true() {
140145
assert_eq!(stringify(true), "true");

0 commit comments

Comments
 (0)