Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Steve's Programming Challenge: Weights and Measures
#10
(08-16-2024, 12:52 AM)Jack Wrote: Steve, I agree with you but most of the time floating-point math works surprisingly well, but then you have cases where it fails catastrophically

Aye.  Folks just need to realize *WHY* it tends to fail, when it does.

FOR i = 1 TO 100 STEP 0.1
   PRINT i
NEXT

^That's *going* to have issues in it, just for the simple fact that we're adding 0.1 -- a number we can't represent perfectly -- over and over in that loop, which compounds the level of error until it's quite noticeable.  It's not a flaw in QB64.  It's not a glitch in the matrix.  There's no "Fix" for it.   That's just life with floating point values.  They're fast -- but they're imprecise.

If one needs precision, use integer values (Loop from 10 to 1000 and divide by 10).  It's why banks track money by pennies and not dollars.  You have 12345 pennies in the bank, not $123.45 dollars.   The dollars is just formatted at the PRINT end, to make it easier for people to understand and relate to.  Internally, it's all pennies.  Smile
Reply


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



Users browsing this thread: 1 Guest(s)