Skip to content

Instantly share code, notes, and snippets.

@killerswan
Created June 2, 2012 05:39
Show Gist options
  • Save killerswan/2856808 to your computer and use it in GitHub Desktop.
Save killerswan/2856808 to your computer and use it in GitHub Desktop.
floats as strings in Rust...
#[test]
fn rounding() {
// trailing zeroes should be inserted, or rather
// this dodgy floating point stuff should be rounded
assert "3.141589999" == #fmt["%9.9f", 3.14159];
assert "3.141589999" == float::to_str_common(3.14159, 9u, false);
assert "3.141590000" == my_to_str_common(3.14159, 9u, false); // FIXED
assert "3.14158" == float::to_str_common(3.14159, 5u, false);
assert "3.14159" == my_to_str_common(3.14159, 5u, false); // FIXED
// this truncation should be rounded
assert "3.1415" == float::to_str(3.14159, 4u);
assert "3.1416" == my_to_str_exact(3.14159, 4u); // FIXED
assert "3" == my_to_str_exact(3.14159, 0u); // FIXED
assert "17" == my_to_str_exact(16.9, 0u); // FIXED
assert "7.0000" == my_to_str_exact(6.99999999, 4u); // FIXED
// FIXME: epsilon calc should be adjusted and we should round these accordingly
assert "3.14158999999999988261" == float::to_str_common(3.14159, 20u, false);
assert "3.14158999999999988262" == my_to_str_exact(3.14159, 20u); // STILL WEIRD
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment