Struct rug::float::SmallFloat [] [src]

#[repr(C)]
pub struct SmallFloat { /* fields omitted */ }

A small float that does not require any memory allocation.

This can be useful when you have a primitive number type but need a reference to a Float. The SmallFloat will have a precision according to the type of the primitive used to set its value.

  • i8, u8: the SmallFloat will have eight bits of precision.
  • i16, u16: the SmallFloat will have 16 bits of precision.
  • i32, u32: the SmallFloat will have 32 bits of precision.
  • i64, u64: the SmallFloat will have 64 bits of precision.
  • f32: the SmallFloat will have 24 bits of precision.
  • f64: the SmallFloat will have 53 bits of precision.

The SmallFloat type can be coerced to a Float, as it implements Deref with a Float target.

Examples

use rug::Float;
use rug::float::SmallFloat;
// `a` requires a heap allocation, has 53-bit precision
let mut a = Float::with_val(53, 250);
// `b` can reside on the stack
let b = SmallFloat::from(-100f64);
a += &*b;
assert_eq!(a, 150);
// another computation:
a *= &*b;
assert_eq!(a, -15000);

Methods

impl SmallFloat
[src]

Creates a SmallFloat with value 0.

Methods from Deref<Target = Float>

Returns the precision.

Converts to an integer, rounding to the nearest.

Converts to an integer, applying the specified rounding method.

If the value is a finite number, returns an Integer and exponent such that self is exactly equal to the integer multiplied by two raised to the power of the exponent.

Examples

use rug::{Assign, Float};
use rug::float::Special;
let mut float = Float::with_val(16, 6.5);
// 6.5 in binary is 110.1
// Since the precision is 16 bits, this becomes
// 1101_0000_0000_0000 times two to the power of -12
let (int, exp) = float.to_integer_exp().unwrap();
assert_eq!(int, 0b1101_0000_0000_0000);
assert_eq!(exp, -13);

float.assign(0);
let (zero, _) = float.to_integer_exp().unwrap();
assert_eq!(zero, 0);

float.assign(Special::Infinity);
assert!(float.to_integer_exp().is_none());

If the value is a finite number, returns a Rational number preserving all the precision of the value.

Examples

use rug::{Float, Rational};
use rug::float::Round;
use std::str::FromStr;
use std::cmp::Ordering;

// Consider the number 123,456,789 / 10,000,000,000.
let res = Float::from_str_round("0.0123456789", 35, Round::Down);
let (f, f_rounding) = res.unwrap();
assert_eq!(f_rounding, Ordering::Less);
let r = Rational::from_str("123456789/10000000000").unwrap();
// Set fr to the value of f exactly.
let fr = f.to_rational().unwrap();
// Since f == fr and f was rounded down, r != fr.
assert_ne!(r, fr);
let (frf, frf_rounding) = Float::with_val_round(35, &fr, Round::Down);
assert_eq!(frf_rounding, Ordering::Equal);
assert_eq!(frf, f);
assert_eq!(format!("{:.9}", frf), "1.23456789e-2");

In the following example, the Float values can be represented exactly.

use rug::Float;

let large_f = Float::with_val(16, 6.5);
let large_r = large_f.to_rational().unwrap();
let small_f = Float::with_val(16, -0.125);
let small_r = small_f.to_rational().unwrap();

assert_eq!(*large_r.numer(), 13);
assert_eq!(*large_r.denom(), 2);
assert_eq!(*small_r.numer(), -1);
assert_eq!(*small_r.denom(), 8);

Converts to an i32, rounding to the nearest.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned. If the value is a NaN, None is returned.

Converts to an i32, applying the specified rounding method.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned. If the value is a NaN, None is returned.

Converts to a u32, rounding to the nearest.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned. If the value is a NaN, None is returned.

Converts to a u32, applying the specified rounding method.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned. If the value is a NaN, None is returned.

Converts to an f32, rounding to the nearest.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned.

Converts to an f32, applying the specified rounding method.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned.

Converts to an f64, rounding to the nearest.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned.

Converts to an f64, applying the specified rounding method.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned.

Converts to an f32 and an exponent, rounding to the nearest.

The returned f32 is in the range 0.5 ≤ x < 1.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned.

Examples

use rug::Float;
let zero = Float::new(64);
let (d0, exp0) = zero.to_f32_exp();
assert_eq!((d0, exp0), (0.0, 0));
let three_eighths = Float::with_val(64, 0.375);
let (d3_8, exp3_8) = three_eighths.to_f32_exp();
assert_eq!((d3_8, exp3_8), (0.75, -1));

Converts to an f32 and an exponent, applying the specified rounding method.

The returned f32 is in the range 0.5 ≤ x < 1.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned.

Examples

use rug::Float;
use rug::float::Round;
let frac_10_3 = Float::with_val(64, 10) / 3u32;
let (f_down, exp_down) = frac_10_3.to_f32_exp_round(Round::Down);
assert_eq!((f_down, exp_down), (0.8333333, 2));
let (f_up, exp_up) = frac_10_3.to_f32_exp_round(Round::Up);
assert_eq!((f_up, exp_up), (0.8333334, 2));

Converts to an f64 and an exponent, rounding to the nearest.

The returned f64 is in the range 0.5 ≤ x < 1.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned.

Converts to an f64 and an exponent, applying the specified rounding method.

The returned f64 is in the range 0.5 ≤ x < 1.

If the value is too small or too large for the target type, the minimum or maximum value allowed is returned.

Returns a string representation of self for the specified radix rounding to the nearest.

The exponent is encoded in decimal. The output string will have enough precision such that reading it again will give the exact same number.

Examples

use rug::Float;
use rug::float::Special;
let neg_inf = Float::with_val(53, Special::MinusInfinity);
assert_eq!(neg_inf.to_string_radix(10, None), "-inf");
assert_eq!(neg_inf.to_string_radix(16, None), "-@inf@");
let twentythree = Float::with_val(8, 23);
assert_eq!(twentythree.to_string_radix(10, None), "2.300e1");
assert_eq!(twentythree.to_string_radix(16, None), "1.70@1");
assert_eq!(twentythree.to_string_radix(10, Some(2)), "2.3e1");
assert_eq!(twentythree.to_string_radix(16, Some(4)), "1.700@1");

Panics

Panics if radix is less than 2 or greater than 36.

Returns a string representation of self for the specified radix applying the specified rounding method.

The exponent is encoded in decimal. The output string will have enough precision such that reading it again will give the exact same number.

Panics

Panics if radix is less than 2 or greater than 36.

Returns true if self is an integer.

Returns true if self is not a number.

Returns true if self is plus or minus infinity.

Returns true if self is a finite number, that is neither NaN nor infinity.

Returns true if self is plus or minus zero.

Returns true if self is a normal number, that is neither NaN, nor infinity, nor zero. Note that Float cannot be subnormal.

Returns Ordering::Less if self is less than zero, Ordering::Greater if self is greater than zero, or Ordering::Equal if self is equal to zero.

Compares the absolute values of self and other.

Returns the exponent of self if self is a normal number, otherwise None. The significand is assumed to be in the range [0.5,1).

Returns the sign bit, that is true if the number is negative.

Computes the square, rounding to the nearest.

Compuets the square.

Computes the square root, rounding to the nearest.

Computes the square root.

Computes the reciprocal square root, rounding to the nearest.

Computes the reciprocal square root.

Computes the cube root, rounding to the nearest.

Computes the cube root.

Computes the kth root, rounding to the nearest.

Computes the kth root.

Computes the absolute value.

Computes the absolute value.

Computes the reciprocal, rounding to the nearest.

Computes the reciprocal.

Computes the positive difference between self and other, rounding to the nearest.

Computes the positive difference.

Computes the natural logarithm, rounding to the nearest.

Computes the natural logarithm.

Computes the logarithm to base 2, rounding to the nearest.

Computes the logarithm to base 2.

Computes the logarithm to base 10, rounding to the nearest.

Computes the logarithm to base 10.

Computes the exponential, rounding to the nearest.

Computes the exponential.

Computes 2 to the power of self, rounding to the nearest.

Computes 2 to the power of the value.

Computes 10 to the power of self, rounding to the nearest.

Computes 10 to the power of the value.

Computes the sine, rounding to the nearest.

Computes the sine.

Computes the cosine, rounding to the nearest.

Computes the cosine.

Computes the tangent, rounding to the nearest.

Computes the tangent.

Computes the sine and cosine of self, rounding to the nearest.

The sine is stored in self and keeps its precision, while the cosine is stored in cos keeping its precision.

Computes the sine and cosine.

Examples

use rug::{Assign, Float};
// sin(0.5) = 0.47943, cos(0.5) = 0.87758
let angle = Float::with_val(53, 0.5);
let r = angle.sin_cos_ref();
// use only 10 bits of precision here to
// make comparison easier
let (mut sin, mut cos) = (Float::new(10), Float::new(10));
(&mut sin, &mut cos).assign(r);
assert_eq!(sin, Float::with_val(10, 0.47943));
assert_eq!(cos, Float::with_val(10, 0.87748));

