types.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. //! Type aliases used in the consensus codebase.
  19. use std::ops::{Add, AddAssign, Div, Mul, Sub};
  20. use dashu::{
  21. base::Abs,
  22. float::{round::mode::Zero, FBig, Repr},
  23. };
  24. use super::constants::RADIX_BITS;
  25. const B: u64 = 10;
  26. #[derive(Clone, PartialEq, PartialOrd, Debug)]
  27. pub struct Float10(FBig<Zero, B>);
  28. impl Float10 {
  29. pub fn repr(&self) -> &Repr<B> {
  30. self.0.repr()
  31. }
  32. pub fn abs(&self) -> Self {
  33. Self(self.0.clone().abs())
  34. }
  35. pub fn powf(&self, exp: Self) -> Self {
  36. Self(self.0.powf(&exp.0))
  37. }
  38. pub fn ln(&self) -> Self {
  39. Self(self.0.ln())
  40. }
  41. }
  42. impl Add for Float10 {
  43. type Output = Self;
  44. fn add(self, other: Self) -> Self {
  45. Self(self.0 + other.0)
  46. }
  47. }
  48. impl AddAssign for Float10 {
  49. fn add_assign(&mut self, other: Self) {
  50. *self = Self(self.0.clone() + other.0);
  51. }
  52. }
  53. impl Sub for Float10 {
  54. type Output = Self;
  55. fn sub(self, other: Self) -> Self {
  56. Self(self.0 - other.0)
  57. }
  58. }
  59. impl Mul for Float10 {
  60. type Output = Self;
  61. fn mul(self, other: Self) -> Self {
  62. Self(self.0 * other.0)
  63. }
  64. }
  65. impl Div for Float10 {
  66. type Output = Self;
  67. fn div(self, other: Self) -> Self {
  68. Self(self.0 / other.0)
  69. }
  70. }
  71. impl std::fmt::Display for Float10 {
  72. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  73. write!(f, "{}", self.0)
  74. }
  75. }
  76. impl TryFrom<&str> for Float10 {
  77. type Error = crate::Error;
  78. fn try_from(value: &str) -> Result<Self, Self::Error> {
  79. Ok(Self(FBig::from_str_native(value)?.with_precision(RADIX_BITS).value()))
  80. }
  81. }
  82. impl TryFrom<u64> for Float10 {
  83. type Error = crate::Error;
  84. fn try_from(value: u64) -> Result<Self, Self::Error> {
  85. Ok(Self(FBig::from(value)))
  86. }
  87. }
  88. impl TryFrom<i64> for Float10 {
  89. type Error = crate::Error;
  90. fn try_from(value: i64) -> Result<Self, Self::Error> {
  91. Ok(Self(FBig::from(value)))
  92. }
  93. }