Compare commits

...
Author SHA1 Message Date
42ccff8b12
fix: maximum significant digits
oops :3
2026-07-16 11:39:22 -07:00
9372d5dfa8
feat: support unicode mult & div 2026-07-16 11:36:21 -07:00
4d7a806caa
feat: support sqrt
other roots may prove to be quite a challenge,
since I would need to parse the syntax `root(base, value)`
2026-07-16 11:33:03 -07:00
93d8147048
feat: set a max for displayed significant digits 2026-07-16 11:29:51 -07:00
50358db183
refactor: significant digits calculation out of fn main 2026-07-16 11:25:48 -07:00
7c4074113f
feat: shorthand to concatonate arguments
in the shell this allows for `tylc 1 + 1`,
making it equivelent to `tylc "1+1"`

this makes implementing other arguments difficult/impossible, though
2026-07-16 11:01:58 -07:00
cf8d218b11
refactor: use compiler builtin constant for pi
as per clippy lint
2026-07-16 10:55:23 -07:00
ae0f2a3aaf
feat: print a reasonable number of significant digits 2026-07-16 10:51:44 -07:00
fc06e1e981
feat: add constant pi
copy and pasted from ALEKS calculator,
as I gave up trying to find what the best approximation to use would be.
2026-07-16 00:36:46 -07:00
8d26022403
tests: implicit_mult 2026-07-16 00:30:53 -07:00
5 changed files with 41 additions and 4 deletions

View file

@ -12,8 +12,22 @@ pub fn parse_and_calc(s: &str) -> Float {
nodes::Nodes::from_str(s).unwrap().evaluate() nodes::Nodes::from_str(s).unwrap().evaluate()
} }
pub fn get_significant_digits(n: &Float) -> usize {
let raw_string = format!("{n}");
let mut significant_digits = raw_string.len() - 1;
for (i, _) in raw_string.char_indices() {
if !raw_string[i..raw_string.len()].contains(|c| matches!(c, '1'..'9')) {
significant_digits = i;
break;
}
}
significant_digits
}
fn main() { fn main() {
let input = match env::args().nth(1) { let input = match env::args().skip(1).reduce(|x, a| a + &x) {
Some(s) => s, Some(s) => s,
None => { None => {
let mut buf = String::new(); let mut buf = String::new();
@ -22,5 +36,7 @@ fn main() {
} }
}; };
println!("{:.3}", parse_and_calc(&input)); let result = parse_and_calc(&input);
println!("{result:.0$}", get_significant_digits(&result).clamp(0, 6));
} }

View file

@ -40,6 +40,8 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) {
{ {
match &mut nodes[start..end][index + 1] { match &mut nodes[start..end][index + 1] {
Node::Number(n) => match variant { Node::Number(n) => match variant {
Function::Sqrt => n.sqrt_mut(),
Function::Sine => n.sin_mut(), Function::Sine => n.sin_mut(),
Function::ArcSine => n.asin_mut(), Function::ArcSine => n.asin_mut(),
Function::Cosine => n.cos_mut(), Function::Cosine => n.cos_mut(),

View file

@ -1,3 +1,4 @@
use core::f64;
use std::str::FromStr; use std::str::FromStr;
use rug::{Float, float::ParseFloatError, ops::CompleteRound}; use rug::{Float, float::ParseFloatError, ops::CompleteRound};
@ -9,6 +10,7 @@ impl Node {
Ok(match s { Ok(match s {
"c" => Self::Number(Float::with_val(PREC, 299_792_458)), "c" => Self::Number(Float::with_val(PREC, 299_792_458)),
"A" => Self::Number(Float::with_val(PREC, 6.02214076e23)), "A" => Self::Number(Float::with_val(PREC, 6.02214076e23)),
"pi" => Self::Number(Float::with_val(PREC, f64::consts::PI)),
_ => Self::Function(s.parse()?), _ => Self::Function(s.parse()?),
}) })
} }
@ -19,8 +21,8 @@ impl TryFrom<char> for Operator {
match value { match value {
'+' => Ok(Self::Plus), '+' => Ok(Self::Plus),
'-' => Ok(Self::Minus), '-' => Ok(Self::Minus),
'*' => Ok(Self::Mult), '*' | '×' => Ok(Self::Mult),
'/' => Ok(Self::Div), '/' | '÷' => Ok(Self::Div),
'^' => Ok(Self::Exp), '^' => Ok(Self::Exp),
_ => Err(value), _ => Err(value),
} }
@ -30,6 +32,8 @@ impl FromStr for Function {
type Err = String; type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
match s { match s {
"sqrt" => Ok(Self::Sqrt),
"sin" => Ok(Self::Sine), "sin" => Ok(Self::Sine),
"asin" => Ok(Self::ArcSine), "asin" => Ok(Self::ArcSine),
"cos" => Ok(Self::Cosine), "cos" => Ok(Self::Cosine),

View file

@ -40,6 +40,8 @@ enum Operator {
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
enum Function { enum Function {
Sqrt,
Sine, Sine,
ArcSine, ArcSine,
Cosine, Cosine,

View file

@ -1,9 +1,18 @@
use core::f64;
use std::str::FromStr; use std::str::FromStr;
use rug::Float; use rug::Float;
use crate::nodes::{Function, Node, Nodes, Operator, PREC}; use crate::nodes::{Function, Node, Nodes, Operator, PREC};
#[test]
fn implicit_mult() {
assert_eq!(
Nodes::from_str("10 sin(10)").unwrap().evaluate().to_f64(),
-5.440211108893698
);
}
#[test] #[test]
fn constants() { fn constants() {
assert_eq!( assert_eq!(
@ -14,6 +23,10 @@ fn constants() {
Nodes::from_str("A").unwrap().evaluate().to_f64(), Nodes::from_str("A").unwrap().evaluate().to_f64(),
6.02214076e23 6.02214076e23
); );
assert_eq!(
Nodes::from_str("pi").unwrap().evaluate().to_f64(),
f64::consts::PI
);
} }
#[test] #[test]