paholg / dimensioned

Compile-time dimensional analysis for various unit systems using Rust's type system.
https://crates.io/crates/dimensioned
MIT License
300 stars 23 forks source link

Support f256 via the qd github repo #77

Closed MarkSwanson closed 2 years ago

MarkSwanson commented 2 years ago

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!

paholg commented 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.

paholg commented 2 years ago

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.

MarkSwanson commented 2 years ago

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...

MarkSwanson commented 2 years ago

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 = Meter { value_unsafe: qd!(1.0), _marker: PhantomData, }; let KILOGRAM: Kilogram = Kilogram { value_unsafe: qd!(1.0), _marker: PhantomData, }; let SECOND: Second = Second { value_unsafe: qd!(1.0), _marker: PhantomData, }; derived!( si, SI: Meter2KilogramPerSecond = Meter * Meter * Kilogram / Second ); let PLANCK: Meter2KilogramPerSecond = Meter2KilogramPerSecond { value_unsafe: qd!(6.62607015e-34), _marker: PhantomData, }; let x = qd!(3.0) * PLANCK; println!("{x}"); }
paholg commented 2 years ago

This is a limitation due to the orphan rules. It should work if you put the dimensioned quantity on the left-hand side.

MarkSwanson commented 2 years ago

Perhaps closer :-) let x = PLANCK * qd!(3.0); expected struct SI, found struct Quad

MarkSwanson commented 2 years ago

This works :-) let x = PLANCK.value_unsafe * y;

paholg commented 2 years ago

Yeah, that works, but you lose all of your units.

MarkSwanson commented 2 years ago

Because of orphan rules does it seem like the best way is to support qd / Quad inside of dimensioned?

paholg commented 2 years ago

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;
MarkSwanson commented 2 years ago

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.

paholg commented 2 years ago

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));
MarkSwanson commented 2 years ago

Great! Will try it out shortly.

MarkSwanson commented 2 years ago

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⁻¹

MarkSwanson commented 2 years ago

All of my brief testing today works fine. Closing. Thanks again.