num_traits::ops::mul_add

Trait MulAdd

Source
pub trait MulAdd<A = Self, B = Self> {
    type Output;

    // Required method
    fn mul_add(self, a: A, b: B) -> Self::Output;
}
Expand description

Fused multiply-add. Computes (self * a) + b with only one rounding error, yielding a more accurate result than an unfused multiply-add.

Using mul_add can be more performant than an unfused multiply-add if the target architecture has a dedicated fma CPU instruction.

Note that A and B are Self by default, but this is not mandatory.

§Example

use std::f32;

let m = 10.0_f32;
let x = 4.0_f32;
let b = 60.0_f32;

// 100.0
let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();

assert!(abs_difference <= 100.0 * f32::EPSILON);

Required Associated Types§

Source

type Output

The resulting type after applying the fused multiply-add.

Required Methods§

Source

fn mul_add(self, a: A, b: B) -> Self::Output

Performs the fused multiply-add operation (self * a) + b

Implementations on Foreign Types§

Source§

impl MulAdd for f32

Source§

type Output = f32

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for f64

Source§

type Output = f64

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for i8

Source§

type Output = i8

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for i16

Source§

type Output = i16

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for i32

Source§

type Output = i32

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for i64

Source§

type Output = i64

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for i128

Source§

type Output = i128

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for isize

Source§

type Output = isize

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for u8

Source§

type Output = u8

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for u16

Source§

type Output = u16

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for u32

Source§

type Output = u32

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for u64

Source§

type Output = u64

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for u128

Source§

type Output = u128

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Source§

impl MulAdd for usize

Source§

type Output = usize

Source§

fn mul_add(self, a: Self, b: Self) -> Self::Output

Implementors§