Macro fixed::const_fixed_from_int[][src]

macro_rules! const_fixed_from_int {
    ($(const $NAME : ident : $Fixed : ty = $int : expr ;) *) => { ... };
}
Expand description

Defines constant fixed-point numbers from integer expressions.

This macro is useful because from_num cannot be used in constant expressions.

Alternative

The fixed-macro crate provides a convenient macro to write down fixed-point constants literally in code which has two advantages over this macro:

  1. It can handle fixed-point numbers with fractions, not just integers.
  2. It can be used anywhere an expression or constant expression can be used, not just to define a constant.

Examples

use fixed::{const_fixed_from_int, types::I16F16};
const_fixed_from_int! {
    // define a constant using an integer
    const FIVE: I16F16 = 5;
    // define a constant using an integer expression
    const SUM: I16F16 = 3 + 2;
}
assert_eq!(FIVE, 5);
assert_eq!(SUM, 5);

The following would fail to compile because i32::MAX is not representable by I16F16.

use fixed::{const_fixed_from_int, types::I16F16};
const_fixed_from_int! {
    // fails because i32::MAX > I16F16::MAX
    const _OVERFLOW: I16F16 = i32::MAX;
}

The following would fail to compile because I16F16 is an alias for FixedI32<U32>, and this macro can define FixedI32 constants using i32 expressions, not i16 expressions.

use fixed::{const_fixed_from_int, types::I16F16};
const_fixed_from_int! {
    // fails because 0i16 is not of type i32
    const _MISMATCH: I16F16 = 0i16;
}