Ratio Percentage Dilemma

101 Views Asked by At

I was given a scenario at work in which I was to set a boolean flag if two weights of items were a certain percentage of each other.

So for example, I have a box a customer has dropped off for shipping. The customer stated the box weighed, 5 lbs, let's call this our gross weight.

Later on throughout the shipping process, we weigh this box with a scale. Let's call this weight the tare weight.

The scenario I was given was,

 If the gross weight is greater than or equal to 110% of the tare weight, set a value to "true" else set it to "false" 

So, I setup a simple ratio in our application to calculate this:

if(grossWeight/tareWeight >= 1.1) -> set to true

However, I came across an issue where bad data could lead to dividing by zero, so instead of writing the above I used the following:

if(grossWeight/1.1 >= tareWeight -> set to true

This way, no matter what data given, I will always be dividing by 1.1 and can stop any bad division errors caused by bad input data.

Now my question would be are both of these equations equal?

Given a grossWeight of 0 and a tareWeight of 0, the first equation is either invalid or true, because anything divided by zero is undefined. However, some cases x/0 is infinite and infinity is always greater than 1.1

The second equation is true because 0 is greater than or equal to zero.

2

There are 2 best solutions below

0
On BEST ANSWER

To answer your basic question, the two inequalities are definitely equivalent for non-zero values of the variables. Another way to avoid problematic division would be to check if grossWeight is $\ge 1.1\times$ tareWeight.

If you don’t want the flag to arise in the case that both weights are zero, you could just add another check: if (GW $\ge 1.1\times$ TW) and (GW $>0$) then TRUE.

0
On

Let $g$ be the gross weight, $t$ be the tare weight. Then we have $$\frac gt \geq 1.1$$

Multiply by $t$:

$$g \geq 1.1t$$

Divide by $1.1$:

$$\frac{g}{1.1} \geq t$$

So yes, they are indeed the same equation.