Skip to content

midpoint function #357

@tyilo

Description

@tyilo

I think it would be very useful to have the midpoint function in num_traits for implementing binary search.

I'm currently using:

pub trait Midpoint: Num {
    fn midpoint(&self, rhs: &Self) -> Self;
}

impl<T> Midpoint for T
where
    T: Num + Shr<u8, Output = T>,
    for<'a> &'a T: BitXor<&'a T, Output = T> + BitAnd<&'a T, Output = T>,
{
    fn midpoint(&self, rhs: &Self) -> Self {
        ((self ^ rhs) >> 1u8) + (self & rhs)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions