add from_f32, from_f64, to_f32 and to_f64 examples

This commit is contained in:
Trevor Spiteri 2018-08-14 11:45:23 +02:00
parent 364b4ef23c
commit d1830b6282
2 changed files with 93 additions and 13 deletions

View File

@ -237,7 +237,7 @@ mod tests {
// -111.1111 -> -111.1111
assert_eq!(Fix::from_f32(-127.0 / 16.0).unwrap(), Fix::from_bits(-127));
// -111.11111 -> -1000.0000
// -111.11111 -> -1000.0000 (tie to even)
assert_eq!(Fix::from_f32(-255.0 / 32.0).unwrap(), Fix::from_bits(-128));
// -1000.00001 -> -1000.0000 (tie to even)
assert_eq!(Fix::from_f32(-257.0 / 32.0).unwrap(), Fix::from_bits(-128));
@ -266,7 +266,7 @@ mod tests {
assert_eq!(Fix::from_f32(1.0 / 32.0).unwrap(), Fix::from_bits(0));
// -0.00001 -> 0000.0000 (tie to even)
assert_eq!(Fix::from_f32(-1.0 / 32.0).unwrap(), Fix::from_bits(0));
// -0.0001 -> -ve
// -0.0001 -> too small
assert!(Fix::from_f32(-1.0 / 16.0).is_none());
// 1111.1111 -> 1111.1111

View File

@ -201,10 +201,58 @@ macro_rules! doc_comment_signed_unsigned {
}
macro_rules! from_float {
(fn $method:ident($Float:ident) -> $Fixed:ident < $Frac:ident >) => {
doc_comment! {
($Signedness:tt, fn $method:ident($Float:ident) -> $Fixed:ident < $Frac:ident >) => {
doc_comment_signed_unsigned! {
$Signedness,
concat!(
"Creates a fixed-point number from `", stringify!($Float), "`."
"Creates a fixed-point number from `", stringify!($Float), "`.\n",
"\n",
"This method rounds to the nearest, with ties rounding to even.",
"\n",
"# Examples\n",
"\n",
"```rust\n",
"use fixed::frac;\n",
"use fixed::", stringify!($Fixed), ";\n",
"type Fix = ", stringify!($Fixed), "<frac::U4>;\n",
"// 1.75 is 0001.1100, that is from_bits(28)\n",
"assert_eq!(Fix::from_", stringify!($Float),
"(1.75), Some(Fix::from_bits(28)));\n",
"assert_eq!(Fix::from_", stringify!($Float),
"(-1.75), Some(Fix::from_bits(-28)));\n",
"// 1e-10 is too small for four fractional bits\n",
"assert_eq!(Fix::from_", stringify!($Float),
"(1e-10), Some(Fix::from_bits(0)));\n",
"assert_eq!(Fix::from_", stringify!($Float),
"(-1e-10), Some(Fix::from_bits(0)));\n",
"// 2e38 is too large for ", stringify!($Fixed), "<frac::U4>\n",
"assert!(Fix::from_", stringify!($Float),
"(2e38).is_none());\n",
"assert!(Fix::from_", stringify!($Float),
"(-2e38).is_none());\n",
"```\n"
),
concat!(
"Creates a fixed-point number from `", stringify!($Float), "`.\n",
"\n",
"This method rounds to the nearest, with ties rounding to even.",
"\n",
"# Examples\n",
"\n",
"```rust\n",
"use fixed::frac;\n",
"use fixed::", stringify!($Fixed), ";\n",
"type Fix = ", stringify!($Fixed), "<frac::U4>;\n",
"// 1.75 is 0001.1100, that is from_bits(28)\n",
"assert_eq!(Fix::from_", stringify!($Float),
"(1.75), Some(Fix::from_bits(28)));\n",
"// 1e-10 is too small for four fractional bits\n",
"assert_eq!(Fix::from_", stringify!($Float),
"(1e-10), Some(Fix::from_bits(0)));\n",
"// 2e38 is too large for ", stringify!($Fixed), "<frac::U4>\n",
"assert!(Fix::from_", stringify!($Float),
"(2e38).is_none());\n",
"```\n"
),
#[inline]
pub fn $method(val: $Float) -> Option<$Fixed<$Frac>> {
@ -244,10 +292,42 @@ macro_rules! from_float {
}
macro_rules! to_float {
(fn $method:ident($Fixed:ident < $Frac:ident >) -> $Float:ident) => {
doc_comment! {
($Signedness:tt, fn $method:ident($Fixed:ident < $Frac:ident >) -> $Float:ident) => {
doc_comment_signed_unsigned! {
$Signedness,
concat!(
"Converts the fixed-point number to `", stringify!($Float), "`."
"Converts the fixed-point number to `", stringify!($Float), "`.\n",
"\n",
"This method rounds to the nearest, with ties rounding to even.",
"\n",
"# Examples\n",
"\n",
"```rust\n",
"use fixed::frac;\n",
"use fixed::", stringify!($Fixed), ";\n",
"type Fix = ", stringify!($Fixed), "<frac::U4>;\n",
"// 1.75 is 0001.1100, that is from_bits(28)\n",
"assert_eq!(Fix::from_bits(28).to_", stringify!($Float),
"(), 1.75);\n",
"assert_eq!(Fix::from_bits(-28).to_", stringify!($Float),
"(), -1.75);\n",
"```\n"
),
concat!(
"Converts the fixed-point number to `", stringify!($Float), "`.\n",
"\n",
"This method rounds to the nearest, with ties rounding to even.",
"\n",
"# Examples\n",
"\n",
"```rust\n",
"use fixed::frac;\n",
"use fixed::", stringify!($Fixed), ";\n",
"type Fix = ", stringify!($Fixed), "<frac::U4>;\n",
"// 1.75 is 0001.1100, that is from_bits(28)\n",
"assert_eq!(Fix::from_bits(28).to_", stringify!($Float),
"(), 1.75);\n",
"```\n"
),
#[inline]
pub fn $method(self) -> $Float {
@ -623,6 +703,11 @@ macro_rules! fixed {
}
}
from_float! { $Signedness, fn from_f32(f32) -> $Fixed<Frac> }
from_float! { $Signedness, fn from_f64(f64) -> $Fixed<Frac> }
to_float! { $Signedness, fn to_f32($Fixed<Frac>) -> f32 }
to_float! { $Signedness, fn to_f64($Fixed<Frac>) -> f64 }
doc_comment_signed_unsigned! {
$Signedness,
concat!(
@ -731,11 +816,6 @@ macro_rules! fixed {
}
}
from_float! { fn from_f32(f32) -> $Fixed<Frac> }
from_float! { fn from_f64(f64) -> $Fixed<Frac> }
to_float! { fn to_f32($Fixed<Frac>) -> f32 }
to_float! { fn to_f64($Fixed<Frac>) -> f64 }
pass_method! {
"Returns the number of ones in the binary representation.",
$Fixed($Inner) => fn count_ones(self) -> u32