Struct sibyl::Number

source ·
pub struct Number<'a> { /* private fields */ }
Expand description

Represents OTS types NUMBER, NUMERIC, INT, SHORTINT, REAL, DOUBLE PRECISION, FLOAT and DECIMAL.

Implementations§

source§

impl<'a> Number<'a>

source

pub fn new(ctx: &'a dyn Ctx) -> Self

Returns a new uninitialized number.

source

pub fn zero(ctx: &'a dyn Ctx) -> Self

Creates a new Number that is equal to zero.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::zero(&env);

assert!(num.is_zero()?);
source

pub fn pi(ctx: &'a dyn Ctx) -> Self

Creates a new Number that is equal to Pi.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::pi(&env);

assert_eq!(num.to_string("TM")?, "3.1415926535897932384626433832795028842");
source

pub fn from_string(txt: &str, fmt: &str, ctx: &'a dyn Ctx) -> Result<Self>

Creates a new Number from a string using specified format.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_string("6.62607004E-34", "9D999999999EEEE", &env)?;

assert_eq!(num.to_string("TME")?, "6.62607004E-34");
source

pub fn from_int<T: Integer>(val: T, ctx: &'a dyn Ctx) -> Result<Self>

Creates a new Number from an integer.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(42, &env)?;

assert!(num.is_int()?);
assert_eq!(num.to_int::<i32>()?, 42);
source

pub fn from_real<T: Real>(val: T, ctx: &'a dyn Ctx) -> Result<Self>

Creates a new Number from a floating point number.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(2.7182818284590452353602874713527, &env)?;

assert_eq!(num.to_string("TM")?, "2.71828182845905");
source

pub fn from_number(other: &'a Number<'_>) -> Result<Self>

Creates a clone of the other number.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(8128, &env)?;
let dup = Number::from_number(&num)?;

assert_eq!(dup.to_int::<i32>()?, 8128);
source

pub fn assign(&mut self, src: &Number<'_>) -> Result<()>

Assigns self the value of the specified number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let src = Number::from_int(33550336, &env)?;
let mut dst = Number::zero(&env);
assert_eq!(dst.to_int::<i32>()?, 0);

dst.assign(&src)?;
assert_eq!(dst.to_int::<i32>()?, 33550336);
source

pub fn to_string(&self, fmt: &str) -> Result<String>

Converts the given number to a character string according to the specified format.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(42, &env)?;
let txt = num.to_string("FM0G999")?;

assert_eq!(txt, "0,042");
source

pub fn to_int<T: Integer>(&self) -> Result<T>

Converts this Number into an integer (u128, u64, u32, u16, u8, i128, i64, i32, i16, i8).

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::pi(&env);
let val = num.to_int::<i32>()?;

assert_eq!(val, 3);
source

pub fn to_real<T: Real>(&self) -> Result<T>

Returns floating point representation of self

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::pi(&env);
let val = num.to_real::<f64>()?;

assert!(3.14159265358978 < val && val < 3.14159265358980);
source

pub fn is_zero(&self) -> Result<bool>

Test if this number is equal to zero

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let mut num = Number::zero(&env);

assert!(num.is_zero()?);

num.inc()?;

assert!(!num.is_zero()?);
source

pub fn is_int(&self) -> Result<bool>

Tests if this number is an integer

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::zero(&env);

assert!(num.is_int()?);

let num = Number::pi(&env);

assert!(!num.is_int()?);
source

pub fn inc(&mut self) -> Result<()>

Increments Oracle number in place

It is assumed that the input is an integer between 0 and 100^21-2. If the is input too large, it will be treated as 0 - the result will be an Oracle number 1. If the input is not a positive integer, the result will be unpredictable.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let mut num = Number::zero(&env);
num.inc()?;

assert_eq!(num.to_int::<i32>()?, 1);
source

pub fn dec(&mut self) -> Result<()>

Decrements Oracle number in place

It is assumed that the input is an integer between 0 and 100^21-2. If the is input too large, it will be treated as 0 - the result will be an Oracle number 1. If the input is not a positive integer, the result will be unpredictable.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let mut num = Number::from_int(97, &env)?;
num.dec()?;

assert_eq!(num.to_int::<i32>()?, 96);
source

pub fn sign(&self) -> Result<Ordering>

Returns sign of a number (as a result of comparing it to zero).

Example
use sibyl::{ self as oracle, Number };
use std::cmp::Ordering;
let env = oracle::env()?;

let num = Number::from_int(19, &env)?;

assert_eq!(num.sign()?, Ordering::Greater);

let num = Number::from_int(-17, &env)?;

assert_eq!(num.sign()?, Ordering::Less);

let num = Number::zero(&env);

assert_eq!(num.sign()?, Ordering::Equal);
source

pub fn compare(&self, other: &Self) -> Result<Ordering>

Compares self to a number.

Example
use sibyl::{ self as oracle, Number };
use std::cmp::Ordering;
let env = oracle::env()?;

let pi = Number::pi(&env);
let e = Number::from_real(2.71828182845905, &env)?;

assert_eq!(pi.compare(&e)?, Ordering::Greater);
assert_eq!(e.compare(&pi)?, Ordering::Less);
source

pub fn add(&self, num: &Number<'_>) -> Result<Self>

Adds a Number to this Number and returns the sum as a new Number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(19, &env)?;
let arg = Number::from_int(50, &env)?;
let res = num.add(&arg)?;

assert_eq!(res.to_int::<i32>()?, 69);
source

pub fn sub(&self, num: &Number<'_>) -> Result<Self>

Subtracts a Number from this Number and returns the difference as a new Number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(90, &env)?;
let arg = Number::from_int(21, &env)?;
let res = num.sub(&arg)?;

assert_eq!(res.to_int::<i32>()?, 69);
source

pub fn mul(&self, num: &Number<'_>) -> Result<Self>

Multiplies a Number to this Number and returns the product as a new Number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(3.5, &env)?;
let arg = Number::from_int(8, &env)?;
let res = num.mul(&arg)?;

assert!(res.is_int()?);
assert_eq!(res.to_int::<i32>()?, 28);
source

pub fn div(&self, num: &Number<'_>) -> Result<Self>

Divides a Number (dividend) by a Number (divisor) and returns the quotient as a new Number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(256, &env)?;
let arg = Number::from_int(8, &env)?;
let res = num.div(&arg)?;

assert!(res.is_int()?);
assert_eq!(res.to_int::<i32>()?, 32);
source

pub fn rem(&self, num: &Number<'_>) -> Result<Self>

Finds the remainder of the division of two Numbers and returns it as a new Number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(255, &env)?;
let arg = Number::from_int(32, &env)?;
let res = num.rem(&arg)?;

assert!(res.is_int()?);
assert_eq!(res.to_int::<i32>()?, 31);
source

pub fn pow(&self, num: &Number<'_>) -> Result<Self>

Raises a number to an arbitrary power and returns the result as a new Number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(2.55, &env)?;
let arg = Number::from_real(3.2, &env)?;
let res = num.pow(&arg)?;
let val = res.to_real::<f64>()?;

assert!(19.995330061114 < val && val < 19.995330061115);
source

pub fn powi(&self, num: i32) -> Result<Self>

Raises a number to an integer power and returns the result as a new Number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(2.55, &env)?;
let res = num.powi(3)?;
let val = res.to_real::<f64>()?;

assert!(16.581374999 < val && val < 16.581375001);
source

pub fn pow10(&self, num: i32) -> Result<Self>

Multiplies a number by by a power of 10 and returns the result as a new Number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(2.55, &env)?;
let res = num.pow10(2)?;
assert_eq!(res.to_int::<i32>()?, 255);

let res = res.pow10(-1)?;
assert_eq!(res.to_real::<f64>()?, 25.5);
source

pub fn trunc(&self, num: i32) -> Result<Self>

Truncates a number at a specified decimal place and returns the result as a new Number num is the number of decimal digits to the right of the decimal point to truncate at. Negative values are allowed.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::pi(&env);
let res = num.trunc(7)?;
assert_eq!(res.to_string("TM")?, "3.1415926");

let res = res.pow10(5)?;
assert_eq!(res.to_real::<f64>()?, 314159.26);

let res = res.trunc(-3)?;
assert_eq!(res.to_real::<f64>()?, 314000.0);
source

pub fn round(&self, num: i32) -> Result<Self>

Rounds a number to a specified decimal place and returns the result as a new Number. num is the number of decimal digits to the right of the decimal point to truncate at. Negative values are allowed.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::pi(&env);
let res = num.round(7)?;

assert_eq!(res.to_string("TM")?, "3.1415927");
source

pub fn prec(&self, num: i32) -> Result<Self>

Performs a floating point round with respect to the number of digits and returns the result as a new Number.

num is the number of decimal digits desired in the result.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::pi(&env);
let res = num.prec(10)?;

assert_eq!(res.to_string("TM")?, "3.141592654");
source

pub fn neg(&self) -> Result<Self>

Negates a number and returns the result as a new Number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(42, &env)?;
let res = num.neg()?;

assert_eq!(res.to_int::<i32>()?, -42);
source

pub fn abs(&self) -> Result<Self>

Returns the absolute value of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(-42, &env)?;
let res = num.abs()?;

assert_eq!(res.to_int::<i32>()?, 42);
source

pub fn ceil(&self) -> Result<Self>

Returns the smallers integer greater than or equal to a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::pi(&env);
let res = num.ceil()?;

assert!(res.is_int()?);
assert_eq!(res.to_int::<i32>()?, 4);
source

pub fn floor(&self) -> Result<Self>

Returns the largest integer less than or equal to a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::pi(&env);
let res = num.floor()?;

assert!(res.is_int()?);
assert_eq!(res.to_int::<i32>()?, 3);
source

pub fn sqrt(&self) -> Result<Self>

Returns the square root of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(121, &env)?;
let res = num.sqrt()?;

assert!(res.is_int()?);
assert_eq!(res.to_int::<i32>()?, 11);
source

pub fn sin(&self) -> Result<Self>

Return the sine in radians of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(0.52359877559, &env)?;
let res = num.sin()?;
let val = res.to_real::<f64>()?;

assert!(0.499999999 < val && val < 0.500000001);
source

pub fn asin(&self) -> Result<Self>

Return the arcsine in radians of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(0.5, &env)?;
let res = num.asin()?;
let val = res.to_real::<f64>()?;

assert!(0.523598775 < val && val < 0.523598776);
source

pub fn sinh(&self) -> Result<Self>

Return the hyperbolic sine in radians of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(0.88137358702, &env)?;
let res = num.sinh()?;
let val = res.to_real::<f64>()?;

assert!(0.999999999 < val && val < 1.000000001);
source

pub fn cos(&self) -> Result<Self>

Return the cosine in radians of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(1.0471975512, &env)?;
let res = num.cos()?;
let val = res.to_real::<f64>()?;

assert!(0.499999999 < val && val < 0.500000001);
source

pub fn acos(&self) -> Result<Self>

Return the arccosine in radians of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(0.5, &env)?;
let res = num.acos()?;
let val = res.to_real::<f64>()?;

assert!(1.047197551 < val && val < 1.047197552);
source

pub fn cosh(&self) -> Result<Self>

Return the hyperbolic cosine in radians of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(0.96242365012, &env)?;
let res = num.cosh()?;
let val = res.to_real::<f64>()?;

assert!(1.499999999 < val && val < 1.500000001);
source

pub fn tan(&self) -> Result<Self>

Return the tangent in radians of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(0.785398163397, &env)?;
let res = num.tan()?;
let val = res.to_real::<f64>()?;

assert!(0.999999999 < val && val < 1.000000001);
source

pub fn atan(&self) -> Result<Self>

Return the arctangent in radians of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(1, &env)?;
let res = num.atan()?;
let val = res.to_real::<f64>()?;

assert!(0.785398163 < val && val < 0.785398164);
source

pub fn atan2(&self, num: &Number<'_>) -> Result<Self>

Returns the four quadrant arctangent of self and num in radians

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let x = Number::from_int(4, &env)?;
let y = Number::from_int(-3, &env)?;
let res = x.atan2(&y)?;
let val = res.to_real::<f64>()?;

assert!(2.2142974355 < val && val < 2.2142974356);
source

pub fn tanh(&self) -> Result<Self>

Returns the hyperbolic tangent in radians of a number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(0.54930614434, &env)?;
let res = num.tanh()?;
let val = res.to_real::<f64>()?;

assert!(0.499999999 < val && val < 0.500000001);
source

pub fn exp(&self) -> Result<Self>

Returns e^(self) - the exponential function

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(2.71828182845905, &env)?;
let res = num.exp()?;
let val = res.to_real::<f64>()?;

assert!(15.154262241 < val && val < 15.154262242);
source

pub fn ln(&self) -> Result<Self>

Returns the natural logarithm of the number

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_real(2.71828182845905, &env)?;
let res = num.ln()?;
let val = res.to_real::<f64>()?;

assert!(0.9999999999 < val && val < 1.0000000001);
source

pub fn log(&self, num: &Number<'_>) -> Result<Self>

Returns the logarithm of the numer using with respect to an arbitrary base.

Example
use sibyl::{ self as oracle, Number };
let env = oracle::env()?;

let num = Number::from_int(65536, &env)?;
let base = Number::from_int(4, &env)?;
let res = num.log(&base)?;

assert_eq!(res.to_int::<i32>()?, 8);
source

pub fn size(&self) -> usize

Trait Implementations§

source§

impl Debug for Number<'_>

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Deref for Number<'_>

§

type Target = OCINumber

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl DerefMut for Number<'_>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<'a> FromSql<'a> for Number<'a>

source§

fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self>

Converts, if possible, data stored in the column buffer into the requested type and returns the created value. Read more
source§

impl ToSql for &Number<'_>

source§

fn bind_to(
&mut self,
pos: usize,
params: &mut Params,
stmt: &OCIStmt,
err: &OCIError
) -> Result<usize>

Binds itself to the SQL parameter placeholder Read more
source§

fn update_from_bind(&mut self, pos: usize, _params: &Params) -> Result<usize>

A callback that is called to update OUT (or INOUT) argumetns. For example, to set the length of the received data.
source§

impl ToSql for &mut Number<'_>

source§

fn bind_to(
&mut self,
pos: usize,
params: &mut Params,
stmt: &OCIStmt,
err: &OCIError
) -> Result<usize>

Binds itself to the SQL parameter placeholder Read more
source§

fn update_from_bind(&mut self, pos: usize, _params: &Params) -> Result<usize>

A callback that is called to update OUT (or INOUT) argumetns. For example, to set the length of the received data.
source§

impl ToSql for Number<'_>

source§

fn bind_to(
&mut self,
pos: usize,
params: &mut Params,
stmt: &OCIStmt,
err: &OCIError
) -> Result<usize>

Binds itself to the SQL parameter placeholder Read more
source§

fn update_from_bind(&mut self, pos: usize, _params: &Params) -> Result<usize>

A callback that is called to update OUT (or INOUT) argumetns. For example, to set the length of the received data.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Number<'a>

§

impl<'a> Send for Number<'a>

§

impl<'a> Sync for Number<'a>

§

impl<'a> Unpin for Number<'a>

§

impl<'a> !UnwindSafe for Number<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere
T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.