magnus/r_float.rs
1use std::fmt;
2
3use rb_sys::{rb_float_new, rb_float_value, ruby_value_type, VALUE};
4
5#[cfg(ruby_use_flonum)]
6use crate::value::Flonum;
7use crate::{
8 error::Error,
9 float::Float,
10 into_value::IntoValue,
11 numeric::Numeric,
12 try_convert::TryConvert,
13 value::{
14 private::{self, ReprValue as _},
15 NonZeroValue, ReprValue, Value,
16 },
17 Ruby,
18};
19
20/// # `RFloat`
21///
22/// Functions that can be used to create Ruby `Float`s.
23///
24/// See also the [`RFloat`] type.
25impl Ruby {
26 /// Create a new `RFloat` from an `f64.`
27 ///
28 /// Returns `Ok(RFloat)` if `n` requires a high precision float, otherwise
29 /// returns `Err(Flonum)`.
30 ///
31 /// # Examples
32 ///
33 /// ```
34 /// use magnus::{rb_assert, Error, Ruby};
35 ///
36 /// fn example(ruby: &Ruby) -> Result<(), Error> {
37 /// let f = ruby.r_float_from_f64(1.7272337110188890e-77).unwrap();
38 /// rb_assert!(ruby, "f == 1.7272337110188890e-77", f);
39 ///
40 /// // can fit within a Flonum, so does not require an RFloat
41 /// assert!(ruby.r_float_from_f64(1.7272337110188893e-77).is_err());
42 ///
43 /// Ok(())
44 /// }
45 /// # Ruby::init(example).unwrap()
46 /// ```
47 #[cfg(ruby_use_flonum)]
48 pub fn r_float_from_f64(&self, n: f64) -> Result<RFloat, Flonum> {
49 unsafe {
50 let val = Value::new(rb_float_new(n));
51 RFloat::from_value(val)
52 .ok_or_else(|| Flonum::from_rb_value_unchecked(val.as_rb_value()))
53 }
54 }
55
56 /// Create a new `RFloat` from an `f64.`
57 ///
58 /// # Examples
59 ///
60 /// ```
61 /// use magnus::{rb_assert, Error, Ruby};
62 ///
63 /// fn example(ruby: &Ruby) -> Result<(), Error> {
64 /// let f = ruby.r_float_from_f64(1.7272337110188893e-77).unwrap();
65 /// rb_assert!(ruby, "f == 1.7272337110188893e-77", f);
66 ///
67 /// let f = ruby.r_float_from_f64(1.7272337110188890e-77).unwrap();
68 /// rb_assert!(ruby, "f == 1.7272337110188890e-77", f);
69 ///
70 /// Ok(())
71 /// }
72 /// # Ruby::init(example).unwrap()
73 /// ```
74 #[cfg(not(ruby_use_flonum))]
75 pub fn r_float_from_f64(&self, n: f64) -> Result<RFloat, RFloat> {
76 unsafe { Ok(RFloat::from_rb_value_unchecked(rb_float_new(n))) }
77 }
78}
79
80/// A Value pointer to an RFloat struct, Ruby's internal representation of
81/// high precision floating point numbers.
82///
83/// See also [`Float`] and [`Flonum`].
84///
85/// See the [`ReprValue`] trait for additional methods available on this type.
86/// See [`Ruby`](Ruby#rfloat) for methods to create an `RFloat`.
87#[derive(Clone, Copy)]
88#[repr(transparent)]
89pub struct RFloat(NonZeroValue);
90
91impl RFloat {
92 /// Return `Some(RFloat)` if `val` is a `RFloat`, `None` otherwise.
93 ///
94 /// # Examples
95 ///
96 /// ```
97 /// use magnus::{eval, RFloat};
98 /// # let _cleanup = unsafe { magnus::embed::init() };
99 ///
100 /// assert!(RFloat::from_value(eval("1.7272337110188890e-77").unwrap()).is_some());
101 /// // can fit within a Flonum, so does not require an RFloat
102 /// assert!(RFloat::from_value(eval("1.7272337110188893e-77").unwrap()).is_none());
103 /// // not an RFloat
104 /// assert!(RFloat::from_value(eval("1").unwrap()).is_none());
105 /// ```
106 #[inline]
107 pub fn from_value(val: Value) -> Option<Self> {
108 unsafe {
109 (val.rb_type() == ruby_value_type::RUBY_T_FLOAT
110 && (!cfg!(ruby_use_flonum) || !val.is_flonum()))
111 .then(|| Self(NonZeroValue::new_unchecked(val)))
112 }
113 }
114
115 #[inline]
116 pub(crate) unsafe fn from_rb_value_unchecked(val: VALUE) -> Self {
117 Self(NonZeroValue::new_unchecked(Value::new(val)))
118 }
119
120 /// Create a new `RFloat` from an `f64.`
121 ///
122 /// Returns `Ok(RFloat)` if `n` requires a high precision float, otherwise
123 /// returns `Err(Flonum)`.
124 ///
125 /// # Panics
126 ///
127 /// Panics if called from a non-Ruby thread. See [`Ruby::r_float_from_f64`]
128 /// for the non-panicking version.
129 ///
130 /// # Examples
131 ///
132 /// ```
133 /// # #![allow(deprecated)]
134 /// use magnus::{rb_assert, RFloat};
135 /// # let _cleanup = unsafe { magnus::embed::init() };
136 ///
137 /// let f = RFloat::from_f64(1.7272337110188890e-77).unwrap();
138 /// rb_assert!("f == 1.7272337110188890e-77", f);
139 ///
140 /// // can fit within a Flonum, so does not require an RFloat
141 /// assert!(RFloat::from_f64(1.7272337110188893e-77).is_err());
142 /// ```
143 #[cfg_attr(
144 not(feature = "old-api"),
145 deprecated(note = "please use `Ruby::r_float_from_f64` instead")
146 )]
147 #[cfg_attr(docsrs, doc(cfg(feature = "old-api")))]
148 #[cfg(ruby_use_flonum)]
149 #[inline]
150 pub fn from_f64(n: f64) -> Result<Self, Flonum> {
151 get_ruby!().r_float_from_f64(n)
152 }
153
154 /// Create a new `RFloat` from an `f64.`
155 ///
156 /// # Panics
157 ///
158 /// Panics if called from a non-Ruby thread. See [`Ruby::r_float_from_f64`]
159 /// for the non-panicking version.
160 ///
161 /// # Examples
162 ///
163 /// ```
164 /// # #![allow(deprecated)]
165 /// use magnus::{rb_assert, RFloat};
166 /// # let _cleanup = unsafe { magnus::embed::init() };
167 ///
168 /// let f = RFloat::from_f64(1.7272337110188893e-77).unwrap();
169 /// rb_assert!("f == 1.7272337110188893e-77", f);
170 ///
171 /// let f = RFloat::from_f64(1.7272337110188890e-77).unwrap();
172 /// rb_assert!("f == 1.7272337110188890e-77", f);
173 /// ```
174 #[cfg_attr(
175 not(feature = "old-api"),
176 deprecated(note = "please use `Ruby::r_float_from_f64` instead")
177 )]
178 #[cfg_attr(docsrs, doc(cfg(feature = "old-api")))]
179 #[cfg(not(ruby_use_flonum))]
180 #[inline]
181 pub fn from_f64(n: f64) -> Result<Self, Self> {
182 get_ruby!().r_float_from_f64(n)
183 }
184
185 /// Convert `self` to a `f64`.
186 ///
187 /// # Examples
188 ///
189 /// ```
190 /// use magnus::{eval, RFloat};
191 /// # let _cleanup = unsafe { magnus::embed::init() };
192 ///
193 /// let f: RFloat = eval("1.7272337110188890e-77").unwrap();
194 /// assert_eq!(f.to_f64(), 1.7272337110188890e-77);
195 /// ```
196 pub fn to_f64(self) -> f64 {
197 debug_assert_value!(self);
198 unsafe { rb_float_value(self.as_rb_value()) }
199 }
200}
201
202impl fmt::Display for RFloat {
203 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
204 write!(f, "{}", unsafe { self.to_s_infallible() })
205 }
206}
207
208impl fmt::Debug for RFloat {
209 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
210 write!(f, "{}", self.inspect())
211 }
212}
213
214impl IntoValue for RFloat {
215 #[inline]
216 fn into_value_with(self, _: &Ruby) -> Value {
217 self.0.get()
218 }
219}
220
221impl Numeric for RFloat {}
222
223unsafe impl private::ReprValue for RFloat {}
224
225impl ReprValue for RFloat {}
226
227impl TryConvert for RFloat {
228 fn try_convert(val: Value) -> Result<Self, Error> {
229 let float = Float::try_convert(val)?;
230 if let Some(rfloat) = RFloat::from_value(float.as_value()) {
231 Ok(rfloat)
232 } else {
233 Err(Error::new(
234 Ruby::get_with(val).exception_range_error(),
235 "float in range for flonum",
236 ))
237 }
238 }
239}