Computes the secant, rounding to the nearest.

Computes the secant.

Computes the cosecant, rounding to the nearest.

Computes the cosecant.

Computes the cotangent, rounding to the nearest.

Computes the cotangent.

Computes the arc-cosine, rounding to the nearest.

Computes the arc-cosine.

Computes the arc-sine, rounding to the nearest.

Computes the arc-sine.

Computes the arc-tangent, rounding to the nearest.

Computes the arc-tangent.

Computes the arc-tangent2 of self and other, rounding to the nearest.

This is similar to the arc-tangent of self / other, except in the cases when either self or other or both are zero or infinity.

Computes the arc-tangent.

Computes the hyperbolic sine, rounding to the nearest.

Computes the hyperbolic sine.

Computes the hyperbolic cosine, rounding to the nearest.

Computes the hyperbolic cosine.

Computes the hyperbolic tangent, rounding to the nearest.

Computes the hyperbolic tangent.

Computes the hyperbolic sine and cosine of self, rounding to the nearest.

The sine is stored in self and keeps its precision, while the cosine is stored in cos keeping its precision.

Computes the hyperbolic sine and cosine.

Examples

use rug::{Assign, Float};
// sinh(0.5) = 0.52110, cosh(0.5) = 1.1276
let angle = Float::with_val(53, 0.5);
let r = angle.sinh_cosh_ref();
// use only 10 bits of precision here to
// make comparison easier
let (mut sinh, mut cosh) = (Float::new(10), Float::new(10));
(&mut sinh, &mut cosh).assign(r);
assert_eq!(sinh, Float::with_val(10, 0.52110));
assert_eq!(cosh, Float::with_val(10, 1.1276));

Computes the hyperbolic secant, rounding to the nearest.

Computes the hyperbolic secant.

Computes the hyperbolic cosecant, rounding to the nearest.

Computes the hyperbolic cosecant.

Computes the hyperbolic cotangent, rounding to the nearest.

Computes the hyperbolic cotangent.

Computes the inverse hyperbolic cosine, rounding to the nearest.

Computes the inverse hyperbolic cosine

Computes the inverse hyperbolic sine, rounding to the nearest.

Computes the inverse hyperbolic sine.

Computes the inverse hyperbolic tangent, rounding to the nearest.

Computes the inverse hyperbolic tangent.

Computes the natural logarithm of one plus self, rounding to the nearest.

Computes the natural logorithm of one plus the value.

Subtracts one from the exponential of self, rounding to the nearest.

Computes one less than the exponential of the value.

Computes the exponential integral, rounding to the nearest.

Computes the exponential integral.

Computes the real part of the dilogarithm of self, rounding to the nearest.

Computes the real part of the dilogarithm of the value.

Computes the value of the Gamma function on self, rounding to the nearest.

Computes the Gamma function on the value.

Computes the logarithm of the Gamma function on self, rounding to the nearest.

Computes the logarithm of the Gamma function on the value.

Computes the logarithm of the absolute value of the Gamma function on self, rounding to the nearest.

Returns Ordering::Less if the Gamma function is negative, or Ordering::Greater if the Gamma function is positive.

Examples

use rug::Float;
use rug::float::Constant;
use std::cmp::Ordering;

// gamma of 1/2 is sqrt(pi)
let ln_gamma_64 = Float::with_val(64, Constant::Pi).sqrt().ln();

let f = Float::with_val(53, 0.5);
let (ln_gamma, sign) = f.ln_abs_gamma();
// gamma of 1/2 is positive
assert_eq!(sign, Ordering::Greater);
// check to 53 significant bits
assert_eq!(ln_gamma, Float::with_val(53, &ln_gamma_64));

If the Gamma function is negative, the sign returned is Ordering::Less.

use rug::Float;
use rug::float::Constant;
use std::cmp::Ordering;

// gamma of -1/2 is -2 * sqrt(pi)
let abs_gamma_64 = Float::with_val(64, Constant::Pi).sqrt() * 2u32;
let ln_gamma_64 = abs_gamma_64.ln();

let f = Float::with_val(53, -0.5);
let (ln_gamma, sign) = f.ln_abs_gamma();
// gamma of -1/2 is negative
assert_eq!(sign, Ordering::Less);
// check to 53 significant bits
assert_eq!(ln_gamma, Float::with_val(53, &ln_gamma_64));

Computes the logarithm of the absolute value of the Gamma function on val.

Examples

use rug::{Assign, Float};
use rug::float::Constant;
use std::cmp::Ordering;

