Closed MarkSwanson closed 2 years ago
dimensioned is already generic with regards to value type, so it should work out of the box. You just won't be able to use the default constants, but you can make your own.
Imagine that dimensioned only supported f32
, then you could write the following to use f64
:
#[macro_use]
extern crate dimensioned;
use std::marker::PhantomData;
use dimensioned::{
derived,
si::{self, Kilogram, Meter, Second, SI},
};
const METER: Meter<f64> = Meter {
value_unsafe: 1.0f64,
_marker: PhantomData,
};
const KILOGRAM: Kilogram<f64> = Kilogram {
value_unsafe: 1.0f64,
_marker: PhantomData,
};
const SECOND: Second<f64> = Second {
value_unsafe: 1.0f64,
_marker: PhantomData,
};
derived!(
si,
SI: Meter2KilogramPerSecond = Meter * Meter * Kilogram / Second
);
const PLANCK: Meter2KilogramPerSecond<f64> = Meter2KilogramPerSecond {
value_unsafe: 6.62607015e-34f64,
_marker: PhantomData,
};
fn main() {
let x = 3.0 * PLANCK;
println!("{x}");
}
As I write this example, I see that there are a lot of things that can be improved, such as the use of const functions, and (relatively) new macro syntax.
That said, I'm surprised you would need 256 bits. Numbers smaller than planck's constant in SI units are expressible in 64 bits, and 128 bits can get you all the way down to 10^−4966.
Heck, if you used a 128 bit integer, expressing a planck length as 1, you could get all the way up to over 5000 meters.
f64 only has precision down to about 15-16 digits, and I need around 42. https://blog.demofox.org/2017/11/21/floating-point-precision/ (section: How Many Digits Can I Rely On?)
Ty for example. trying it out...
Perhaps an auto deref of the derived type - to a Quad would solve this?
79 | let x = qd!(3.0) * PLANCK; | ^ no implementation for Quad * SI<Quad, TArr<PInt<UInt<UInt<UTerm, B1>, B0>>, TArr<PInt<UInt<UTerm, B1>>, TArr<NInt<UInt<UTerm, B1>>, TArr<Z0, TArr<Z0, TArr<Z0, TArr<Z0, ATerm>>>>>>>> |
---|
= help: the trait Mul<SI<Quad, TArr<PInt<UInt<UInt<UTerm, B1>, B0>>, TArr<PInt<UInt<UTerm, B1>>, TArr<NInt<UInt<UTerm, B1>>, TArr<Z0, TArr<Z0, TArr<Z0, TArr<Z0, ATerm>>>>>>>>>
is not implemented for Quad
= help: the following other types implement trait Mul<Rhs>
:
<&Quad as Mul
This is a limitation due to the orphan rules. It should work if you put the dimensioned quantity on the left-hand side.
Perhaps closer :-)
let x = PLANCK * qd!(3.0);
expected struct SI
, found struct Quad
This works :-) let x = PLANCK.value_unsafe * y;
Yeah, that works, but you lose all of your units.
Because of orphan rules does it seem like the best way is to support qd / Quad inside of dimensioned?
Ah, now I remember (sorry, it's been a while since I've worked on this crate).
There's a fix for this in dimensioned using the oibit
feature, but that requires nightly, and is currently broken (I'll work on getting a version of it working again here).
You should be able to do:
let x = si::Unitless::new(qd!(3.0)) * PLANCK;
I'm happy to use nightly. Edit: Thank you so much for chatting with me about this. It will be great to have something like dimensioned working with qd! Once the basics are working I can contribute to quadprefixes.rs.
I have just published version 0.8.0. If you use it, nightly, and the oibit
feature, then you can do multiplication like PLANCK * qd!(3.0)
(but not the other way round, sadly).
One note: In this version, the new
function is now const, so you can declare consts like
const PLANCK: Meter2KilogramPerSecond<Complex64> =
Meter2KilogramPerSecond::new(qd!(6.62607015e-34));
Great! Will try it out shortly.
I'm thrilled to have this working! Thanks for your effort! The output of the units is really nice: dimensioned: 0.000000000000000000000000000000001987821045 m^2kgs^-1 Will play around with superscript etc like this: m²×kg×s⁻¹
All of my brief testing today works fine. Closing. Thanks again.
Hello,
I'd like to investigate dimensioned, but I need f256 support for the quantum mechanics work I'd like to do. Planck's constant is 6.62607015 × 10−34 so even f128 isn't good enough.
If you give me a few pointers I could take a stab at adding f256 support to dimensioned.
Thanks!