Dev.to WebDev πŸ›  Dev πŸ‘ 2 πŸ“– 2 min read

Percentage of what? The off-by-8% bug hiding in candle fragrance math

I maintain a set of free craft calculators. The candle fragrance one looks like a single multiplication, and it produced the most confidently wrong first version of anything I have written. The problem A cand

I maintain a set of free craft calculators. The candle fragrance one looks like a single multiplication, and it produced the most confidently wrong first version of anything I have written.

The problem

A candle maker wants to fill an 8 oz jar at an 8% fragrance load. How much wax and how much fragrance oil?

The obvious answer is 8% of 8 oz: 0.64 oz of oil and 7.36 oz of wax. It is wrong, and candle makers will tell you so.

Percentage of what?

In candle making, fragrance load is a percentage of the wax weight, not of the finished candle. That is the industry convention, and it is how wax suppliers state the maximum load a wax can hold.

So the two quantities are tied together like this:

fragrance = wax * load
total     = wax + fragrance = wax * (1 + load)

If you know the wax weight, you multiply. If you know the total pour weight, which is what a jar size gives you, you have to divide:

wax       = total / (1 + load)
fragrance = total - wax

For the 8 oz jar at 8%: wax is 8 / 1.08 = 7.41 oz and fragrance is 0.59 oz. The naive version puts 0.64 oz of oil into 7.36 oz of wax, which is a real load of 8.7%. On a wax rated for 8% that is enough to make the candle sweat oil.

The code

The function accepts either starting point and refuses to guess when it gets both or neither:

type Input =
  | { loadPercent: number; wax: number }
  | { loadPercent: number; total: number };

function fragrance(input: Input) {
  const load = input.loadPercent / 100;
  const wax = 'wax' in input ? input.wax : input.total / (1 + load);
  const oil = wax * load;
  return { wax, oil, total: wax + oil };
}

This is a simplified version of what runs on the site. The real one works in grams internally and converts back at the end, so rounding happens once.

What I took from it

A percentage is not a number until you know its base. Markup and margin have the same trap: 50% markup on cost is 33% margin on price. So do tax-inclusive prices and anything described as "percent of the batch".

Now the first thing I write for any calculator with a percent input is the sentence "percent of ___", and the first test is the case where the two readings give different answers.

The calculator is here if you want to try the numbers: https://handmadecalculators.com/calculators/candle-fragrance

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.