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