mirror of https://github.com/rm-dr/daisy
Compare commits
14 Commits
11b5cd877a
...
d906c474c5
Author | SHA1 | Date |
---|---|---|
Mark | d906c474c5 | |
Mark | 315be575ee | |
Mark | 31c368f7d8 | |
Mark | 8a026fc2ea | |
Mark | 8497c125ef | |
Mark | 682205f5e1 | |
Mark | e1ba2a9c1f | |
Mark | b9cfe719a6 | |
mark | 47abd9d18e | |
mark | 4353548900 | |
mark | 84ebf89d6f | |
mark | 2391606ae1 | |
mark | 38c982bb00 | |
mark | 80b1c76c59 |
|
@ -8,12 +8,6 @@ version = "1.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "az"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
|
@ -28,24 +22,14 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||
|
||||
[[package]]
|
||||
name = "daisycalc"
|
||||
version = "1.0.1"
|
||||
version = "1.1.0"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"rug",
|
||||
"num",
|
||||
"termion",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gmp-mpfr-sys"
|
||||
version = "1.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b560063e2ffa8ce9c2ef9bf487f2944a97deca5b8de0b5bcd0ae6437ef8b75f"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.12.3"
|
||||
|
@ -64,9 +48,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.140"
|
||||
version = "0.2.147"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
|
||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
|
@ -74,6 +58,82 @@ version = "2.5.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-complex",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "numtoa"
|
||||
version = "0.1.0"
|
||||
|
@ -98,17 +158,6 @@ dependencies = [
|
|||
"redox_syscall",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rug"
|
||||
version = "1.19.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "555e8b44763d034526db899c88cd56ccc4486cd38b444c8aa0e79d4e70ae5a34"
|
||||
dependencies = [
|
||||
"az",
|
||||
"gmp-mpfr-sys",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.164"
|
||||
|
@ -170,63 +219,6 @@ dependencies = [
|
|||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.42.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.4.6"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "daisycalc"
|
||||
version = "1.0.1"
|
||||
version = "1.1.0"
|
||||
edition = "2021"
|
||||
build = "buildscript/main.rs"
|
||||
license = "GPL-3.0-only"
|
||||
|
@ -28,7 +28,8 @@ cfg-if = "1.0.0"
|
|||
|
||||
[target.'cfg(target_family = "unix")'.dependencies]
|
||||
termion = "2.0.1"
|
||||
rug = "1.19.2"
|
||||
num = "0.4.1"
|
||||
#astro-float = "0.7.1"
|
||||
|
||||
[build-dependencies]
|
||||
toml = "0.7.4"
|
4
TODO.md
4
TODO.md
|
@ -26,14 +26,13 @@
|
|||
- Optional history file
|
||||
- Compile to WASM, publish a webapp
|
||||
- evaluate straight from command line
|
||||
- Package for debian
|
||||
- Package for debian, nix
|
||||
|
||||
|
||||
## Internals
|
||||
- Non-recursive treeify
|
||||
- Faster factorial function. Maybe use gamma instead?
|
||||
- Arbitrary precision float (rug doesn't offer arbitrary exponents)
|
||||
- Remove rug dependency (too big, incompatible)
|
||||
|
||||
## Math Features
|
||||
- Mean, Median, Min
|
||||
|
@ -53,7 +52,6 @@
|
|||
- Numbered expressions, history recall
|
||||
- Enable/disable unit sets (defaults?)
|
||||
- Consistent unit ordering
|
||||
- Command to list substitutions
|
||||
|
||||
## Units
|
||||
- long prefixes (megatonne, etc)
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
let
|
||||
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/f155f0cf4ea43c4e3c8918d2d327d44777b6cad4.tar.gz") {};
|
||||
in pkgs.mkShell {
|
||||
buildInputs = with pkgs; [
|
||||
cargo
|
||||
rustc
|
||||
rustfmt
|
||||
m4
|
||||
];
|
||||
}
|
||||
|
|
@ -61,7 +61,7 @@ impl Config {
|
|||
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone)]
|
||||
//#[derive(Clone)]
|
||||
pub struct Context {
|
||||
pub config: Config,
|
||||
|
||||
|
@ -76,12 +76,12 @@ pub struct Context {
|
|||
// General functions
|
||||
impl Context {
|
||||
pub fn new() -> Context {
|
||||
Context{
|
||||
Context {
|
||||
config: Config::new(),
|
||||
history: Vec::new(),
|
||||
variables: HashMap::new(),
|
||||
functions: HashMap::new(),
|
||||
shadow: HashMap::new(),
|
||||
shadow: HashMap::new()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ pub fn evaluate(
|
|||
|
||||
context.get_variable(&s)
|
||||
},
|
||||
Expression::Operator(_, Operator::Function(_), _) => { Some(eval_function(g)?) },
|
||||
Expression::Operator(_, Operator::Function(_), _) => { eval_function(g)? },
|
||||
Expression::Operator(_, _, _) => { eval_operator(context, g)? },
|
||||
};
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ fn to_radians(q: Quantity) -> Result<Quantity, ()> {
|
|||
|
||||
|
||||
|
||||
pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyError)> {
|
||||
pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation, DaisyError)> {
|
||||
|
||||
let Expression::Operator(loc, Operator::Function(f), args) = g else {unreachable!()};
|
||||
|
||||
|
@ -41,113 +41,112 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyE
|
|||
))
|
||||
};
|
||||
|
||||
let Expression::Quantity(l, q) = a else {panic!()};
|
||||
|
||||
let Expression::Quantity(l, q) = a else { return Ok(None); };
|
||||
|
||||
match f {
|
||||
Function::NoUnit => { return Ok(Expression::Quantity(*loc + *l, q.without_unit())); }
|
||||
Function::ToBase => { return Ok(Expression::Quantity(*loc + *l, q.convert_to_base())); }
|
||||
Function::NoUnit => { return Ok(Some(Expression::Quantity(*loc + *l, q.without_unit()))); }
|
||||
Function::ToBase => { return Ok(Some(Expression::Quantity(*loc + *l, q.convert_to_base()))); }
|
||||
|
||||
|
||||
|
||||
Function::Abs => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.abs()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.abs())));
|
||||
},
|
||||
Function::Floor => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.floor()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.floor())));
|
||||
},
|
||||
Function::Ceil => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.ceil()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.ceil())));
|
||||
},
|
||||
Function::Round => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.round()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.round())));
|
||||
},
|
||||
Function::NaturalLog => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.ln()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.ln())));
|
||||
},
|
||||
Function::TenLog => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.log10()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.log10())));
|
||||
},
|
||||
|
||||
|
||||
|
||||
Function::Sin => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.sin()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.sin())));
|
||||
},
|
||||
Function::Cos => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.cos()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.cos())));
|
||||
},
|
||||
Function::Tan => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.tan()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.tan())));
|
||||
},
|
||||
Function::Csc => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.csc()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.csc())));
|
||||
},
|
||||
Function::Sec => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.sec()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.sec())));
|
||||
},
|
||||
Function::Cot => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.cot()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.cot())));
|
||||
},
|
||||
Function::Sinh => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.sinh()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.sinh())));
|
||||
},
|
||||
Function::Cosh => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.cosh()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.cosh())));
|
||||
},
|
||||
Function::Tanh => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.tanh()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.tanh())));
|
||||
},
|
||||
Function::Csch => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.csch()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.csch())));
|
||||
},
|
||||
Function::Sech => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.sech()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.sech())));
|
||||
},
|
||||
Function::Coth => {
|
||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||
return Ok(Expression::Quantity(*loc + *l, q.coth()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.coth())));
|
||||
},
|
||||
Function::Asin => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.asin()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.asin())));
|
||||
},
|
||||
Function::Acos => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.acos()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.acos())));
|
||||
},
|
||||
Function::Atan => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.atan()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.atan())));
|
||||
},
|
||||
Function::Asinh => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.asinh()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.asinh())));
|
||||
},
|
||||
Function::Acosh => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.acosh()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.acosh())));
|
||||
},
|
||||
Function::Atanh => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
return Ok(Expression::Quantity(*loc + *l, q.atanh()));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, q.atanh())));
|
||||
},
|
||||
|
||||
|
||||
|
@ -160,7 +159,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyE
|
|||
let mut r = q.without_unit();
|
||||
r += Quantity::new_rational(-273.15f64).unwrap();
|
||||
|
||||
return Ok(Expression::Quantity(*loc + *l, r));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, r)));
|
||||
},
|
||||
Function::ToFahrenheit => {
|
||||
let mut k = Quantity::new_rational(1f64).unwrap();
|
||||
|
@ -172,7 +171,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyE
|
|||
r += Quantity::new_rational(-459.67).unwrap();
|
||||
|
||||
|
||||
return Ok(Expression::Quantity(*loc + *l, r));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, r)));
|
||||
},
|
||||
Function::FromCelsius => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
|
@ -181,7 +180,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyE
|
|||
r += q.clone();
|
||||
r.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
|
||||
|
||||
return Ok(Expression::Quantity(*loc + *l, r));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, r)));
|
||||
},
|
||||
Function::FromFahrenheit => {
|
||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||
|
@ -191,7 +190,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyE
|
|||
r *= Quantity::new_rational_from_frac(5i64, 9i64).unwrap();
|
||||
r.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
|
||||
|
||||
return Ok(Expression::Quantity(*loc + *l, r));
|
||||
return Ok(Some(Expression::Quantity(*loc + *l, r)));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -164,7 +164,8 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
|||
|
||||
if let Expression::Quantity(la, a) = a {
|
||||
if let Expression::Quantity(lb, b) = b {
|
||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() * b.clone())));
|
||||
let o = a.clone() * b.clone();
|
||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, o)));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ use std::ops::{
|
|||
|
||||
use std::cmp::Ordering;
|
||||
use super::ScalarBase;
|
||||
use super::dec_to_sci;
|
||||
|
||||
|
||||
macro_rules! foward {
|
||||
|
@ -25,16 +26,39 @@ pub struct F64Base where {
|
|||
}
|
||||
|
||||
impl ToString for F64Base {
|
||||
fn to_string(&self) -> String { self.val.to_string() }
|
||||
fn to_string(&self) -> String {
|
||||
// Remove negative sign from string
|
||||
let mut s = self.val.to_string();
|
||||
|
||||
let neg = s.starts_with("-");
|
||||
if neg { s = String::from(&s[1..]); }
|
||||
|
||||
// Power of ten
|
||||
let mut p: i64 = {
|
||||
if let Some(x) = s.find(".") {
|
||||
x as i64
|
||||
} else {
|
||||
s.len() as i64
|
||||
}
|
||||
};
|
||||
p -= 1;
|
||||
|
||||
// We no longer need a decimal point in our string.
|
||||
// also, trim off leading zeros and adjust power.
|
||||
let mut s: &str = &s.replace(".", "");
|
||||
s = &s[0..];
|
||||
s = s.trim_end_matches('0');
|
||||
while s.starts_with('0') {
|
||||
s = &s[1..];
|
||||
p -= 1;
|
||||
}
|
||||
|
||||
return dec_to_sci(neg, s.to_string(), p);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl ScalarBase for F64Base {
|
||||
|
||||
fn from_f64(f: f64) -> Option<F64Base> {
|
||||
return Some(F64Base{ val: f });
|
||||
}
|
||||
|
||||
fn from_string(s: &str) -> Option<F64Base> {
|
||||
let v = s.parse::<f64>();
|
||||
let v = match v {
|
||||
|
@ -51,6 +75,7 @@ impl ScalarBase for F64Base {
|
|||
fn is_one(&self) -> bool {self.val == 1f64}
|
||||
fn is_negative(&self) -> bool { self.val.is_sign_negative() }
|
||||
fn is_positive(&self) -> bool { self.val.is_sign_positive() }
|
||||
fn is_int(&self) -> bool { self.val.floor() == self.val }
|
||||
|
||||
foward!(abs);
|
||||
foward!(floor);
|
||||
|
@ -60,9 +85,11 @@ impl ScalarBase for F64Base {
|
|||
foward!(sin);
|
||||
foward!(cos);
|
||||
foward!(tan);
|
||||
foward!(csc);
|
||||
foward!(sec);
|
||||
foward!(cot);
|
||||
|
||||
fn csc(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.sin() }) }
|
||||
fn sec(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.cos() }) }
|
||||
fn cot(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.tan() }) }
|
||||
|
||||
foward!(asin);
|
||||
foward!(acos);
|
||||
foward!(atan);
|
||||
|
@ -70,9 +97,11 @@ impl ScalarBase for F64Base {
|
|||
foward!(sinh);
|
||||
foward!(cosh);
|
||||
foward!(tanh);
|
||||
foward!(csch);
|
||||
foward!(sech);
|
||||
foward!(coth);
|
||||
|
||||
fn csch(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.sinh() }) }
|
||||
fn sech(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.cosh() }) }
|
||||
fn coth(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.tanh() }) }
|
||||
|
||||
foward!(asinh);
|
||||
foward!(acosh);
|
||||
foward!(atanh);
|
||||
|
@ -161,11 +190,11 @@ impl Rem<F64Base> for F64Base {
|
|||
|
||||
fn rem(self, modulus: F64Base) -> Self::Output {
|
||||
if {
|
||||
(!self.fract().unwrap().is_zero()) ||
|
||||
(!modulus.fract().unwrap().is_zero())
|
||||
(!self.is_int()) ||
|
||||
(!modulus.is_int())
|
||||
} { panic!() }
|
||||
|
||||
F64Base{val : self.val.fract() % modulus.val.fract()}
|
||||
F64Base{val : self.val.round() % modulus.val.round()}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use rug::Float;
|
||||
use rug::Assign;
|
||||
use rug::ops::AssignRound;
|
||||
use rug::ops::Pow;
|
||||
use bigdecimal::BigDecimal;
|
||||
use bigdecimal::Zero;
|
||||
use bigdecimal::RoundingMode;
|
||||
use std::str::FromStr;
|
||||
|
||||
use std::ops::{
|
||||
Add, Sub, Mul, Div,
|
||||
|
@ -14,158 +14,151 @@ use std::ops::{
|
|||
use std::cmp::Ordering;
|
||||
|
||||
use super::ScalarBase;
|
||||
use super::PRINT_LEN;
|
||||
use super::FLOAT_PRECISION;
|
||||
use super::dec_to_sci;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone)]
|
||||
pub struct FloatBase where {
|
||||
pub val: Float
|
||||
pub val: BigDecimal
|
||||
}
|
||||
|
||||
impl FloatBase {
|
||||
pub fn from<T>(a: T) -> Option<FloatBase> where
|
||||
Float: Assign<T> + AssignRound<T>
|
||||
{
|
||||
let v = Float::with_val(FLOAT_PRECISION, a);
|
||||
return Some(FloatBase{ val: v });
|
||||
pub fn new(s: &str) -> FloatBase {
|
||||
return FloatBase {
|
||||
val: s.parse().unwrap()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl ToString for FloatBase {
|
||||
fn to_string(&self) -> String {
|
||||
let (sign, mut string, exp) = self.val.to_sign_string_exp(10, Some(PRINT_LEN));
|
||||
|
||||
// zero, nan, or inf.
|
||||
let sign = if sign {"-"} else {""};
|
||||
if exp.is_none() { return format!("{sign}{string}"); }
|
||||
let exp = exp.unwrap();
|
||||
|
||||
// Remove trailing zeros.
|
||||
// At this point, string is guaranteed to be nonzero.
|
||||
while string.chars().last().unwrap() == '0' {
|
||||
string.remove(string.len() - 1);
|
||||
if self.val.is_nan() {
|
||||
return "NaN".to_string();
|
||||
} else if self.val.is_inf_neg() {
|
||||
return "-Inf".to_string();
|
||||
} else if self.val.is_inf_pos() {
|
||||
return "+Inf".to_string();
|
||||
}
|
||||
|
||||
let exp_u: usize;
|
||||
|
||||
if exp < 0 {
|
||||
exp_u = (-exp).try_into().unwrap()
|
||||
} else {
|
||||
exp_u = exp.try_into().unwrap()
|
||||
}
|
||||
// Already in scientific notation,we just need to trim significant digits.
|
||||
let mut _a = self.val.round(32, astro_float::RoundingMode::Up).to_string();
|
||||
let mut _b = _a.split('e');
|
||||
|
||||
if exp_u >= PRINT_LEN {
|
||||
// Exponential notation
|
||||
let pre = &string[0..1];
|
||||
let post = &string[1..];
|
||||
let mut s = String::from(_b.next().unwrap()); // Decimal
|
||||
let p: i64 = _b.next().unwrap().parse().unwrap(); // Exponent
|
||||
|
||||
format!(
|
||||
"{pre}{}{post}e{}",
|
||||
if post.len() != 0 {"."} else {""},
|
||||
//if exp > 0 {"+"} else {""},
|
||||
exp - 1
|
||||
)
|
||||
} else {
|
||||
if exp <= 0 { // Decimal, needs `0.` and leading zeros
|
||||
format!(
|
||||
"{sign}0.{}{string}",
|
||||
"0".repeat(exp_u)
|
||||
)
|
||||
} else if exp_u < string.len() { // Decimal, needs only `.`
|
||||
format!(
|
||||
"{sign}{}.{}",
|
||||
&string[0..exp_u],
|
||||
&string[exp_u..]
|
||||
)
|
||||
} else { // Integer, needs trailing zeros
|
||||
format!(
|
||||
"{sign}{string}{}",
|
||||
"0".repeat(exp_u - string.len())
|
||||
)
|
||||
}
|
||||
}
|
||||
// Remove negative sign from string
|
||||
let neg = s.starts_with("-");
|
||||
if neg { s = String::from(&s[1..]); }
|
||||
|
||||
// We no longer need a decimal point in our string.
|
||||
// also, trim off leading zeros and adjust power.
|
||||
let mut s: &str = &s.replace(".", "");
|
||||
s = &s[0..];
|
||||
s = s.trim_end_matches('0');
|
||||
s = s.trim_start_matches('0');
|
||||
|
||||
return dec_to_sci(neg, s.to_string(), p);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
macro_rules! foward {
|
||||
( $x:ident ) => {
|
||||
fn $x(&self) -> Option<FloatBase> {
|
||||
Some(FloatBase{ val: self.val.clone().$x()})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ScalarBase for FloatBase {
|
||||
|
||||
fn from_f64(f: f64) -> Option<FloatBase> {
|
||||
let v = Float::with_val(FLOAT_PRECISION, f);
|
||||
return Some(FloatBase{ val: v });
|
||||
}
|
||||
|
||||
fn from_string(s: &str) -> Option<FloatBase> {
|
||||
let v = Float::parse(s);
|
||||
let v = BigDecimal::from_str(s);
|
||||
let v = match v {
|
||||
Ok(x) => x,
|
||||
Err(_) => return None
|
||||
};
|
||||
|
||||
return Some(
|
||||
FloatBase{ val:
|
||||
Float::with_val(FLOAT_PRECISION, v)
|
||||
}
|
||||
);
|
||||
return Some(FloatBase{ val: v });
|
||||
}
|
||||
|
||||
foward!(fract);
|
||||
//foward!(fract);
|
||||
|
||||
fn is_zero(&self) -> bool {self.val.is_zero()}
|
||||
fn is_one(&self) -> bool {self.val == Float::with_val(FLOAT_PRECISION, 1)}
|
||||
fn is_negative(&self) -> bool { self.val.is_sign_negative() }
|
||||
fn is_positive(&self) -> bool { self.val.is_sign_positive() }
|
||||
fn is_one(&self) -> bool {self.val == BigDecimal::from_str("1").unwrap()}
|
||||
fn is_negative(&self) -> bool { self.val.sign() == num::bigint::Sign::Minus }
|
||||
fn is_positive(&self) -> bool { self.val.sign() == num::bigint::Sign::Plus }
|
||||
|
||||
fn is_int(&self) -> bool {
|
||||
self.fract() == FloatBase::from_f64(0f64)
|
||||
fn is_int(&self) -> bool { self.val.is_integer() }
|
||||
|
||||
fn abs(&self) -> Option<FloatBase> { Some(FloatBase{ val: self.val.abs() }) }
|
||||
fn round(&self) -> Option<FloatBase> { Some(FloatBase{ val: self.val.round(0) }) }
|
||||
|
||||
fn floor(&self) -> Option<FloatBase> {
|
||||
let (_, scale) = self.val.as_bigint_and_exponent();
|
||||
Some(FloatBase{ val: self.val.with_scale_round(scale, RoundingMode::Down) })
|
||||
}
|
||||
|
||||
foward!(abs);
|
||||
foward!(floor);
|
||||
foward!(ceil);
|
||||
foward!(round);
|
||||
|
||||
foward!(sin);
|
||||
foward!(cos);
|
||||
foward!(tan);
|
||||
foward!(csc);
|
||||
foward!(sec);
|
||||
foward!(cot);
|
||||
foward!(asin);
|
||||
foward!(acos);
|
||||
foward!(atan);
|
||||
|
||||
foward!(sinh);
|
||||
foward!(cosh);
|
||||
foward!(tanh);
|
||||
foward!(csch);
|
||||
foward!(sech);
|
||||
foward!(coth);
|
||||
foward!(asinh);
|
||||
foward!(acosh);
|
||||
foward!(atanh);
|
||||
|
||||
foward!(exp);
|
||||
foward!(ln);
|
||||
foward!(log10);
|
||||
foward!(log2);
|
||||
|
||||
fn log(&self, base: FloatBase) -> Option<FloatBase> {
|
||||
Some(FloatBase{ val: self.val.clone().log10() } / base.log10().unwrap())
|
||||
fn ceil(&self) -> Option<FloatBase> {
|
||||
let (_, scale) = self.val.as_bigint_and_exponent();
|
||||
Some(FloatBase{ val: self.val.with_scale_round(scale, RoundingMode::Up) })
|
||||
}
|
||||
|
||||
fn pow(&self, base: FloatBase) -> Option<FloatBase> {
|
||||
Some(FloatBase{ val: self.val.clone().pow(base.val)})
|
||||
fn fract(&self) -> Option<FloatBase> { Some(self.clone() - self.floor().unwrap()) }
|
||||
|
||||
|
||||
fn sin(&self) -> Option<FloatBase> {
|
||||
let c0: BigDecimal = "1.276278962".parse().unwrap();
|
||||
let c1: BigDecimal = "-.285261569".parse().unwrap();
|
||||
let c2: BigDecimal = "0.009118016".parse().unwrap();
|
||||
let c3: BigDecimal = "-.000136587".parse().unwrap();
|
||||
let c4: BigDecimal = "0.000001185".parse().unwrap();
|
||||
let c5: BigDecimal = "-.000000007".parse().unwrap();
|
||||
|
||||
|
||||
// z should be between -0.25 to 0.25 (percent of a full circle)
|
||||
let z: BigDecimal = self.val.clone() / 360f64;
|
||||
let w = BigDecimal::from(4) * z;
|
||||
let x: BigDecimal = 2 * w.clone() * w.clone() - 1;
|
||||
|
||||
let p = (
|
||||
c0 * 1 +
|
||||
c1 * x.clone() +
|
||||
c2 * (2 * x.clone()*x.clone() - 1) +
|
||||
c3 * (4 * x.clone()*x.clone()*x.clone() - 3 * x.clone()) +
|
||||
c4 * (8 * x.clone()*x.clone()*x.clone()*x.clone() - 8 * x.clone()*x.clone() + 1) +
|
||||
c5 * (16 * x.clone()*x.clone()*x.clone()*x.clone()*x.clone() - 20 * x.clone()*x.clone()*x.clone() + 5 * x.clone())
|
||||
) * w;
|
||||
|
||||
return Some(FloatBase{ val: p })
|
||||
}
|
||||
fn cos(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn tan(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn csc(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn sec(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn cot(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn asin(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn acos(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn atan(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn sinh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn cosh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn tanh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn csch(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn sech(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn coth(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn asinh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn acosh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn atanh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn exp(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn ln(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn log10(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
fn log2(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
|
||||
|
||||
|
||||
fn log(&self, _base: FloatBase) -> Option<FloatBase> {
|
||||
Some(FloatBase{ val: "1".parse().unwrap() })
|
||||
}
|
||||
|
||||
fn pow(&self, _base: FloatBase) -> Option<FloatBase> {
|
||||
Some(FloatBase{ val: "1".parse().unwrap() })
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -223,7 +216,7 @@ impl Div for FloatBase {
|
|||
|
||||
impl DivAssign for FloatBase where {
|
||||
fn div_assign(&mut self, other: Self) {
|
||||
self.val /= other.val;
|
||||
self.val = self.val.clone() / other.val;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -240,11 +233,11 @@ impl Rem<FloatBase> for FloatBase {
|
|||
|
||||
fn rem(self, modulus: FloatBase) -> Self::Output {
|
||||
if {
|
||||
(!self.fract().unwrap().is_zero()) ||
|
||||
(!modulus.fract().unwrap().is_zero())
|
||||
(!self.is_int()) ||
|
||||
(!modulus.is_int())
|
||||
} { panic!() }
|
||||
|
||||
FloatBase{val : self.val.trunc() % modulus.val.trunc()}
|
||||
FloatBase{val : self.val.round(0) % modulus.val.round(0)}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,92 @@
|
|||
const FLOAT_PRECISION: u32 = 1024;
|
||||
const PRINT_LEN: usize = 5; // How many significant digits we will show in output
|
||||
//const FLOAT_PRECISION: u32 = 1024;
|
||||
const SHOW_SIG: usize = 5; // How many significant digits we will show in output
|
||||
const MAX_LEN: usize = 5; // If a scientific exponent is >= this value, do not use scientific notation.
|
||||
|
||||
pub(in self) mod rationalbase;
|
||||
pub(in self) mod floatbase;
|
||||
//mod f64base;
|
||||
|
||||
|
||||
// Pick a float implementation.
|
||||
// floatbase is high-precision, f64base is for testing.
|
||||
|
||||
//pub(in self) mod floatbase;
|
||||
//pub use floatbase::FloatBase;
|
||||
|
||||
pub(in self) mod f64base;
|
||||
pub use f64base::F64Base as FloatBase;
|
||||
|
||||
|
||||
|
||||
mod scalar;
|
||||
pub use self::scalar::Scalar;
|
||||
pub use self::scalar::ScalarBase;
|
||||
|
||||
|
||||
|
||||
// Convert a string to scientific notation,
|
||||
// with parameters SHOW_SIG and MAX_LEN.
|
||||
//
|
||||
// input:
|
||||
// neg: true if negative
|
||||
// s: decimal portion. Must contain only digits and a single decimal point.
|
||||
// zeros must be stripped from both ends.
|
||||
// p: power of ten to multiply by.
|
||||
//
|
||||
// So, (-1)^(neg) + (s * 10^p) should give us our number.
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(in self) fn dec_to_sci(neg: bool, mut s: String, p: i64) -> String {
|
||||
// Pick significant digits and round
|
||||
if s.len() > SHOW_SIG {
|
||||
let round;
|
||||
if s.len() != SHOW_SIG + 1 {
|
||||
round = s[SHOW_SIG..SHOW_SIG+1].parse().unwrap();
|
||||
} else { round = 0; }
|
||||
|
||||
s = String::from(&s[0..SHOW_SIG]);
|
||||
|
||||
if round >= 5 {
|
||||
let new = s[s.len()-1..s.len()].parse::<u8>().unwrap() + 1u8;
|
||||
if new != 10 {
|
||||
s = format!("{}{new}", &s[0..s.len()-1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s = format!("{s}{}", "0".repeat(SHOW_SIG - s.len()));
|
||||
// at this point, s is guaranteed to have exactly SHOW_SIG digits.
|
||||
|
||||
let neg = if neg {"-"} else {""};
|
||||
|
||||
if (p.abs() as usize) < MAX_LEN {
|
||||
// Print whole decimal
|
||||
|
||||
if p >= 0 {
|
||||
let q = p as usize;
|
||||
|
||||
let first = &s[0..q+1];
|
||||
let mut rest = &s[q+1..];
|
||||
rest = rest.trim_end_matches('0');
|
||||
if rest == "" {
|
||||
return format!("{neg}{first}");
|
||||
} else {
|
||||
return format!("{neg}{first}.{rest}");
|
||||
}
|
||||
} else {
|
||||
let q = p.abs() as usize;
|
||||
let t = format!("0.{}{s}", "0".repeat(q-1));
|
||||
return format!("{neg}{}", t.trim_end_matches('0'));
|
||||
}
|
||||
|
||||
} else {
|
||||
// Print full scientific notation
|
||||
|
||||
let first = &s[0..1];
|
||||
let mut rest = &s[1..];
|
||||
rest = rest.trim_end_matches('0');
|
||||
if rest == "" {
|
||||
return format!("{neg}{first}e{p}");
|
||||
} else {
|
||||
return format!("{neg}{first}.{rest}e{p}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ use std::ops::{
|
|||
};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use super::floatbase::FloatBase as FloatBase;
|
||||
use super::FloatBase as FloatBase;
|
||||
use super::rationalbase::RationalBase;
|
||||
|
||||
|
||||
|
@ -21,7 +21,6 @@ pub trait ScalarBase:
|
|||
PartialEq + PartialOrd
|
||||
{
|
||||
// Creation
|
||||
fn from_f64(f: f64) -> Option<Self>;
|
||||
fn from_string(s: &str) -> Option<Self>;
|
||||
|
||||
// Utility
|
||||
|
@ -87,8 +86,8 @@ fn to_float(r: Scalar) -> Scalar {
|
|||
match &r {
|
||||
Scalar::Float {..} => r,
|
||||
Scalar::Rational {v} => wrap_float!(
|
||||
FloatBase::from(v.val.numer()).unwrap() /
|
||||
FloatBase::from(v.val.denom()).unwrap()
|
||||
FloatBase::from_string(&v.val.numer().to_string()).unwrap() /
|
||||
FloatBase::from_string(&v.val.denom().to_string()).unwrap()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -105,13 +104,13 @@ impl ToString for Scalar {
|
|||
// Creation methods
|
||||
impl Scalar {
|
||||
pub fn new_float(f: f64) -> Option<Self> {
|
||||
let v = FloatBase::from_f64(f);
|
||||
let v = FloatBase::from_string(&f.to_string());
|
||||
if v.is_none() { return None; }
|
||||
return Some(wrap_float!(v.unwrap()));
|
||||
}
|
||||
|
||||
pub fn new_rational(f: f64) -> Option<Self> {
|
||||
let r = RationalBase::from_f64(f);
|
||||
let r = RationalBase::from_string(&f.to_string());
|
||||
if r.is_none() { return None; }
|
||||
return Some(wrap_rational!(r.unwrap()));
|
||||
}
|
||||
|
@ -185,7 +184,7 @@ impl Scalar {
|
|||
|
||||
pub fn is_nan(&self) -> bool {
|
||||
match self {
|
||||
Scalar::Float {v} => {v.val.is_nan()},
|
||||
Scalar::Float{ v } => {v.val.is_nan()},
|
||||
Scalar::Rational {..} => {false}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -275,7 +275,6 @@ impl Unit {
|
|||
let mut q = Quantity::new_rational(1f64).unwrap();
|
||||
q.set_unit(b);
|
||||
return Some(q);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -131,7 +131,7 @@ fn operators() {
|
|||
|
||||
good_expr("125", "5^(+3)");
|
||||
good_expr("125", "+5^3");
|
||||
good_expr("0.2148", "3 ^ (-1.4)");
|
||||
good_expr("0.21479", "3 ^ (-1.4)");
|
||||
|
||||
// Should parse as ((2^3)^4)^5
|
||||
good_expr("1.1529e18", "2^3^4^5");
|
||||
|
|
Loading…
Reference in New Issue