autocommit 2022-01-08 16:57:40 CET
This commit is contained in:
parent
f8e7557d1d
commit
33549184d4
42 changed files with 2072 additions and 190 deletions
47
src/fixed.rs
47
src/fixed.rs
|
|
@ -1,4 +1,5 @@
|
|||
use std::fmt::{Debug, Display, Formatter};
|
||||
use std::ops::{Add, AddAssign, Sub, SubAssign};
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
|
||||
#[repr(transparent)]
|
||||
|
|
@ -8,6 +9,18 @@ impl Fixed {
|
|||
pub fn from_1616(i: i32) -> Self {
|
||||
Self(i >> 8)
|
||||
}
|
||||
|
||||
pub fn from_int(i: i32) -> Self {
|
||||
Self(i << 8)
|
||||
}
|
||||
|
||||
pub fn round_down(self) -> i32 {
|
||||
self.0 >> 8
|
||||
}
|
||||
|
||||
pub fn apply_fract(self, i: i32) -> Self {
|
||||
Self((i << 8) | (self.0 & 255))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f64> for Fixed {
|
||||
|
|
@ -22,12 +35,6 @@ impl From<Fixed> for f64 {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<Fixed> for i32 {
|
||||
fn from(f: Fixed) -> Self {
|
||||
f.0 >> 8
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Fixed {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
Debug::fmt(&f64::from(*self), f)
|
||||
|
|
@ -39,3 +46,31 @@ impl Display for Fixed {
|
|||
Display::fmt(&f64::from(*self), f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Fixed {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
Self(self.0 - rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Fixed {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self(self.0 + rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for Fixed {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.0 += rhs.0;
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign for Fixed {
|
||||
fn sub_assign(&mut self, rhs: Self) {
|
||||
self.0 -= rhs.0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue