1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright © 2016–2024 Trevor Spiteri

// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License and
// a copy of the GNU General Public License along with this program. If not, see
// <https://www.gnu.org/licenses/>.

use crate::{Float, Integer};
use az::{CheckedAs, CheckedCast};
use num_traits::cast::ToPrimitive;
use num_traits::ops::inv::Inv;
use num_traits::ops::mul_add::{MulAdd, MulAddAssign};

impl Inv for Float {
    type Output = Self;

    #[inline]
    fn inv(self) -> Self::Output {
        self.recip()
    }
}

impl MulAdd for Float {
    type Output = Float;

    #[inline]
    fn mul_add(self, a: Float, b: Float) -> Float {
        self.mul_add(&a, &b)
    }
}

impl MulAddAssign for Float {
    #[inline]
    fn mul_add_assign(&mut self, a: Float, b: Float) {
        self.mul_add_mut(&a, &b);
    }
}

impl MulAdd<&Float, &Float> for Float {
    type Output = Float;

    #[inline]
    fn mul_add(self, a: &Float, b: &Float) -> Float {
        self.mul_add(a, b)
    }
}

impl MulAddAssign<&Float, &Float> for Float {
    #[inline]
    fn mul_add_assign(&mut self, a: &Float, b: &Float) {
        self.mul_add_mut(a, b);
    }
}

impl ToPrimitive for Float {
    #[inline]
    fn to_i64(&self) -> Option<i64> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_u64(&self) -> Option<u64> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_isize(&self) -> Option<isize> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_i8(&self) -> Option<i8> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_i16(&self) -> Option<i16> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_i32(&self) -> Option<i32> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_i128(&self) -> Option<i128> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_usize(&self) -> Option<usize> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_u8(&self) -> Option<u8> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_u16(&self) -> Option<u16> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_u32(&self) -> Option<u32> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_u128(&self) -> Option<u128> {
        self.checked_as::<Integer>()
            .and_then(CheckedCast::checked_cast)
    }
    #[inline]
    fn to_f32(&self) -> Option<f32> {
        Some(self.to_f32())
    }
    #[inline]
    fn to_f64(&self) -> Option<f64> {
        Some(self.to_f64())
    }
}