Expand description

Midpoint two-place function

Two-place midpoint function is the function returning an average of two values, such as values of signed or unsigned integer types, floating point types, or pointer types.

This library provides several implementations of two-place midpoint function [currently, only for primitive integers] with different properties (performance, generality, and rounding behavior) whereas the GitHub repo of the lib offers the design document, tests, runnable benchmarks, and pre-generated criterion.rs performance reports.

Example

Cargo.toml

[dependencies]
midpoint = { version = "0.1.2" }

[features]
all = ["const_trait_impl", "unchecked_math", "const_inherent_unchecked_arith"]
const_trait_impl = ["midpoint/const_trait_impl"]
unchecked_math = ["midpoint/unchecked_math"]
const_inherent_unchecked_arith = ["midpoint/const_inherent_unchecked_arith"]

src/main.rs

use midpoint::MidpointViaPrimitivePromotionExt;

// With features = ["all"] or 
// features = ["const_trait_impl", ...] the call can be
// performed in constant context, such as const fn
let result: i32 = (-7).midpoint_via_primitive_promotion(&-2);
assert_eq!(result, -4);

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Macros

Implements a trait with the supplied name for all primitive integers using function body returned by the macro

Implements a trait with the supplied name for all primitive integers with primitive promotion using function body returned by the macro

Macro generating a sum of arguments assuming overflow is impossible.

Traits

Extension trait providing implementation of midpoint algorithm via bitwise operations. For primitive integers, the result is rounded towards zero.

Extension trait providing implementation of midpoint algorithm as suggested for C++20 standard library. For primitive integers, the result is rounded towards left argument.

Extension trait providing implementation of midpoint algorithm via naive midpoint difference. For primitive integers, the result is rounded towards left argument.

Extension trait providing implementation of midpoint algorithm via primitive promotion. For primitive integers, the result is rounded towards zero.

Extension trait providing implementation of naive midpoint algorithm. For primitive integers, the result is rounded towards zero.