Comments (15)
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.
from dimensioned.
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.
from dimensioned.
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...
from dimensioned.
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>
<&Quad as Mul>
<Quad as Mul<&Quad>>
#[test]
pub fn d1() {
let METER: Meter<Quad> = Meter {
value_unsafe: qd!(1.0),
_marker: PhantomData,
};
let KILOGRAM: Kilogram<Quad> = Kilogram {
value_unsafe: qd!(1.0),
_marker: PhantomData,
};
let SECOND: Second<Quad> = Second {
value_unsafe: qd!(1.0),
_marker: PhantomData,
};
derived!(
si,
SI: Meter2KilogramPerSecond = Meter * Meter * Kilogram / Second
);
let PLANCK: Meter2KilogramPerSecond<Quad> = Meter2KilogramPerSecond {
value_unsafe: qd!(6.62607015e-34),
_marker: PhantomData,
};
let x = qd!(3.0) * PLANCK;
println!("{x}");
}
from dimensioned.
This is a limitation due to the orphan rules. It should work if you put the dimensioned quantity on the left-hand side.
from dimensioned.
Perhaps closer :-)
let x = PLANCK * qd!(3.0);
expected struct SI
, found struct Quad
from dimensioned.
This works :-)
let x = PLANCK.value_unsafe * y;
from dimensioned.
Yeah, that works, but you lose all of your units.
from dimensioned.
Because of orphan rules does it seem like the best way is to support qd / Quad inside of dimensioned?
from 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;
from dimensioned.
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.
from dimensioned.
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));
from dimensioned.
Great! Will try it out shortly.
from dimensioned.
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⁻¹
from dimensioned.
All of my brief testing today works fine.
Closing. Thanks again.
from dimensioned.
Related Issues (20)
- Add support for absolute values HOT 2
- Implement various functions on primitives.
- Use #[rustc_on_unimplemented] HOT 1
- travis fails to build HOT 2
- Re-add rustfmt and clippy to TravisCI build HOT 1
- Update `approx` dependency
- Validate and ideally repace uses of `mem::transmute`
- Typo in UCUM: MH20
- http://paholg.com/dimensioned/ is a 404 HOT 3
- Question: accepting generic dimension for calculation in specific units HOT 11
- Question: working with nalgebra HOT 7
- Feature request: Celsius HOT 5
- support std::iter::Sum. HOT 1
- Any good idea how to integrate with `ordered_float`? HOT 3
- Use conventional letter case HOT 1
- fmt String as Joules instead of MKS HOT 2
- has_units() -> bool HOT 2
- Implement `max` and `min` functions for primitive types that have them HOT 1
- `serde_test` should be dev-dependency
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dimensioned.