WoodCentral Forums

Est. 1998 — 27 years of woodworking knowledge

A harder one

Posts

A harder one

#1

A harder one

Alex Y

This one will almost certainly require a calculator or computer.

I went into my local 7-11 and bought four items. The clerk used a calculator to determine the total, which was $7.11 before tax. But I noticed that he had hit the multiplication key rather than the addition key each time, so I asked him to redo it. He did, using the addition key this time, and it came out to the same $7.11 pretax amount.

What were the prices of the four items I bought?

Re: A harder one

#2

Re: A harder one

Bill Earl

Actually, on a computer, it is trickier than it first appears.

Unless you have a BCD math library handy, the straightforward solution will not work as expected.

Re: A harder one

#3

Just one penny

Bill Earl

The rest can be nickels, dimes and quarters.

Re: A harder one

#4

Sounds right.


Re: A harder one

#5

Re: A harder one

Alex Y

What's a BCD math library? Or would I know if you told me? ;)

Re: A harder one

#6

Re: A harder one

Bill Earl

The answer I got was $1.20, $1.25, $1.50 & $3.16

The 'obvious' way to program this is with floating point numbers. The problem is that binary floating point numbers are only an approximation of real numbers. Better programmers are aware of the limited precision for very large or very small numbers, but what is commonly overlooked is the imprecision that exists in the floating point representation of a number like 0.1.

In fact, there is no exact representation for 0.1 in floating point binary. The mantissa ends up as a repeating binary fraction. These errors accumulate with each operation. So, seemingly simple floating point calculations on relatively ordinary looking numbers might produce a result like $7.109999999999999999999 when what you expected was $7.11.

The common solution to this problem is to use Binary Coded Decimal (BCD). Instead of the exponent-mantissa representation of floating-point, BCD uses an array of binary coded decimal digits. This allows any decimal number to be represented to an arbitrary level of precision.

BCD is commonly used in calculators, financial software packages and scientific applications where extreme precision is required.

👍 This page answered my questions

Your vote helps other woodworkers quickly find the answers and techniques that actually work in the shop.