Git Product home page Git Product logo

Comments (15)

paholg avatar paholg commented on May 24, 2024

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.

paholg avatar paholg commented on May 24, 2024

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.

MarkSwanson avatar MarkSwanson commented on May 24, 2024

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.

MarkSwanson avatar MarkSwanson commented on May 24, 2024

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.

paholg avatar paholg commented on May 24, 2024

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.

MarkSwanson avatar MarkSwanson commented on May 24, 2024

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

from dimensioned.

MarkSwanson avatar MarkSwanson commented on May 24, 2024

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

from dimensioned.

paholg avatar paholg commented on May 24, 2024

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

from dimensioned.

MarkSwanson avatar MarkSwanson commented on May 24, 2024

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

from dimensioned.

paholg avatar paholg commented on May 24, 2024

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.

MarkSwanson avatar MarkSwanson commented on May 24, 2024

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.

paholg avatar paholg commented on May 24, 2024

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.

MarkSwanson avatar MarkSwanson commented on May 24, 2024

Great! Will try it out shortly.

from dimensioned.

MarkSwanson avatar MarkSwanson commented on May 24, 2024

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.

MarkSwanson avatar MarkSwanson commented on May 24, 2024

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

from dimensioned.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.