rust/fpnum/src/lib.rs
author alfadur
Thu, 18 Oct 2018 06:50:21 +0300
changeset 13944 665b4c6612ee
parent 13937 7bb60596c27e
child 14086 5d42204ac35e
permissions -rw-r--r--
fix fppoint.max_norm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     1
use std::cmp;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     2
use std::ops;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     3
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     4
#[derive(Clone, Debug, Copy)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     5
pub struct FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     6
    is_negative: bool,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     7
    value: u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     8
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     9
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    10
impl FPNum {
13932
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
    11
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    12
    pub fn new(numerator: i32, denominator: u32) -> Self {
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    13
        FPNum::from(numerator) / denominator
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    14
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    15
13890
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13889
diff changeset
    16
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    17
    pub fn signum(&self) -> i8 {
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    18
        if self.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    19
            -1
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    20
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    21
            1
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    22
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    23
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    24
13890
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13889
diff changeset
    25
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    26
    pub fn is_negative(&self) -> bool {
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    27
        self.is_negative
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    28
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    29
13890
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13889
diff changeset
    30
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    31
    pub fn is_positive(&self) -> bool {
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    32
        !self.is_negative
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    33
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    34
13890
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13889
diff changeset
    35
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    36
    pub fn is_zero(&self) -> bool {
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    37
        self.value == 0
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    38
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    39
13890
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13889
diff changeset
    40
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    41
    pub fn abs(&self) -> Self {
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    42
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    43
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    44
            value: self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    45
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    46
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    47
13890
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13889
diff changeset
    48
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    49
    pub fn round(&self) -> i64 {
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    50
        if self.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    51
            -((self.value >> 32) as i64)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    52
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    53
            (self.value >> 32) as i64
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    54
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    55
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    56
13890
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13889
diff changeset
    57
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    58
    pub fn sqr(&self) -> Self {
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    59
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    60
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    61
            value: ((self.value as u128).pow(2) >> 32) as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    62
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    63
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    64
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    65
    pub fn sqrt(&self) -> Self {
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    66
        debug_assert!(!self.is_negative);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    67
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    68
        let mut t: u64 = 0x4000000000000000;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    69
        let mut r: u64 = 0;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    70
        let mut q = self.value;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    71
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    72
        for _ in 0..32 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    73
            let s = r + t;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    74
            r >>= 1;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    75
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    76
            if s <= q {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    77
                q -= s;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    78
                r += t;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    79
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    80
            t >>= 2;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    81
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    82
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    83
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    84
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    85
            value: r << 16,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    86
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    87
    }
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
    88
13932
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
    89
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    90
    pub fn with_sign(&self, is_negative: bool) -> FPNum {
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
    91
        FPNum{ is_negative, ..*self}
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
    92
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
    93
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
    94
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
    95
    pub fn with_sign_as(self, other: FPNum) -> FPNum {
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
    96
        self.with_sign(other.is_negative)
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
    97
    }
13932
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
    98
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
    99
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   100
    pub fn point(self) -> FPPoint {
13932
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   101
        FPPoint::new(self, self)
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   102
    }
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   103
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   104
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   105
impl From<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   106
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   107
    fn from(n: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   108
        FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   109
            is_negative: n < 0,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   110
            value: (n.abs() as u64) << 32,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   111
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   112
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   113
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   114
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   115
impl From<u32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   116
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   117
    fn from(n: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   118
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   119
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   120
            value: (n as u64) << 32,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   121
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   122
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   123
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   124
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   125
impl From<FPNum> for f64 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   126
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   127
    fn from(n: FPNum) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   128
        if n.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   129
            n.value as f64 / (-0x10000000 as f64)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   130
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   131
            n.value as f64 / 0x10000000 as f64
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   132
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   133
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   134
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   135
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   136
impl PartialEq for FPNum {
13890
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13889
diff changeset
   137
    #[inline]
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   138
    fn eq(&self, other: &Self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   139
        self.value == other.value && (self.is_negative == other.is_negative || self.value == 0)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   140
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   141
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   142
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   143
impl Eq for FPNum {}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   144
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   145
impl PartialOrd for FPNum {
13890
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13889
diff changeset
   146
    #[inline]
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   147
    fn partial_cmp(&self, rhs: &Self) -> std::option::Option<std::cmp::Ordering> {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   148
        Some(self.cmp(rhs))
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   149
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   150
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   151
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   152
impl Ord for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   153
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   154
    fn cmp(&self, rhs: &Self) -> cmp::Ordering {
13889
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   155
        #[inline]
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   156
        fn extend(n: &FPNum) -> i128 {
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   157
            if n.is_negative {
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   158
                -(n.value as i128)
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   159
            } else {
13889
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   160
                n.value as i128
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   161
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   162
        }
13889
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   163
        extend(self).cmp(&(extend(rhs)))
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   164
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   165
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   166
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   167
impl ops::Add for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   168
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   169
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   170
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   171
    fn add(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   172
        if self.is_negative == rhs.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   173
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   174
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   175
                value: self.value + rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   176
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   177
        } else if self.value > rhs.value {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   178
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   179
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   180
                value: self.value - rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   181
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   182
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   183
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   184
                is_negative: rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   185
                value: rhs.value - self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   186
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   187
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   188
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   189
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   190
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   191
impl ops::Sub for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   192
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   193
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   194
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   195
    fn sub(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   196
        if self.is_negative == rhs.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   197
            if self.value > rhs.value {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   198
                Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   199
                    is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   200
                    value: self.value - rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   201
                }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   202
            } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   203
                Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   204
                    is_negative: !rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   205
                    value: rhs.value - self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   206
                }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   207
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   208
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   209
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   210
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   211
                value: self.value + rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   212
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   213
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   214
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   215
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   216
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   217
impl ops::Neg for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   218
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   219
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   220
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   221
    fn neg(self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   222
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   223
            is_negative: !self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   224
            value: self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   225
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   226
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   227
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   228
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   229
impl ops::Mul for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   230
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   231
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   232
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   233
    fn mul(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   234
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   235
            is_negative: self.is_negative ^ rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   236
            value: ((self.value as u128 * rhs.value as u128) >> 32) as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   237
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   238
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   239
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   240
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   241
impl ops::Mul<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   242
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   243
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   244
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   245
    fn mul(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   246
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   247
            is_negative: self.is_negative ^ (rhs < 0),
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   248
            value: self.value * rhs.abs() as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   249
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   250
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   251
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   252
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   253
impl ops::Div for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   254
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   255
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   256
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   257
    fn div(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   258
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   259
            is_negative: self.is_negative ^ rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   260
            value: (((self.value as u128) << 32) / rhs.value as u128) as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   261
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   262
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   263
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   264
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   265
impl ops::Div<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   266
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   267
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   268
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   269
    fn div(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   270
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   271
            is_negative: self.is_negative ^ (rhs < 0),
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   272
            value: self.value / rhs.abs() as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   273
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   274
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   275
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   276
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   277
impl ops::Div<u32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   278
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   279
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   280
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   281
    fn div(self, rhs: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   282
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   283
            is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   284
            value: self.value / rhs as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   285
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   286
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   287
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   288
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   289
#[macro_export]
13896
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   290
macro_rules! fp {
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   291
    (-$n: tt / $d: tt) => { FPNum::new(-$n, $d) };
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   292
    ($n: tt / $d: tt) => { FPNum::new($n, $d) };
13910
fed924791e06 add extra rule for negative integers
alfadur
parents: 13901
diff changeset
   293
    (-$n: tt) => { FPNum::from(-$n) };
13896
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   294
    ($n: tt) => { FPNum::from($n) };
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   295
}
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   296
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   297
const LINEARIZE_TRESHOLD: u64 = 0x1_0000;
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   298
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   299
#[derive(Clone, Copy, Debug)]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   300
pub struct FPPoint {
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   301
    x_is_negative: bool,
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   302
    y_is_negative: bool,
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   303
    x_value: u64,
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   304
    y_value: u64,
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   305
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   306
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   307
impl FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   308
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   309
    pub fn new(x: FPNum, y: FPNum) -> Self {
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   310
        Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   311
            x_is_negative: x.is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   312
            y_is_negative: y.is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   313
            x_value: x.value,
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   314
            y_value: y.value
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   315
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   316
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   317
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   318
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   319
    pub fn zero() -> FPPoint { FPPoint::new(fp!(0), fp!(0)) }
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   320
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   321
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   322
    pub fn unit_x() -> FPPoint { FPPoint::new(fp!(1), fp!(0)) }
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   323
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   324
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   325
    pub fn unit_y() -> FPPoint { FPPoint::new(fp!(0), fp!(1)) }
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   326
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   327
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   328
    pub fn x(&self) -> FPNum {
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   329
        FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   330
            is_negative: self.x_is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   331
            value: self.x_value
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   332
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   333
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   334
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   335
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   336
    pub fn y(&self) -> FPNum {
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   337
        FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   338
            is_negative: self.y_is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   339
            value: self.y_value
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   340
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   341
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   342
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   343
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   344
    pub fn max_norm(&self) -> FPNum {
13944
665b4c6612ee fix fppoint.max_norm
alfadur
parents: 13937
diff changeset
   345
        std::cmp::max(self.x().abs(), self.y().abs())
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   346
    }
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   347
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   348
    #[inline]
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   349
    pub fn sqr_distance(&self) -> FPNum {
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   350
        self.x().sqr() + self.y().sqr()
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   351
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   352
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   353
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   354
    pub fn distance(&self) -> FPNum {
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   355
        let r = self.x_value | self.y_value;
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   356
        if r < LINEARIZE_TRESHOLD {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   357
            FPNum::from(r as u32)
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   358
        } else {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   359
            self.sqr_distance().sqrt()
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   360
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   361
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   362
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   363
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   364
    pub fn is_in_range(&self, radius: FPNum) -> bool {
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   365
        self.max_norm() < radius && self.sqr_distance() < radius.sqr()
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   366
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   367
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   368
    #[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   369
    pub fn dot(&self, other: &FPPoint) -> FPNum {
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   370
        self.x() * other.x() + self.y() * other.y()
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   371
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   372
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   373
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   374
impl PartialEq for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   375
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   376
    fn eq(&self, other: &Self) -> bool {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   377
        self.x() == other.x() && self.y() == other.y()
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   378
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   379
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   380
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   381
impl Eq for FPPoint {}
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   382
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   383
impl ops::Neg for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   384
    type Output = Self;
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   385
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   386
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   387
    fn neg(self) -> Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   388
        Self::new(-self.x(), -self.y())
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   389
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   390
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   391
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   392
macro_rules! bin_op_impl {
13932
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   393
    ($op: ty, $name: tt) => {
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   394
        impl $op for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   395
            type Output = Self;
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   396
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   397
            #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   398
            fn $name(self, rhs: Self) -> Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   399
                Self::new(self.x().$name(rhs.x()),
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   400
                          self.y().$name(rhs.y()))
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   401
            }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   402
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   403
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   404
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   405
13937
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   406
macro_rules! right_scalar_bin_op_impl {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   407
    ($($op: tt)::+, $name: tt) => {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   408
        impl $($op)::+<FPNum> for FPPoint {
13932
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   409
            type Output = Self;
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   410
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   411
            #[inline]
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   412
            fn $name(self, rhs: FPNum) -> Self {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   413
                Self::new(self.x().$name(rhs),
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   414
                          self.y().$name(rhs))
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   415
            }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   416
        }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   417
    }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   418
}
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   419
13937
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   420
macro_rules! left_scalar_bin_op_impl {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   421
    ($($op: tt)::+, $name: tt) => {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   422
        impl $($op)::+<FPPoint> for FPNum {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   423
            type Output = FPPoint;
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   424
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   425
            #[inline]
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   426
            fn $name(self, rhs: FPPoint) -> Self::Output {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   427
                Self::Output::new(self.$name(rhs.x()),
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   428
                                  self.$name(rhs.y()))
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   429
            }
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   430
        }
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   431
    }
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   432
}
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   433
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   434
bin_op_impl!(ops::Add, add);
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   435
bin_op_impl!(ops::Sub, sub);
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   436
bin_op_impl!(ops::Mul, mul);
13934
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   437
bin_op_impl!(ops::Div, div);
13937
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   438
right_scalar_bin_op_impl!(ops::Add, add);
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   439
right_scalar_bin_op_impl!(ops::Mul, mul);
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   440
right_scalar_bin_op_impl!(ops::Sub, sub);
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   441
right_scalar_bin_op_impl!(ops::Div, div);
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   442
left_scalar_bin_op_impl!(ops::Mul, mul);
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   443
13934
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   444
macro_rules! bin_assign_op_impl {
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   445
    ($typ: tt, $($op: tt)::+, $name: tt, $delegate: tt) => {
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   446
        bin_assign_op_impl!($typ, $($op)::+<$typ>, $name, $delegate);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   447
    };
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   448
    ($typ: tt, $($op: tt)::+<$arg: tt>, $name: tt, $delegate: tt) => {
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   449
        impl $($op)::+<$arg> for $typ {
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   450
            #[inline]
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   451
            fn $name(&mut self, rhs: $arg) {
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   452
                *self = *self $delegate rhs;
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   453
            }
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   454
        }
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   455
    }
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   456
}
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   457
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   458
bin_assign_op_impl!(FPNum, ops::AddAssign, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   459
bin_assign_op_impl!(FPNum, ops::SubAssign, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   460
bin_assign_op_impl!(FPNum, ops::MulAssign, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   461
bin_assign_op_impl!(FPNum, ops::DivAssign, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   462
bin_assign_op_impl!(FPNum, ops::MulAssign<i32>, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   463
bin_assign_op_impl!(FPNum, ops::DivAssign<i32>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   464
bin_assign_op_impl!(FPNum, ops::DivAssign<u32>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   465
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   466
bin_assign_op_impl!(FPPoint, ops::AddAssign, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   467
bin_assign_op_impl!(FPPoint, ops::SubAssign, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   468
bin_assign_op_impl!(FPPoint, ops::MulAssign, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   469
bin_assign_op_impl!(FPPoint, ops::DivAssign, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   470
bin_assign_op_impl!(FPPoint, ops::AddAssign<FPNum>, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   471
bin_assign_op_impl!(FPPoint, ops::SubAssign<FPNum>, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   472
bin_assign_op_impl!(FPPoint, ops::MulAssign<FPNum>, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   473
bin_assign_op_impl!(FPPoint, ops::DivAssign<FPNum>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   474
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   475
#[inline]
13933
5fdc41cd0841 make methods public
alfadur
parents: 13932
diff changeset
   476
pub fn distance<T>(x: T, y: T) -> FPNum
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   477
    where T: ops::Add + ops::Mul + Copy +
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   478
             From<<T as ops::Add>::Output> +
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   479
             From<<T as ops::Mul>::Output> +
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   480
             Into<FPNum>
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   481
{
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   482
    let sqr: FPNum = T::from(T::from(x * x) + T::from(y * y)).into();
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   483
    sqr.sqrt()
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   484
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   485
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   486
/* TODO:
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   487
 AngleSin
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   488
 AngleCos
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   489
*/
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   490
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   491
#[cfg(test)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   492
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   493
fn basics() {
13896
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   494
    let n = fp!(15/2);
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   495
    assert!(n.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   496
    assert!(!n.is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   497
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   498
    assert!(!(-n).is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   499
    assert!((-n).is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   500
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   501
    assert_eq!(-(-n), n);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   502
    assert_eq!((-n).abs(), n);
13896
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   503
    assert_eq!(-n, fp!(-15/2));
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   504
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   505
    assert_eq!(n.round(), 7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   506
    assert_eq!((-n).round(), -7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   507
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   508
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   509
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   510
fn zero() {
13896
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   511
    let z = fp!(0);
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   512
    let n = fp!(15/2);
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   513
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   514
    assert!(z.is_zero());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   515
    assert!(z.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   516
    assert!((-z).is_negative);
13888
2bfd7472ef1d Add some more tests
unc0rr
parents: 13887
diff changeset
   517
    assert_eq!(n - n, z);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13887
diff changeset
   518
    assert_eq!(-n + n, z);
13931
2717a5289d88 fix test
alfadur
parents: 13930
diff changeset
   519
    assert_eq!(n.with_sign_as(-n), -n);
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   520
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   521
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   522
#[test]
13889
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   523
fn ord() {
13896
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   524
    let z = fp!(0);
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   525
    let n1_5 = fp!(3/2);
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   526
    let n2_25 = fp!(9/4);
13889
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   527
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   528
    assert!(!(z > z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   529
    assert!(!(z < z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   530
    assert!(n2_25 > n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   531
    assert!(-n2_25 < n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   532
    assert!(-n2_25 < -n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   533
}
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   534
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13888
diff changeset
   535
#[test]
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   536
fn arith() {
13896
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   537
    let n1_5 = fp!(3/2);
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   538
    let n2_25 = fp!(9/4);
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   539
    let n_0_15 = fp!(-15/100);
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   540
13896
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   541
    assert_eq!(n1_5 + n1_5, fp!(3));
9ae1184886db add fpnum literal macro
alfadur
parents: 13890
diff changeset
   542
    assert_eq!(-n1_5 - n1_5, fp!(-3));
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   543
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   544
    assert_eq!(n1_5 * n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   545
    assert_eq!(-n1_5 * -n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   546
    assert_eq!(n1_5 * -n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   547
    assert_eq!(-n1_5 * n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   548
13888
2bfd7472ef1d Add some more tests
unc0rr
parents: 13887
diff changeset
   549
    assert_eq!(-n2_25 / -n1_5, n1_5);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13887
diff changeset
   550
    assert_eq!(n1_5 / -10, n_0_15);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13887
diff changeset
   551
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   552
    assert_eq!(n1_5.sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   553
    assert_eq!((-n1_5).sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   554
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   555
    assert_eq!(n2_25.sqrt(), n1_5);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   556
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   557
    assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25);
13934
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   558
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   559
    let mut m = fp!(1);
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   560
    m += n1_5;
ff77c9920007 add opassign implementations
alfadur
parents: 13933
diff changeset
   561
    assert_eq!(m, fp!(5/2));
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   562
}
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   563
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   564
#[test]
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   565
fn point() {
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   566
    let z = FPPoint::zero();
13937
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   567
    let n = fp!(16/9);
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   568
    let p = FPPoint::new(fp!(1), fp!(-2));
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   569
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   570
    assert_eq!(p.sqr_distance(), fp!(5));
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   571
    assert_eq!(p + -p, FPPoint::zero());
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   572
    assert_eq!(p * z, z);
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   573
    assert_eq!(p.dot(&z), fp!(0));
13937
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13934
diff changeset
   574
    assert_eq!(n * p, p * n);
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   575
    assert_eq!(distance(4, 3), fp!(5));
13932
cf28d7a2b7fe add scalar operations
alfadur
parents: 13931
diff changeset
   576
    assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
13944
665b4c6612ee fix fppoint.max_norm
alfadur
parents: 13937
diff changeset
   577
    assert_eq!(p.max_norm(), fp!(2));
13930
2ee07e751171 add more fpnum functions
alfadur
parents: 13910
diff changeset
   578
}