kalc_lib/
types.rs

1mod cdecimal;
2mod cf32;
3mod cf64;
4mod complex;
5mod decimal;
6mod float;
7mod integer;
8
9pub use cdecimal::CDecimal;
10pub use cf32::CF32;
11pub use cf64::CF64;
12pub use complex::Complex;
13pub use decimal::Decimal;
14pub use float::Float;
15pub use integer::Integer;
16
17use rug::ops::Pow as RugPow;
18
19//TODO malachite num maybe
20//TODO make real only an option
21
22#[derive(PartialEq)]
23pub enum IsPrime {
24    No,
25    Probably,
26    Yes,
27}
28impl From<rug::integer::IsPrime> for IsPrime {
29    fn from(value: rug::integer::IsPrime) -> Self {
30        match value {
31            rug::integer::IsPrime::No => IsPrime::No,
32            rug::integer::IsPrime::Probably => IsPrime::Probably,
33            rug::integer::IsPrime::Yes => IsPrime::Yes,
34        }
35    }
36}
37#[cfg(feature = "serde")]
38use serde::{Deserialize, Serialize};
39
40#[derive(PartialEq, Eq, Clone, Copy, Debug)]
41#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
42pub enum Type {
43    Rug,
44    Fastnum,
45    F64,
46    F32,
47}
48
49pub enum Special {
50    Pi,
51    Nan,
52    Infinity,
53}
54
55pub trait Prec {
56    fn prec(&self) -> u32;
57    fn set_prec(&mut self, new_prec: u32);
58}
59pub trait DivFloor {
60    fn div_floor(self, rhs: f64) -> Self;
61}
62impl DivFloor for f64 {
63    fn div_floor(self, rhs: f64) -> Self {
64        (self / rhs).floor()
65    }
66}
67pub trait Parse<T> {
68    fn parse(prec: u32, s: T) -> Option<Self>
69    where
70        Self: Sized;
71    fn parse_radix(prec: u32, s: T, base: i32) -> Option<Self>
72    where
73        Self: Sized;
74}
75pub trait ParseU<T> {
76    fn parse(t: Type, prec: u32, s: T) -> Option<Self>
77    where
78        Self: Sized;
79    fn parse_radix(t: Type, prec: u32, s: T, base: i32) -> Option<Self>
80    where
81        Self: Sized;
82}
83pub trait WithVal<T> {
84    fn with_val(obj: Type, prec: u32, val: T) -> Self;
85}
86pub trait NewVal {
87    fn new(obj: Type, prec: u32) -> Self;
88}
89pub trait WithValDeci<T> {
90    fn with_val(prec: u32, val: T) -> Self;
91}
92pub trait NewDeciVal {
93    fn new(prec: u32) -> Self;
94}
95pub trait Pow<T> {
96    fn pow(self, val: T) -> Self;
97}
98pub trait Rt<T> {
99    fn root(self, val: T) -> Self;
100}
101pub trait SinhCosh {
102    fn sinh_cosh(self) -> (Self, Self)
103    where
104        Self: Sized;
105}
106pub trait SpecialValuesDeci {
107    fn pi(prec: u32) -> Self;
108    fn nan(prec: u32) -> Self;
109    fn inf(prec: u32) -> Self;
110}
111pub trait SpecialValues {
112    fn pi(t: Type, prec: u32) -> Self;
113    fn nan(t: Type, prec: u32) -> Self;
114    fn inf(t: Type, prec: u32) -> Self;
115}
116
117impl SpecialValuesDeci for f64 {
118    fn pi(_: u32) -> Self {
119        std::f64::consts::PI
120    }
121    fn nan(_: u32) -> Self {
122        f64::NAN
123    }
124    fn inf(_: u32) -> Self {
125        f64::INFINITY
126    }
127}
128impl SpecialValuesDeci for f32 {
129    fn pi(_: u32) -> Self {
130        std::f32::consts::PI
131    }
132    fn nan(_: u32) -> Self {
133        f32::NAN
134    }
135    fn inf(_: u32) -> Self {
136        f32::INFINITY
137    }
138}
139use crate::macros::impls::*;
140impl_types!(f64, f32, i32, u64, u128);
141impl_sinh_cosh!(f64, f32, fastnum::decimal::D512, fastnum::decimal::D256);