Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f8624cf
Adding Mul and Div, also adding more functions for different number t…
Vasyl-Trefilov Mar 25, 2026
fd530d6
fixing all Divide problems, also adding a few new fucntions and first…
Vasyl-Trefilov Mar 27, 2026
b174bf1
Avoiding clone in Add
Vasyl-Trefilov Mar 31, 2026
a34cae2
making Add insane fast and also making code more readable. In next co…
Vasyl-Trefilov Apr 4, 2026
9ea939e
Adding a lot of docs for Values/Colors, also implementing And/Or/Not …
Vasyl-Trefilov Apr 7, 2026
3a44206
Fixing some little errors
Vasyl-Trefilov Apr 7, 2026
1d0942e
not fully, but fixing the clone problems and adding Rem(modulo) also …
Vasyl-Trefilov Apr 21, 2026
9fc842b
Now CoreValue can use rem(Math modulo)
Vasyl-Trefilov Apr 24, 2026
d9ac724
Adding 'Set' Data Type to CoreValue for absolute no reason
Vasyl-Trefilov Apr 26, 2026
802a52a
implementing 'Set' to the end, now its using '<|' and '|>' e.g. '<|1,…
Vasyl-Trefilov Apr 27, 2026
a18cb26
fixing some things
Vasyl-Trefilov Apr 27, 2026
eb08e4c
fixing some things again
Vasyl-Trefilov Apr 27, 2026
70345d0
Now Set is correct with all methods that must have. Hope that Git Act…
Vasyl-Trefilov Apr 28, 2026
7e2ace6
Now must work
Vasyl-Trefilov Apr 28, 2026
48ab02c
Now it must work
Vasyl-Trefilov Apr 28, 2026
b51522b
I dont know
Vasyl-Trefilov Apr 28, 2026
d78f1b6
Now I am 99% sure
Vasyl-Trefilov Apr 28, 2026
dde0ce0
fixing performance in Decimal and TypedDecimal
Vasyl-Trefilov Apr 28, 2026
742e9a3
Now Set is valid datex syntax, e.g. '<|1|>'
Vasyl-Trefilov Apr 29, 2026
9a555f7
Rewriting the extend-datex-syntax.md and also finding some misses in …
Vasyl-Trefilov Apr 30, 2026
f96677d
hmm
Vasyl-Trefilov Apr 30, 2026
b10375c
Now its possible to use [2, 4].len() in datex, so I made Methods poss…
Vasyl-Trefilov May 1, 2026
eba4996
Now Type methods are more easy to implement and has much cleaner stru…
Vasyl-Trefilov May 1, 2026
9b6481e
maybe last commit in this repo, I will miss it
Vasyl-Trefilov May 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ pub fn handle_arithmetic_operation(
match operator {
ArithmeticOperator::Add => Ok((lhs + rhs)?),
ArithmeticOperator::Subtract => Ok((lhs - rhs)?),
// ArithmeticOperator::Multiply => {
// Ok((active_value_container * &value_container)?)
// }
// ArithmeticOperator::Divide => {
// Ok((active_value_container / &value_container)?)
// }
ArithmeticOperator::Multiply => {
Ok((lhs * rhs)?)
}
ArithmeticOperator::Divide => {
Ok((lhs / rhs)?)
}
_ => {
core::todo!(
"#408 Implement arithmetic operation for {:?}",
Expand Down
358 changes: 356 additions & 2 deletions crates/datex-core/src/values/core_value.rs

Large diffs are not rendered by default.

109 changes: 105 additions & 4 deletions crates/datex-core/src/values/core_values/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use core::{
cmp::Ordering,
fmt::Display,
hash::Hash,
ops::{Add, Neg, Sub},
ops::{Add, Neg, Sub, Mul, Div},
str::FromStr,
};
use num::{BigInt, BigRational};
Expand All @@ -28,7 +28,7 @@ use num_traits::{FromPrimitive, Zero};
use rational::Rational;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, Serialize, Deserialize, PartialOrd)]
pub enum Decimal {
Finite(Rational),
Comment thread
Vasyl-Trefilov marked this conversation as resolved.
Nan,
Expand Down Expand Up @@ -69,6 +69,20 @@ impl PartialEq for Decimal {
impl Decimal {
/// Attempts to convert the Decimal to an f32.
/// If an overflow occurs, returns infinity or -infinity.
pub fn as_f64(&self) -> f64 {
Comment thread
Vasyl-Trefilov marked this conversation as resolved.
Outdated
match self {
Decimal::Finite(rational) => {
// Convert Rational to f64
rational.to_f64()
}
Decimal::Zero => 0.0,
Decimal::NegZero => -0.0,
Decimal::Infinity => f64::INFINITY,
Decimal::NegInfinity => f64::NEG_INFINITY,
Decimal::Nan => f64::NAN,
}
}

pub fn into_f32(&self) -> f32 {
match self {
Decimal::Finite(value) => value.to_f32(),
Expand Down Expand Up @@ -136,9 +150,13 @@ impl Decimal {
core::matches!(self, Decimal::Nan)
}

/// Returns true if the value is zero (positive or negative).
/// Returns true if the value is zero (positive or negative)
pub fn is_zero(&self) -> bool {
core::matches!(self, Decimal::Zero | Decimal::NegZero)
Comment thread
Vasyl-Trefilov marked this conversation as resolved.
match self {
Decimal::Zero | Decimal::NegZero => true,
Decimal::Finite(rational) => rational.is_zero(), // Check if rational is zero
_ => false,
}
}

/// Returns true if the value has a positive sign.
Expand Down Expand Up @@ -314,6 +332,89 @@ impl Sub for &Decimal {
}
}

impl Mul for Decimal {
type Output = Self;

fn mul(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Decimal::Finite(a), Decimal::Finite(b)) => Decimal::from(a * b),
(Decimal::NegZero, Decimal::Zero)
| (Decimal::Zero, Decimal::NegZero) => Decimal::Zero,
(Decimal::Zero, b) | (b, Decimal::Zero) => b,
(Decimal::NegZero, b) | (b, Decimal::NegZero) => b,
(Decimal::Infinity, Decimal::NegInfinity)
| (Decimal::NegInfinity, Decimal::Infinity) => Decimal::Nan,
(Decimal::Infinity, _) | (_, Decimal::Infinity) => {
Decimal::Infinity
}
(Decimal::NegInfinity, _) | (_, Decimal::NegInfinity) => {
Decimal::NegInfinity
}
(Decimal::Nan, _) | (_, Decimal::Nan) => Decimal::Nan,
}
}
}

impl Mul for &Decimal {
type Output = Decimal;

fn mul(self, rhs: Self) -> Self::Output {
// FIXME #334: Avoid cloning, as add should be applicable for refs only
Decimal::add(self.clone(), rhs.clone())
}
}

impl Div for Decimal {
type Output = Self;

fn div(self, rhs: Self) -> Self::Output {
match (self, rhs) {
// Division by zero is invalid, so we will return NaN
(_, Decimal::Zero) | (_, Decimal::NegZero) => Decimal::Nan,

// Zero divided by anything (except zero) is zero
(Decimal::Zero, _) => Decimal::Zero,
(Decimal::NegZero, _) => Decimal::NegZero,

// Finite division
(Decimal::Finite(a), Decimal::Finite(b)) => Decimal::from(a / b),

// Infinity / Infinity = NaN
(Decimal::Infinity, Decimal::Infinity)
| (Decimal::NegInfinity, Decimal::NegInfinity)
| (Decimal::Infinity, Decimal::NegInfinity)
| (Decimal::NegInfinity, Decimal::Infinity) => Decimal::Nan,

// Finite / Infinity = 0 (with sign)
(Decimal::Finite(_), Decimal::Infinity) => Decimal::Zero,
(Decimal::Finite(_), Decimal::NegInfinity) => Decimal::NegZero,
(Decimal::NegZero, Decimal::Infinity) => Decimal::NegZero,

// Infinity / Finite = Infinity (preserve sign)
(Decimal::Infinity, Decimal::Finite(_)) => Decimal::Infinity,
(Decimal::NegInfinity, Decimal::Finite(_)) => Decimal::NegInfinity,

// Infinity / Zero = Infinity
(Decimal::Infinity, Decimal::Zero) => Decimal::Infinity,
(Decimal::NegInfinity, Decimal::Zero) => Decimal::NegInfinity,
(Decimal::Infinity, Decimal::NegZero) => Decimal::NegInfinity,
(Decimal::NegInfinity, Decimal::NegZero) => Decimal::Infinity,

// Anything with NaN results in NaN
(Decimal::Nan, _) | (_, Decimal::Nan) => Decimal::Nan,
}
}
}

impl Div for &Decimal {
type Output = Decimal;

fn div(self, rhs: Self) -> Self::Output {
// FIXME #334: Avoid cloning, as add should be applicable for refs only
Decimal::add(self.clone(), rhs.clone())
}
}

impl Display for Decimal {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Expand Down
30 changes: 28 additions & 2 deletions crates/datex-core/src/values/core_values/decimal/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ use crate::prelude::*;

use core::{
fmt::Display,
ops::{Add, Neg},
ops::{Add, Neg, Div, Mul, Sub},
result::Result,
};
use num::BigRational;
use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::{Signed, ToPrimitive, Zero};
use serde::{Deserialize, Serialize};
use serde_with::chrono::SubsecRound;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd)]
pub struct Rational {
big_rational: BigRational,
}
Expand Down Expand Up @@ -228,6 +229,31 @@ impl Add for Rational {
}
}

impl Sub for Rational {
type Output = Self;

fn sub(self, rhs: Self) -> Self::Output {
Rational::from_big_rational(self.big_rational - rhs.big_rational)
}
}


impl Mul for Rational {
type Output = Self;

fn mul(self, rhs: Self) -> Self::Output {
Rational::from_big_rational(self.big_rational * rhs.big_rational)
}
}

impl Div for Rational {
type Output = Self;

fn div(self, rhs: Self) -> Self::Output {
Rational::from_big_rational(self.big_rational / rhs.big_rational)
}
}

impl Display for Rational {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::write!(f, "{}", self.rational_to_string())
Expand Down
Loading
Loading