pub struct Float(/* private fields */);
Expand description
Implementations§
source§impl Float
impl Float
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(Float)
if val
is a Float
, None
otherwise.
§Examples
use magnus::{eval, Float};
assert!(Float::from_value(eval("1.7272337110188893e-77").unwrap()).is_some());
assert!(Float::from_value(eval("1.7272337110188890e-77").unwrap()).is_some());
assert!(Float::from_value(eval("1").unwrap()).is_none());
sourcepub fn from_f64(n: f64) -> Self
pub fn from_f64(n: f64) -> Self
Create a new Float
from an f64
.
§Panics
Panics if called from a non-Ruby thread. See Ruby::float_from_f64
for the non-panicking version.
§Examples
use magnus::{rb_assert, Float};
let f = Float::from_f64(1.7272337110188893e-77);
rb_assert!("f == 1.7272337110188893e-77", f);
let f = Float::from_f64(1.7272337110188890e-77);
rb_assert!("f == 1.7272337110188890e-77", f);
sourcepub fn to_f64(self) -> f64
pub fn to_f64(self) -> f64
Convert self
to a f64
.
§Examples
use magnus::{eval, Float};
let f: Float = eval("2.0").unwrap();
assert_eq!(f.to_f64(), 2.0);
sourcepub fn rationalize_with_prec(self, prec: Self) -> RRational
pub fn rationalize_with_prec(self, prec: Self) -> RRational
Returns a rational approximation of self
.
§Examples
use magnus::{rb_assert, Float};
let pi = Float::from_f64(3.141592);
let r = pi.rationalize_with_prec(Float::from_f64(0.001));
rb_assert!("r == 201/64r", r);
let r = pi.rationalize_with_prec(Float::from_f64(0.01));
rb_assert!("r == 22/7r", r);
let r = pi.rationalize_with_prec(Float::from_f64(0.1));
rb_assert!("r == 16/5r", r);
let r = pi.rationalize_with_prec(Float::from_f64(1.));
rb_assert!("r == 3/1r", r);
sourcepub fn rationalize(self) -> RRational
pub fn rationalize(self) -> RRational
Returns a rational approximation of self
.
§Examples
use magnus::{rb_assert, Float};
let pi = Float::from_f64(3.141592);
rb_assert!("r = 392699/125000r", r = pi.rationalize());
Trait Implementations§
source§impl Numeric for Float
impl Numeric for Float
source§fn coerce_bin<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
fn coerce_bin<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
Apply the operator
op
with coercion. Read moresource§fn coerce_cmp<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
fn coerce_cmp<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
Apply the operator
op
with coercion. Read moresource§fn coerce_relop<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
fn coerce_relop<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
Apply the operator
op
with coercion. Read moresource§impl ReprValue for Float
impl ReprValue for Float
source§fn equal<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
fn equal<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
Checks for equality, delegating to the Ruby method
#==
. Read moresource§fn eql<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
fn eql<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
Checks for equality, delegating to the Ruby method
#eql?
. Read moresource§fn hash(self) -> Result<Integer, Error>
fn hash(self) -> Result<Integer, Error>
Returns an integer non-uniquely identifying
self
. Read moresource§fn to_bool(self) -> bool
fn to_bool(self) -> bool
Convert
self
to a bool
, following Ruby’s rules of false
and nil
as boolean false
and everything else boolean true
. Read moresource§fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
source§fn block_call<M, A, R, T>(
self,
method: M,
args: A,
block: fn(_: &[Value], _: Option<Proc>) -> R
) -> Result<T, Error>
fn block_call<M, A, R, T>( self, method: M, args: A, block: fn(_: &[Value], _: Option<Proc>) -> R ) -> Result<T, Error>
source§fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>where
M: IntoId,
fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>where
M: IntoId,
Check if
self
responds to the given Ruby method. Read moresource§fn is_kind_of<T>(self, class: T) -> bool
fn is_kind_of<T>(self, class: T) -> bool
source§fn enumeratorize<M, A>(self, method: M, args: A) -> Enumerator ⓘwhere
M: IntoSymbol,
A: ArgList,
fn enumeratorize<M, A>(self, method: M, args: A) -> Enumerator ⓘwhere
M: IntoSymbol,
A: ArgList,
source§impl TryConvert for Float
impl TryConvert for Float
impl Copy for Float
Auto Trait Implementations§
impl Freeze for Float
impl RefUnwindSafe for Float
impl Send for Float
impl Sync for Float
impl Unpin for Float
impl UnwindSafe for Float
Blanket Implementations§
source§impl<T> AsRawValue for Twhere
T: ReprValue,
impl<T> AsRawValue for Twhere
T: ReprValue,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more