2
$\begingroup$

If you lets say work 40 hours every week and your working days total to 251/5 weeks for a particular year. Then why is Mathematica messing this calculation and producing incorrect result?

Quantity[40,("Hours")/("Weeks")]*Quantity[251/5,("Weeks")/("Years")]

The total number of hours worked in this particular year should not be:

251/1095

Incorrect magnitude and dimensionless!


Correction: Yes, this quantity is indeed dimensionless but doesn't have to be unitless. I am looking for an intermediate result without automatic unit conversion dissolving it into a unitless proportion.

$\endgroup$
6
  • $\begingroup$ It is weird. I modified what you wrote and added a trace: Trace[Quantity[40, "hours per week"]*Quantity[251/7, "weeks per year"]] and the result is strange. $\endgroup$
    – Mark R
    Commented Jan 4, 2020 at 21:26
  • $\begingroup$ By strange I mean that up until the last one, it seems to be doing the right thing and then abruptly gives the 251/1533 that you say. $\endgroup$
    – Mark R
    Commented Jan 4, 2020 at 21:28
  • 3
    $\begingroup$ And if you want to see something really weird, do the calculation I showed and ask whether these are equal: Quantity[40*251/7, "hours per year"] == Quantity[40, "hours per week"]*Quantity[251/7, "weeks per year"] It says True! $\endgroup$
    – Mark R
    Commented Jan 4, 2020 at 21:30
  • 2
    $\begingroup$ I think the problem might be all these are units of time and Mathematica is applying the conversions before resolving them arithmetically. For me, I wrote a function that converts similar units to the IndependentUnit form so as to avoid automatic conversions. $\endgroup$
    – user13892
    Commented Jan 4, 2020 at 23:10
  • 1
    $\begingroup$ Hours and years are both units of time, so hours/year should be dimensionless, no? $\endgroup$
    – Carmeister
    Commented Jan 5, 2020 at 9:32

3 Answers 3

9
$\begingroup$

"Hours", "Weeks" and "Years" are all time units:

UnitDimensions /@ {"Hours", "Weeks", "Years"}

{{{"TimeUnit", 1}}, {{"TimeUnit", 1}}, {{"TimeUnit", 1}}}

It is not easy to prevent Quantity arithmetic from cancelling these units. You could use UnitConvert to convert the output to the desired unit:

UnitConvert[
    Quantity[40,("Hours")/("Weeks")] * Quantity[251/7,("Weeks")/("Years")],
    "Hours" / "Years"
]

Quantity[10040/7, ("Hours")/("Years")]

$\endgroup$
2
  • 1
    $\begingroup$ Thank you for the answer I did try this but since I am dealing with a large data and can't be sure whether Mathematica's UnitConvert will always follow such convention for dimensionless quantity to produce intermediate results, I have converted all units of the same type into the independent form before they interact arithmetically using the IndependentUnit wrapper so never to invoke Mathematica's own transformation. Which works fine for me for the time being and is producing result I am able to interpret. $\endgroup$
    – user13892
    Commented Jan 5, 2020 at 11:22
  • $\begingroup$ But it would be nice to have a proper mechanism to hold dimensionless qualities in intermediate form by preventing the automatic transformation of units of the same (certain) type in a particular calculation. $\endgroup$
    – user13892
    Commented Jan 5, 2020 at 11:44
12
$\begingroup$

First, regarding the substance of the calculation apart from Mathematica's unit handling: I think your input suggests some confusion about what you're trying to calculate. You say

your working days total to 251/7 weeks for a particular year.

I assume that there are 251 working days in that year (this is a fairly typical number). However, your 251/7 number is not compatible with the meaning of 40 hours per week, which refers to a week containing 5 working days. The number of weeks in the year should be 251/5, not 251/7. So, the result you are looking for is 40 * 251/5 = 2008 hours worked in the year. Or, more simply, 8 hours worked on each of 251 working days gives 2008 hours worked. The other answers here guide you to a result of 10040/7 ~ 1434 hours worked, which I suspect is factually wrong for what you are trying to calculate.

After this correction is made in your input, the result from Mathematica is 251/1095 ~ 0.229. This dimensionless result is correct and has a straightforward physical interpretation: It is the worker's duty cycle, the fraction of time they are working. Based on its physical meaning, this number must be between 0 and 1, which is a sanity check on the calculation.

As Carl Woll's answer suggests, the result you wanted to see is the duty cycle expressed in units of hours per year. You are free to convert it to those units, but it is still a dimensionless quantity.

$\endgroup$
2
  • $\begingroup$ you are right! nice catch, forget to change the denominator to 5 in the question. $\endgroup$
    – user13892
    Commented Jan 5, 2020 at 11:13
  • $\begingroup$ Thank you @nanoman for the answer! Didn't know there was a "termed" interpretation of the simplified fraction. $\endgroup$
    – user13892
    Commented Jan 5, 2020 at 14:00
8
$\begingroup$

This is the computation you really want:

Quantity[40, "Hours"/"Weeks"]Quantity[251/7, "Weeks"]

Quantity[10040/7, "Hours"] r

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.