Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Steve's Programming Challenge: Weights and Measures
#3
(08-15-2024, 11:49 PM)Pete Wrote: My father owned an Italian Deli when I was a kid. This reminds me of the time I was taking my first algebra class in junior high school, and I asked him...

Ham is $1.50 a pound, Cheese is $1.00 a pound, and roast beef is $2.50 a pound. If a man walks into your store and has $20 to spend and he wants twice as much roast beef as ham but half as much cheese as roast beef and wants to know how much roast beef, ham, and cheese he will get for $20, what do you tell him?

My Dad's reply was: "I'd tell him to get the hell out of my store!"

Pete  Big Grin

The trick here is to show the impossibility of the answer.

All weights can be described in relation to the smallest one:

1/256 weight = 1 of the smallest   (1/256)
1/128 weight = 2 of the smallest   (2/256)
1/64   weight = 4 of the smallest   (4/256)

..see where I'm going with this here?

Now, 11/32 can also be represented in relation to that smallest size.  11/32 = 88/256.    As 88 is a whole number, we can pick out whole number combinations to match that.   a 1/4 weight  (8/32) + a 1/16 weight (2/32) + a 1/32 weight = 11/32...     Perfect match.

The problem comes when you get to the 1/10 pound nugget.   That's 25.6/256...   Our values only represent WHOLE numbers.  We might move the scales to 25/256, or over to 26/256, but it's *IMPOSSIBLE* to get 25.6/256 pounds.   

It's the inherent limit of binary math and floating point numbers.  It's literally IMPOSSIBLE to perfectly represent some values.  You can divide your weights in half until they're on the atomic level, and... you're still not going to get 1/10 represented perfectly.  

An exercise like this is the perfect way for folks to visualize what's going on under the hood with floating point values.  Wink
Reply


Messages In This Thread
RE: Steve's Programming Challenge: Weights and Measures - by SMcNeill - 08-16-2024, 12:01 AM



Users browsing this thread: 32 Guest(s)