Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
42ccff8b12 |
|||
|
9372d5dfa8 |
|||
|
4d7a806caa |
|||
|
93d8147048 |
|||
|
50358db183 |
|||
|
7c4074113f |
|||
|
cf8d218b11 |
|||
|
ae0f2a3aaf |
|||
|
fc06e1e981 |
|||
|
8d26022403 |
5 changed files with 41 additions and 4 deletions
20
src/main.rs
20
src/main.rs
|
|
@ -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));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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(),
|
||||||
|
|
|
||||||
|
|
@ -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),
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue