Group Abstract Group Abstract

Message Boards Message Boards

1
|
5.2K Views
|
8 Replies
|
10 Total Likes
View groups...
Share
Share this post:

Correct use of quantities?!

Hi all,

I always thought that working with quantities would be kind of "fool proof", but then - after quite a while of debugging(!) - I stumbled across this:

tempCelsius = Quantity[180., "DegreesCelsius"];
tempKelvins = Quantity[450., "Kelvins"];

tempCelsius/tempKelvins
(*  Out:   0.4`   - obviously nonsense!  *)

UnitConvert[tempCelsius]/tempKelvins
(*  Out:   1.007` - correct value  *)

What am I missing?

POSTED BY: Henrik Schachner
8 Replies
Posted 3 years ago

Got it. Thanks!

POSTED BY: Eric Rimbey
POSTED BY: Henrik Schachner
Posted 3 years ago

In general, I like this updated functionality. But I'm confused by the following:

Quantity[300., "Kelvins"] - Quantity[2., "DegreesCelsius"]

-Quantity[-298., "DegreesCelsius"]

What am I not understanding here? Or is this a bug? Compare to

Quantity[300., "Kelvins"] - UnitConvert[Quantity[2., "DegreesCelsius"]]

Quantity[24.850000000000023, "Kelvins"]

POSTED BY: Eric Rimbey
Posted 3 years ago

V13.2 also added support for temperature differences.

POSTED BY: Rohit Namjoshi

I am glad that WRI did see a problem here - and fixed it in 13.2! Now "DegreesCelsius" and "Kelvins" no longer simply cancel:

enter image description here

Many thanks to WRI for that and all your work - and best regards from Germany!

Henrik Schachner

PS.: For sake of completeness - here the code:

kB = Entity["PhysicalConstant", "BoltzmannConstant"]["Value"]
iceMeltingTempDeg = Quantity[0, "DegreesCelsius"]
iceMeltingTempKelvin = UnitConvert[iceMeltingTempDeg, "Kelvins"]
kB*iceMeltingTempDeg
kB*iceMeltingTempKelvin
POSTED BY: Henrik Schachner
Posted 4 years ago

Again, I see no problem here. You haven't provided any context that would tell us what you expect kB Quantity[0., "DegreesCelsius"] to mean. Maybe we were looking at something that involved the difference of two temperatures, and when the two temperatures are the same, I would absolutely expect the result to be Quantity[0., "Joules"]`.

As for making sure that all values are consistent, well, we're in a garbage-in-garbage-out situation here. The responsibility is totally on the human for specifying semantically meaningful inputs and for carefully interpreting the outputs. It's not the computer's job to tell you when you're being nonsensical.

I could just as easily be exasperated by the fact that Mathematica reduces Sqrt[Quantity[-1, Power["Meters", 2]]] to Quantity[Complex[0, 1], "Meters"]. It is dangerous and unhelpful for Mathematica to tell me that the side length of my square is imaginary. If I have to make sure that my inputs make sense, then I don't need units at all!

We can argue about personal preferences for what the language should do in the cases you've provided, but I'm not sure that's a proper use of this forum. I think folks here would be happy to provide some heuristics or best practices for working with Quantity, if that's what you want. Or if you need help setting up a model that uses Quantity, I'm sure someone here has the expertise. Or if you'd like to know how to write Mathematica code that does the checks that you want, we could show you how to do that.

POSTED BY: Eric Rimbey

Hi Eric,

For example, what should 2*tempCelsius be? Should it be 360 C or 633.15 C?

Yes, thats a good point. I would say it should be (2*180) °C - because I guess nobody expects a physical interpretation of "°C" here. And the unit of the result is simply the product of "1" and "°C".

But in my example the units "°C" and "K" had simply canceled out - and this cannot be! In this case I would at least expect

  • a clear rule in the documentation saying e.g. that everything is converted into SI-units before operations (or cancelations!) are done
  • or those expressions simply stay unevalueated

I mean if I work e.g. with the very frequent expression $k_B T$ and using units, I get:

kB = Entity["PhysicalConstant", "BoltzmannConstant"]["Value"];
kB Quantity[0., "DegreesCelsius"]
(*  Out:   0.`J   *)

And this is just a (dangerous!) nonsense! Why should anyone bother using units if those things are not working reliably? If I always first have to make sure that the units of all values are consistent, I do not need units at all!

POSTED BY: Henrik Schachner
Posted 4 years ago
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard