QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: CharlieJV on April 01, 2022, 12:06:14 am

Title: How do you deal with floating point rounding errors ?
Post by: CharlieJV on April 01, 2022, 12:06:14 am
Programming since 1984, and I can't believe I'm only running into this now.

Lucky?  Meh.

Code: QB64: [Select]
  1. for i -1 to 1 step 0.1

How do you get around this rounding error?

Is it best to avoid floating point numbers, and divide final results by whatever to get right decimal points?

ie. should the code above be changed to:

Code: QB64: [Select]
  1. for i -10 to 10 step 1
  2. print i/10,

Title: Re: How do you deal with floating point rounding errors ?
Post by: Cobalt on April 01, 2022, 12:11:14 am
There is no single right answer.

You'll have to find the way that best works with what your trying to accomplish.

divide as you have shown there, or

z! = INT(I!*1000)\1000  say if you wanted 3 points of precision.

Use that string math thingy thats floating around here somewhere.

Or just work something out that gives you the results that you can work with.

Just the nature of the floating point beast.
Title: Re: How do you deal with floating point rounding errors ?
Post by: CharlieJV on April 01, 2022, 12:27:59 am
2 years in a programming course at college, and 5 years at university to get a computer science degree, and I can't believe this never came up, especially in the two digital design courses at university.

This is pretty fundamental stuff, now that I've read a few things about it.

I have a few choice words for my educators from back then...
Title: Re: How do you deal with floating point rounding errors ?
Post by: Cobalt on April 01, 2022, 12:43:14 am
Its all relative, if thats not an over used phrase.

How much precision do you really need?

If your trying to pinpoint a single snow flake on the surface of Pluto then you probably need some pretty sharp precision.

If your trying to put a golf ball in a 55 gallon drum sitting next to you.. probably not.

They're not going to waste a lot of time on something like this unless your going into a field that requires accuracy on the sub-atomic scale.

(why am I double spacing all my posts?)
Title: Re: How do you deal with floating point rounding errors ?
Post by: SMcNeill on April 01, 2022, 12:45:56 am
You're simply dealing with the immutable nature of floating point values.

In decimal form, what is 1/3? 

It's 0.33333333333333333*** onto an infinite number of 3s.  It's impossible to perfectly represent 1/3 in decimal format, so eventually you reach a point where you say, "It's close enough for my needs!"  That might be 0.33, or 0.33333333, or 0.333333333333333333333, but at some point you just stop pasting on 3s and you call it "good enough".

The problem, however, comes when you start adding those decimal values together.  Add your number three times and you get 0.99, 0.99999999, or 0.999999999999999999999, but you'll never exactly add up to one!

Rounding errors are just part of the nature of the beast, and you have to figure out how to deal with them.

One of the simplest ways, is to do like banks do: Eliminate decimals entirely!  Your bank doesn't track your account as having $123.45 in it.  They track it as you having 12,345 PENNIES in it!  Integer values instead of decimal values, and it's only when displaying the amount that they convert down to decimals for the user.  (Much like your second code box does.)

Another simple way, in this case, is to limit results via rounding, since you only want a single digit decimal value.  Something like the following should work:

i = INT(10 * i + .5) /10   

Another solution is to use PRINT USING to format the results to single decimal precision:

PRINT USING "###.#"; i

Those tend to be your basic 3 ways of dealing with the issue.
1) Convert to integers and don't deal with floating points at all.
2) Manually round to a lower level of precision than the variable type normally holds.
3) Format the print to account for the issue, without altering those floating point values.
Title: Re: How do you deal with floating point rounding errors ?
Post by: Pete on April 01, 2022, 03:17:39 am
The need for "String Math" strikes again. Bruhahahahaha!

Pete
Title: Re: How do you deal with floating point rounding errors ?
Post by: jack on April 01, 2022, 07:07:30 pm
string-math may be the solution for you, it's a pity QB64 doesn't have a decimal type
I know that some people use integers as a means to simulate fixed-point decimals but it's a far cry from having true decimal-floating point
you could use one of the decimal floating point libraries like Intel's library https://www.intel.com/content/www/us/en/developer/articles/tool/intel-decimal-floating-point-math-library.html but it's use would be cumbersome
Title: Re: How do you deal with floating point rounding errors ?
Post by: Cobalt on April 02, 2022, 10:32:46 am
but it's use would be cumbersome

Hence why QB64 would not have one. BASIC is not meant to be cumbersome. Which of course may leave some wanting, but if they need

something like that they probably need to step away from BASIC languages.

But then it comes down to how much actual precision do you really need?
Title: Re: How do you deal with floating point rounding errors ?
Post by: bplus on April 02, 2022, 11:01:46 am
Steve said it some time ago, I paraphrase: Separate the calculations with variables function from the displaying of the results function.

You just need a good format routine to adjust for garbage that enters into calculations with floats. Probably would include rounding to the precision of decimals you desire in the output (display) format.

For CharlieJV this seems to work:
Code: QB64: [Select]
  1. For i = -1 To 1 Step .1
  2.     Print i, format$("##.#", Str$(i))
  3.  
  4. Function format$ (template As String, Source As String)
  5.     Dim d, s, n, i, t$
  6.     d = _Dest: s = _Source
  7.     n = _NewImage(80, 80, 0)
  8.     _Dest n: _Source n
  9.     Print Using template; Val(Source)
  10.     For i = 1 To 79
  11.         t$ = t$ + Chr$(Screen(1, i))
  12.     Next
  13.     If Left$(t$, 1) = "%" Then t$ = Mid$(t$, 2)
  14.     format$ = _Trim$(t$)
  15.     _Dest d: _Source s
  16.     _FreeImage n
  17.  
  18.  
 [ This attachment cannot be displayed inline in 'Print Page' view ]  

Well shoot, seems to be stopping at .9 not 1, dang!

OK this fixes that!
Code: QB64: [Select]
  1. For i = -1 To 1.0001 Step .1
  2.     Print i, format$("##.#", Str$(i))
  3. Function format$ (template As String, Source As String)
  4.     Dim d, s, n, i, t$
  5.     d = _Dest: s = _Source
  6.     n = _NewImage(80, 80, 0)
  7.     _Dest n: _Source n
  8.     Print Using template; Val(Source)
  9.     For i = 1 To 79
  10.         t$ = t$ + Chr$(Screen(1, i))
  11.     Next
  12.     If Left$(t$, 1) = "%" Then t$ = Mid$(t$, 2)
  13.     format$ = _Trim$(t$)
  14.     _Dest d: _Source s
  15.     _FreeImage n
  16.  
Title: Re: How do you deal with floating point rounding errors ?
Post by: jack on April 02, 2022, 11:32:53 am
gcc runtime library has decimal arithmetic functions https://gcc.gnu.org/onlinedocs/gccint/Decimal-float-library-routines.html so it may be possible to use them in QB64
@Cobalt I think that you may have misunderstood what I meant when I said that using a decimal library would be cumbersome, for example you would have call functions for the basic arithmetic operators but if the decimal type was built-into QB64 then you would use as any other supported type
Title: Re: How do you deal with floating point rounding errors ?
Post by: CharlieJV on April 02, 2022, 11:38:19 am
(SNIP!)
Code: QB64: [Select]
  1. For i = -1 To 1.0001 Step .1
  2.     Print i, format$("##.#", Str$(i))
  3. Function format$ (template As String, Source As String)
  4.     Dim d, s, n, i, t$
  5.     d = _Dest: s = _Source
  6.     n = _NewImage(80, 80, 0)
  7.     _Dest n: _Source n
  8.     Print Using template; Val(Source)
  9.     For i = 1 To 79
  10.         t$ = t$ + Chr$(Screen(1, i))
  11.     Next
  12.     If Left$(t$, 1) = "%" Then t$ = Mid$(t$, 2)
  13.     format$ = _Trim$(t$)
  14.     _Dest d: _Source s
  15.     _FreeImage n
  16.  

I'm pretty sure I'll be leaning this way going forward:

Code: QB64: [Select]
  1. For i = -10 To 10 Step 1
  2.     rem i / 10 wherever needed
Title: Re: How do you deal with floating point rounding errors ?
Post by: bplus on April 02, 2022, 11:48:20 am
Yeah it works for Doppler's problem too.
https://qb64forum.alephc.xyz/index.php?topic=4753.msg141737#msg141737

That does not guarantee a panacea, still have rounding errors when garbage builds up with adding and multiplying but this covers allot of problems.
Title: Re: How do you deal with floating point rounding errors ?
Post by: SMcNeill on April 02, 2022, 01:09:48 pm
Personally, here's the general method I use when I have to deal with this type of thing:

Code: QB64: [Select]
  1. For i = -1 To 1 Step 0.1
  2.     Print i
  3.  
  4. For i = -1 To 1 Step 0.1
  5.     Locate , 20: Print i;
  6.     i = Int(i * 10 + .5) / 10
  7.     Locate , 40: Print i

Run this and you'll first see the garbled up mess that adding floating point values give naturally.

Press any key to break sleep, and then you'll see how we're rounding i to our desired precision, and how it affects things.  In the middle column, we see that i keeps to a much smaller level of variance since we're always rounding it to the desired level of precision, keeping the glitches to a bare minimum.  On the right hand column, we see that the results are as we expect them to be after we round them.

The absolute easiest way to deal with this type of issue is to just add a simple function into your code to Fix_Precision, like so:

Code: QB64: [Select]
  1. For i = -1 To 1 Step 0.1
  2.     i = Fix_Precision(i)
  3.     Print i
  4.  
  5. Function Fix_Precision## (value##)
  6.     Fix_Precision = Int(value## * 10 + .5) / 10
Title: Re: How do you deal with floating point rounding errors ?
Post by: SMcNeill on April 02, 2022, 01:14:15 pm
Notice how the fix can also be applied for Doppler's problem as well: 
Code: QB64: [Select]
  1. _Title "Total it" ' from Doppler "defsng math going off the rails, creating a total from list of numbers in file"
  2. ' ref: https://qb64forum.alephc.xyz/index.php?topic=4753.msg141698#msg141698
  3. total = 0
  4. t = 0
  5.  
  6.     Read t$
  7.     If t$ = "EOD" Then Exit Do
  8.     t = Fix_Precision(Val(t$))
  9.     Print t
  10.     Print Str$(total) + " plus " + t$ + " equals ";
  11.     total = Fix_Precision(total + t)
  12.     Print total;
  13.     Print " press any to contiue..."
  14.     Sleep
  15. Data -750,21.71,13.67,22.92,91.25,119.63,10.84,18.46,40.44,171.73,3.08,92.39,57.20,22.79,-2.80,43.87,34.81,83.82,10.85,EOD
  16.  
  17.  
  18. Function Fix_Precision## (value##)
  19.     Fix_Precision = Int(value## * 100 + .5) / 100

Title: Re: How do you deal with floating point rounding errors ?
Post by: SMcNeill on April 02, 2022, 01:24:43 pm
Steve said it some time ago, I paraphrase: Separate the calculations with variables function from the displaying of the results function.

You just need a good format routine to adjust for garbage that enters into calculations with floats. Probably would include rounding to the precision of decimals you desire in the output (display) format.

For CharlieJV this seems to work:
Code: QB64: [Select]
  1. For i = -1 To 1 Step .1
  2.     Print i, format$("##.#", Str$(i))
  3.  
  4. Function format$ (template As String, Source As String)
  5.     Dim d, s, n, i, t$
  6.     d = _Dest: s = _Source
  7.     n = _NewImage(80, 80, 0)
  8.     _Dest n: _Source n
  9.     Print Using template; Val(Source)
  10.     For i = 1 To 79
  11.         t$ = t$ + Chr$(Screen(1, i))
  12.     Next
  13.     If Left$(t$, 1) = "%" Then t$ = Mid$(t$, 2)
  14.     format$ = _Trim$(t$)
  15.     _Dest d: _Source s
  16.     _FreeImage n
  17.  
  18.  
 [ This attachment cannot be displayed inline in 'Print Page' view ]  

Well shoot, seems to be stopping at .9 not 1, dang!

OK this fixes that!
Code: QB64: [Select]
  1. For i = -1 To 1.0001 Step .1
  2.     Print i, format$("##.#", Str$(i))
  3. Function format$ (template As String, Source As String)
  4.     Dim d, s, n, i, t$
  5.     d = _Dest: s = _Source
  6.     n = _NewImage(80, 80, 0)
  7.     _Dest n: _Source n
  8.     Print Using template; Val(Source)
  9.     For i = 1 To 79
  10.         t$ = t$ + Chr$(Screen(1, i))
  11.     Next
  12.     If Left$(t$, 1) = "%" Then t$ = Mid$(t$, 2)
  13.     format$ = _Trim$(t$)
  14.     _Dest d: _Source s
  15.     _FreeImage n
  16.  

The one issue with using format$ (or Print Using) to display your results is the simple fact that:

1) You're still going to be dealing with actually incorrect values.  If you have a line of code in that IF block that read something like IF i = 0.9 THEN...   you'd have a false result as I would be 0.90000002 and not 0.9.
2) Since i holds those incorrect rounding errors, the error is going to keep growing ever larger over time.  Notice in your screenshot where it's 0.80000001 -- a variance of 0.0000001, and then the next value is 0.90000002...  The glitch grows over time!

Round to the desired precision as you go and your problem goes away without accumulating and adding up over time to become a major issue for you.  ;)
Title: Re: How do you deal with floating point rounding errors ?
Post by: CharlieJV on April 02, 2022, 01:36:00 pm
Personally, here's the general method I use when I have to deal with this type of thing:

Code: QB64: [Select]
  1. For i = -1 To 1 Step 0.1
  2. ...
(snip!)

I don't know if I can ever again trust a loop that involves incrementing by decimal values.

Maybe a knee-jerk reaction, but I find myself wondering, looking at any kind of loop like that, if there is unintentional short-changing of the loop (or an opposite "over-loop"?) by an iteration.
Title: Re: How do you deal with floating point rounding errors ?
Post by: SMcNeill on April 02, 2022, 01:50:20 pm
I don't know if I can ever again trust a loop that involves incrementing by decimal values.

Maybe a knee-jerk reaction, but I find myself wondering, looking at any kind of loop like that, if there is unintentional short-changing of the loop (or an opposite "over-loop"?) by an iteration.

It's always a possibility.

FOR i = -1 TO 1 STEP 0.1

NEXT

In the above, even if we add a line that sets i to being exactly 0.9, we can't be certain that the increment won't make it's value 1.00000001 -- which is greater than 1 and will xit the loop one step early!

The only way to be certain we get that final loop is by adding a tolerance level into the code for floating point imperfections:

FOR i = -1 TO 1.01 STEP 0.1

NEXT

The end point is now great enough to absorb the error, but not large enough to allow an extra increment to pass.



I still say the absolute best solution, when possible, is to just avoid the floating point imperfections entirely.

FOR i = -10 TO 10 'use integers when possible
  iDec = i /10 'only convert to the floating point value when necessary
NEXT

It's why the banks track how many PENNIES are in your account, and not how many dollars.  The results are displayed as dollars, but all the calculations are in pennies.
Title: Re: How do you deal with floating point rounding errors ?
Post by: CharlieJV on April 02, 2022, 02:45:33 pm
(SNIP!)
I still say the absolute best solution, when possible, is to just avoid the floating point imperfections entirely.

FOR i = -10 TO 10 'use integers when possible
  iDec = i /10 'only convert to the floating point value when necessary
NEXT

It's why the banks track how many PENNIES are in your account, and not how many dollars.  The results are displayed as dollars, but all the calculations are in pennies.

Yeah, I do believe I am firmly in that camp.

I might just adopt a standard variable and approach for that kind of thing.

like:

Code: QB64: [Select]
  1. fpa = 10 ' floating-point adjustment
  2. FOR i = -1*fpa TO 1*fpa step 0.1*fpa
  3.   print = i/fpa
  4.  
  5. fpa = 100 ' floating-point adjustment
  6. FOR i = -0.1*fpa TO 0.1*fpa step 0.01*fpa
  7.   print i /fpa
  8.  

Maybe.  Time, mood, and pudding o' proof will tell.
Title: Re: How do you deal with floating point rounding errors ?
Post by: jack on April 02, 2022, 02:59:21 pm
CharlieJV
maybe use while loops instead
Title: Re: How do you deal with floating point rounding errors ?
Post by: CharlieJV on April 02, 2022, 04:33:32 pm
CharlieJV
maybe use while loops instead

You've lost me there.  How does a while help?

(Aside and unrelated: I don't really see while loops and for loops as always interchangeable, each to me better than the other depending on the job at hand, often times just for the sake of readability.)

Trying to figure out what you mean, but I'm sure I'm way off:
Code: QB64: [Select]
  1. fpa = 10 ' floating-point adjustment
  2. i = -1*fpa
  3. WHILE i <= TO 1*fpa
  4.   print = i/fpa
  5.   i = i + 0.1*fpa
  6.  
Title: Re: How do you deal with floating point rounding errors ?
Post by: bplus on April 02, 2022, 04:41:22 pm
@CharlieJV  you might be in habit of thinking of i as an incremented (by 1) integer index (like for arrays).

When i is an integer it will do the last loop
Code: QB64: [Select]
  1. for i = 1 to 10
  2.  print i

jack is suggesting a While loop I think to try an get that last loop for i when it isn't an incremented (by 1) integer index but I had same idea tried it and still i a single type + .1 collects gabage and passes exactly 1.000 just like in For loop!


Title: Re: How do you deal with floating point rounding errors ?
Post by: Pete on April 02, 2022, 05:57:40 pm
@SMcNeill

"i = Int(i * 10 + .5) / 10"

That's what I used some 35 years ago to handle accounting in my Quick Basic office software. Never any errors as far as my applications were concerned for addition, subtraction, multiplication, and division.

Great minds think a like, and you and I come up with the same ideas, once in awhile, too! (Left myself wide open on that one.)

Pete
Title: Re: How do you deal with floating point rounding errors ?
Post by: CharlieJV on April 02, 2022, 07:02:44 pm
@CharlieJV  you might be in habit of thinking of i as an incremented (by 1) integer index (like for arrays).

No, I am not thinking that.  When I do a For i = 1 to 5, I expect 1,2,3,4,5 to happen.


jack is suggesting a While loop I think to try an get that last loop for i when it isn't an incremented (by 1) integer index but I had same idea tried it and still i a single type + .1 collects gabage and passes exactly 1.000 just like in For loop!

As per the example you had given with .1, my solution to the problem is to multiply everything by 10 so that we are no longer dealing with floating point numbers, and then divide by ten when we want to show the floating point values.

And that works great.


I'm very confused because I don't understand what point either you or Jack are trying to make.  With the samples I gave, I don't see what value a while-wend would provide over a for-next.


Title: Re: How do you deal with floating point rounding errors ?
Post by: SMcNeill on April 02, 2022, 07:31:03 pm
While... Wend or Do...Loop or GOTO whatever...  *WON'T* make any difference.

The problem is -- and say it with me when I repeat it once more guys -- IT'S IMPOSSIBLE TO PERFECTLY REPRESENT 1/10 IN BINARY FORMAT!!  It simply can't be done!

1/10 is a flawed representation in binary, just as it's impossible to perfect represent 1/3 in decimal.  All you can do is give the closest working estimation for the value -- which is basically a "close enough" figure for most applications.   

The initial value of 1/10 is flawed, by its very nature in binary.  Adding it up repeatedly, in ANY sort of situation -- loop or not -- simply increases the magnitude of that incremental flaw.

Charlie has a good, working solution: Convert to integer values and do away with the floating point inaccuracies completely.  Minimizing usage of floating point variables minimizes the loss of precision which is inherent in their very nature.

It's just the inherent nature of the math base at work.

Title: Re: How do you deal with floating point rounding errors ?
Post by: bplus on April 02, 2022, 09:14:04 pm
Quote
@CharlieJV  you might be in habit of thinking of i as an incremented (by 1) integer index (like for arrays).

No, I am not thinking that.  When I do a For i = 1 to 5, I expect 1,2,3,4,5 to happen.

LOL, I thought April Fools day was yesterday!

And I say this:
Quote
jack is suggesting a While loop I think to try an get that last loop for i when it isn't an incremented (by 1) integer index but I had same idea tried it and still i a single type + .1 collects gabage and passes exactly 1.000 just like in For loop!
Translation While doesn't work either.

And both you and Steve repeat there is no advantage to While, so yeah! we all agree.
Title: Re: How do you deal with floating point rounding errors ?
Post by: jack on April 02, 2022, 09:39:26 pm
gcc runtime library has decimal arithmetic functions https://gcc.gnu.org/onlinedocs/gccint/Decimal-float-library-routines.html so it may be possible to use them in QB64
tried it, but unfortunately QB64 passes the arguments by reference and they need to be by value
since the functions are inline and there's no library I can't use the byval keyword
Code: QB64: [Select]
  1. declare Function __bid_adddd3~&& (a~&&, b~&&)
  2. declare Function __bid_extenddfdd~&& (a#)
  3. declare Function __bid_truncdddf# (a~&&)
  4.  
  5. Dim As Double dx, dy
  6.  
  7. dx = 3.141592653589793
  8. x = __bid_extenddfdd~&&(dx)
  9. y = bid_add(x, x)
  10. dy = __bid_truncdddf#(y)
  11.  
Title: Re: How do you deal with floating point rounding errors ?
Post by: CharlieJV on April 02, 2022, 09:55:26 pm
LOL, I thought April Fools day was yesterday!
(SNIP!)

Pff, I don't need a special day to be fooled something silly.  No pride, no fear: bring it on baby!
Title: Re: How do you deal with floating point rounding errors ?
Post by: STxAxTIC on April 02, 2022, 10:05:17 pm
Hey Steve I was thinking - uh oh, get ready...

...and I know you're infinitely busy already, all thoughts and prayers going your way. That said, can I humbly encourage your QB64 Bible project to hit the issue of floating point math next? Even if it wasn't the next chapter you were going to write, we need something final on this question once and for all. It comes up way too often in the forums. The proper write-up should minimize out-of-house references, and be something written entirely by us, for us -  and you're the guy to do it Steve. The challenge is to write it out *so good* that it answers every question that ever came up about floating point math in qb64.exe, and also anticipates every future question that may arise. Do whatever you want to do, but make this section stand perfectly alone, and write it soon. No references to other chapters, just a stand alone gospel on QB64's float.

This might make the overall project feel less daunting. Do chapters by demand. Do float while it's fresh. Invest the 10,000 keystrokes and you'll save us all 100,000. Pretty Virginia Please.
Title: Re: How do you deal with floating point rounding errors ?
Post by: SMcNeill on April 03, 2022, 12:05:22 am
Hey Steve I was thinking - uh oh, get ready...

...and I know you're infinitely busy already, all thoughts and prayers going your way. That said, can I humbly encourage your QB64 Bible project to hit the issue of floating point math next? Even if it wasn't the next chapter you were going to write, we need something final on this question once and for all. It comes up way too often in the forums. The proper write-up should minimize out-of-house references, and be something written entirely by us, for us -  and you're the guy to do it Steve. The challenge is to write it out *so good* that it answers every question that ever came up about floating point math in qb64.exe, and also anticipates every future question that may arise. Do whatever you want to do, but make this section stand perfectly alone, and write it soon. No references to other chapters, just a stand alone gospel on QB64's float.

This might make the overall project feel less daunting. Do chapters by demand. Do float while it's fresh. Invest the 10,000 keystrokes and you'll save us all 100,000. Pretty Virginia Please.

The problem with floating point stuff is that *even I* -- as unbelievable as that sounds -- am still learning more about how it behaves and what its quirks are all the time.

Here's a quirk I just picked up on a little while ago that made me just close my QB64 window and give up on things for a while:

Code: QB64: [Select]
  1. i = -1.9
  2. x## = i
  3. Print i, x##

i is a single, and it's value isi -1.9.  x## is a float -- much more precision to work with than a trifling single, so the value should be the same easily.  Right??

  [ This attachment cannot be displayed inline in 'Print Page' view ]  

WRONG!!

By passing a SINGLE into a SUB/FUNCTION that looks for a _FLOAT, the value can change just because the variable type used to store that value changed!!

Who would've ever thunk that??  Especially since we're going from low precision floats to high precision floats.  I could almost understand if a float held a value that a single couldn't, but in this case, I'm just blown away by the value change.

Floating Point wierdness....

Can *anyone* ever really write a end-all testament to describe all the oddness that it can perform??
Title: Re: How do you deal with floating point rounding errors ?
Post by: STxAxTIC on April 03, 2022, 01:33:33 am
I know that the world of floating point is wild, but I'm not so taken aback by the example above, especially from a scientific notation point of view. As far as significant figures go, the x## number is just as accurate as the single version, to the precision of single, and that's all we can ask of it. It's like a conservation of information thing. There are lots of ways to justify it this point - something that helps is the single-precision number, print all of the implied zero that follow the last digit. Where ever those zeros end, that's where your x## version's digits start not mattering.

Maybe I should refine my request above. I'm not saying to write something that documents all possible behavior of float, or even to predict the stray digits that occur after the last significant one. What we should do is teach a zen that avoids these questions outright. Don't mess with edge cases, be careful with conversions, don't believe every decimal you see. It's garbage in, garbage out, and only up to the number if significant figures.

It's like calculating the area of something. If you decide some rectangle is 37.23 inches across, and 34.57 inches wide, the product of those numbers is 1287.0411. But ya know what? The ".0411" is complete bullshit if you want to interpret that answer as the area. Each input number had 4 digits, and the area can only be precise to 4 digits, so it's just 1287, no remainder. Anyway, that's same ghost haunting the example you laid out.
Title: Re: How do you deal with floating point rounding errors ?
Post by: CharlieJV on April 03, 2022, 12:26:15 pm
For anybody who wants to do some extra reading on his/her down time:  Google Search for "0.1 0.2 not equal 0.3" (https://www.google.com/search?q=0.1+0.2+not+equal+0.3&rlz=1CABRFU_enCA980&sxsrf=APq-WBtGFBH_o9_AShNSwce8RgftOsLFkQ%3A1649002962673&ei=0slJYrDiKJLr9APik7KwCQ&ved=0ahUKEwiwk8vepvj2AhWSNX0KHeKJDJYQ4dUDCA4&uact=5&oq=0.1+0.2+not+equal+0.3&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEB4yBAgAEB4yBggAEAUQHjIGCAAQBRAeOgYIABAHEB46BAgAEEM6BQgAEIAEOggIABAIEAcQHjoICAAQBxAFEB46BggAEAgQHkoECEEYAUoECEYYAFDrDljCPGD6QmgDcAB4AIABb4gBrgqSAQM0LjmYAQCgAQHAAQE&sclient=gws-wiz)

It is pretty interesting stuff that I wish my digital design course (from university many moons ago) had covered.
Title: Re: How do you deal with floating point rounding errors ?
Post by: bplus on April 03, 2022, 01:11:37 pm
Yeah there's STx paper right there:
Quote
Computers don't usually work in base 10, they work in base 2.

So consider yourself warned. :)