1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use crate::scalar::macros::*;
use crate::*;
use std::convert::TryFrom;
use std::ops::{Add, Div, Mul, Neg, Sub};
use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
#[repr(C)]
pub struct Rfloat(pub f64);
impl Rfloat {
gen_impl!(Rfloat, f64);
pub fn is_nan(&self) -> bool {
self.0.is_nan()
}
pub fn is_sign_positive(&self) -> bool {
self.0.is_sign_positive()
}
pub fn is_sign_negative(&self) -> bool {
self.0.is_sign_negative()
}
pub fn is_infinite(&self) -> bool {
self.0.is_infinite()
}
pub fn is_subnormal(&self) -> bool {
self.0.is_subnormal()
}
}
gen_trait_impl!(Rfloat, f64, |x: &Rfloat| x.inner().is_na(), f64::na());
gen_from_primitive!(Rfloat, f64);
gen_from_scalar!(Rfloat, f64);
gen_sum_iter!(Rfloat, 0f64);
gen_binop!(
Rfloat,
f64,
Add,
|lhs: f64, rhs: f64| Some(lhs + rhs),
"Add two Rfloat values or an option of f64."
);
gen_binop!(
Rfloat,
f64,
Sub,
|lhs: f64, rhs: f64| Some(lhs - rhs),
"Subtract two Rfloat values or an option of f64."
);
gen_binop!(
Rfloat,
f64,
Mul,
|lhs: f64, rhs: f64| Some(lhs * rhs),
"Multiply two Rfloat values or an option of f64."
);
gen_binop!(
Rfloat,
f64,
Div,
|lhs: f64, rhs: f64| Some(lhs / rhs),
"Divide two Rfloat values or an option of f64."
);
gen_binopassign!(
Rfloat,
f64,
AddAssign,
|lhs: f64, rhs: f64| Some(lhs + rhs),
"Add two Rfloat values or an option of f64, modifying the left-hand side in place. Overflows to NA."
);
gen_binopassign!(
Rfloat,
f64,
SubAssign,
|lhs: f64, rhs: f64| Some(lhs - rhs),
"Subtract two Rfloat values or an option of f64, modifying the left-hand side in place. Overflows to NA."
);
gen_binopassign!(
Rfloat,
f64,
MulAssign,
|lhs: f64, rhs: f64| Some(lhs * rhs),
"Multiply two Rfloat values or an option of f64, modifying the left-hand side in place. Overflows to NA."
);
gen_binopassign!(
Rfloat,
f64,
DivAssign,
|lhs: f64, rhs: f64| Some(lhs / rhs),
"Divide two Rfloat values or an option of f64, modifying the left-hand side in place. Overflows to NA."
);
gen_unop!(Rfloat, Neg, |lhs: f64| Some(-lhs), "Negate a Rfloat value.");
impl TryFrom<&Robj> for Rfloat {
type Error = Error;
fn try_from(robj: &Robj) -> Result<Self> {
match robj.len() {
0 => return Err(Error::ExpectedNonZeroLength(robj.clone())),
1 => {}
_ => return Err(Error::ExpectedScalar(robj.clone())),
};
if robj.is_na() {
return Ok(Rfloat::na());
}
if let Some(v) = robj.as_real() {
return Ok(Rfloat::from(v));
}
if let Some(v) = robj.as_integer() {
return Ok(Rfloat::from(v as f64));
}
Err(Error::ExpectedNumeric(robj.clone()))
}
}
impl std::fmt::Debug for Rfloat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.is_na() {
write!(f, "NA_REAL")
} else {
self.inner().fmt(f)
}
}
}