Compare commits
5 changed files with 4 additions and 41 deletions
20
src/main.rs
20
src/main.rs
|
|
@ -12,22 +12,8 @@ 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().skip(1).reduce(|x, a| a + &x) {
|
let input = match env::args().nth(1) {
|
||||||
Some(s) => s,
|
Some(s) => s,
|
||||||
None => {
|
None => {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
@ -36,7 +22,5 @@ fn main() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = parse_and_calc(&input);
|
println!("{:.3}", parse_and_calc(&input));
|
||||||
|
|
||||||
println!("{result:.0$}", get_significant_digits(&result).clamp(0, 6));
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,8 +40,6 @@ 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(),
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
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};
|
||||||
|
|
@ -10,7 +9,6 @@ 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()?),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -21,8 +19,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),
|
||||||
}
|
}
|
||||||
|
|
@ -32,8 +30,6 @@ 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),
|
||||||
|
|
|
||||||
|
|
@ -40,8 +40,6 @@ enum Operator {
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
enum Function {
|
enum Function {
|
||||||
Sqrt,
|
|
||||||
|
|
||||||
Sine,
|
Sine,
|
||||||
ArcSine,
|
ArcSine,
|
||||||
Cosine,
|
Cosine,
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,9 @@
|
||||||
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!(
|
||||||
|
|
@ -23,10 +14,6 @@ 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]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue