I need to design a function to perform the basic arithmetic operations that are addition (+)
, subtraction (-)
, multiplication (x)
, and division (/)
between 2 numbers.
That function takes 3 arguments:
-number1
: composed of a value
and a unit
.
-number2
: compoased of a value
and a unit
.
-operation_type
: one of the 4 aforementioned operations.
and should return:
-number3
: composed of a value
and a unit
.
Returning number3
's value
is easy as all I need to do is use conditional statements to perform the corresponding operation (e.g. if (operation_type == 'addition') { number3.value = number1.value + number2.value; }
).
However I'm having difficulties figuring out how I should represent and deal with the unit
(for multiplication
and subtraction
operations, as for addition
and subtraction
it remains the same) in a way that I can chain up multiple operations and properly update the resulting unit
every time. Here is an example:
operations type: multiplications
number1.unit: Kw
number2.unit: h / user / year
number3.unit: Kwh / user / year _
|
number1.unit: Kwh / user / year <-
number2.unit: user
number3.unit: Kwh / year _
|
number1.unit: Kwh / year <-
number2.unit: year
number3.unit: Kwh
...
My only constraint is that the unit
must initially be represented as a string
because it is stored in the database, then we can parse parse it into an object/array
as needed.
Some of the problems I'm facing:
Q1: how to ensure consistent order in the resulting unit (e.g. h x Kw
and Kw x h
should both give Kwh
)?
Q2: how to deal with complex units (e.g. foo / Kw x h / bar
)?
And because hope dies last:
Q3: Are there any known algorithms / design patterns for dealing with this?