let neg1_2 = Float::with_val(53, -0.5);
// gamma of -1/2 is -2 * sqrt(pi)
let abs_gamma_64 = Float::with_val(64, Constant::Pi).sqrt() * 2u32;
let ln_gamma_64 = abs_gamma_64.ln();

// Assign rounds to the nearest
let r = neg1_2.ln_abs_gamma_ref();
let (mut f, mut sign) = (Float::new(53), Ordering::Equal);
(&mut f, &mut sign).assign(r);
// gamma of -1/2 is negative
assert_eq!(sign, Ordering::Less);
// check to 53 significant bits
assert_eq!(f, Float::with_val(53, &ln_gamma_64));

Computes the value of the Digamma function on self, rounding to the nearest.

Computes the Digamma function on the value.

Computes the value of the Riemann Zeta function on self, rounding to the nearest.

Computes the Riemann Zeta function on the value.

Computes the value of the error function, rounding to the nearest.

Computes the error function.

Computes the value of the complementary error function, rounding to the nearest.

Computes the complementary error function.

Computes the value of the first kind Bessel function of order 0, rounding to the nearest.

Computes the first kind Bessel function of order 0.

Computes the value of the first kind Bessel function of order 1, rounding to the nearest.

Computes the first kind Bessel function of order 1.

Computes the value of the first kind Bessel function of order n, rounding to the nearest.

Computes the first kind Bessel function of order n.

Computes the value of the second kind Bessel function of order 0, rounding to the nearest.

Computes the second kind Bessel function of order 0.

Computes the value of the second kind Bessel function of order 1, rounding to the nearest.

Computes the second kind Bessel function of order 1.

Computes the value of the second kind Bessel function of order n, rounding to the nearest.

Computes the second kind Bessel function of order n.

Computes the arithmetic-geometric mean of self and other, rounding to the nearest.

Computes the arithmetic-geometric mean.

Computes the Euclidean norm of self and other, rounding to the nearest.

Computes the Euclidean norm.

Computes the value of the Airy function Ai on self, rounding to the nearest.

Computes the Airy function Ai on the value.

Rounds up to the next higher integer.

Rounds up to the next higher integer. The result may be rounded again when assigned to the target.

Rounds down to the next lower integer.

Rounds down to the next lower integer. The result may be rounded again when assigned to the target.

Rounds to the nearest integer, rounding half-way cases away from zero.

Rounds to the nearest integer, rounding half-way cases away from zero. The result may be rounded again when assigned to the target.

Examples

use rug::{AssignRound, Float};
use rug::float::Round;
let f = Float::with_val(53, 6.5);
// 6.5 (binary 110.1) is rounded to 7 (binary 111)
let r = f.round_ref();
// use only 2 bits of precision in destination
let mut dst = Float::new(2);
// 7 (binary 111) is rounded to 8 (binary 1000) by
// round-even rule in order to store in 2-bit Float, even
// though 6 (binary 110) is closer to original 6.5).
dst.assign_round(r, Round::Nearest);
assert_eq!(dst, 8);

Rounds to the next integer towards zero.

Rounds to the next integer towards zero. The result may be rounded again when assigned to the target.

Gets the fractional part of the number.

Gets the fractional part of the number.

Gets the integer and fractional parts of the number, rounding to the nearest.

The integer part is stored in self and keeps its precision, while the fractional part is stored in fract keeping its precision.

Gets the integer and fractional parts of the number.

Trait Implementations

impl Default for SmallFloat
[src]

Returns the "default value" for a type. Read more

impl Deref for SmallFloat
[src]

The resulting type after dereferencing

The method called to dereference a value

impl<T> From<T> for SmallFloat where
    SmallFloat: Assign<T>, 
[src]

Performs the conversion.

impl Assign<i8> for SmallFloat
[src]

Peforms the assignement. Read more

impl Assign<i16> for SmallFloat
[src]

Peforms the assignement. Read more

impl Assign<i32> for SmallFloat
[src]

Peforms the assignement. Read more

impl Assign<i64> for SmallFloat
[src]

Peforms the assignement. Read more

impl Assign<u8> for SmallFloat
[src]

Peforms the assignement. Read more

impl Assign<u16> for SmallFloat
[src]

Peforms the assignement. Read more

impl Assign<u32> for SmallFloat
[src]

Peforms the assignement. Read more

impl Assign<u64> for SmallFloat
[src]

Peforms the assignement. Read more

impl Assign<f32> for SmallFloat
[src]

Peforms the assignement. Read more

impl Assign<f64> for SmallFloat
[src]

Peforms the assignement. Read more