Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Capture division by zero
Re: Capture division by zero [message #58696 is a reply to message #55277] |
Mon, 25 July 2022 03:37   |
jjacksonRIAB
Messages: 227 Registered: June 2011
|
Experienced Member |
|
|
Quote:
The calculation of your bank account balance is infinite
Anyone have an opinion on this one? I'm of the opinion that floats should not be used to represent any kind of currency or bank balance and I tended to replace them in C# with Decimal when I worked for an accounting department. I haven't checked but does U++ have a fixed-point decimal type for dealing with currency and other numbers where float accuracy is not good enough? If not maybe one should be made.
EDIT Just realized I didn't provide a reason why not to use them. What I found is it wasn't NaN-stuff (though that could be a problem), but mainly because floats have a tendency to accumulate errors over successive calculations - that and the fact that you might want Banker's rounding or some other kind of rounding algorithm. Another reason I ditched floats is because they can't produce consistent results on different architectures, different CPUs, different families of CPUs, or even the same CPU. In one particularly egregious case I had a bad video driver produce wildly inaccurate results on a single accountant's machine and I could only guess that some of the computation was being offloaded to the GPU.
Looking further into C#'s decimal type, it appears it uses arbitrary precision floats underneath. There are number of libraries for doing this in C++, some listed here:
https://en.wikipedia.org/wiki/List_of_arbitrary-precision_ar ithmetic_software
Since they are still floats it's not a cure all for the problem of cumulative errors so I was wrong about that. That said, I still think a fixed-point decimal class would be desirable.
[Updated on: Mon, 25 July 2022 04:49] Report message to a moderator
|
|
|
 |
|
Capture division by zero
By: koldo on Fri, 16 October 2020 09:15
|
 |
|
Re: Capture division by zero
By: Didier on Sat, 17 October 2020 23:53
|
 |
|
Re: Capture division by zero
By: koldo on Sun, 18 October 2020 21:38
|
 |
|
Re: Capture division by zero
By: koldo on Mon, 19 October 2020 07:57
|
 |
|
Re: Capture division by zero
By: Didier on Mon, 19 October 2020 20:54
|
 |
|
Re: Capture division by zero
By: koldo on Tue, 20 October 2020 08:02
|
 |
|
Re: Capture division by zero
By: Didier on Tue, 20 October 2020 20:58
|
 |
|
Re: Capture division by zero
By: koldo on Wed, 21 October 2020 07:55
|
 |
|
Re: Capture division by zero
By: Didier on Wed, 21 October 2020 20:29
|
 |
|
Re: Capture division by zero
By: mirek on Tue, 27 October 2020 09:27
|
 |
|
Re: Capture division by zero
By: koldo on Tue, 27 October 2020 10:58
|
 |
|
Re: Capture division by zero
|
 |
|
Re: Capture division by zero
By: mirek on Mon, 25 July 2022 09:39
|
 |
|
Re: Capture division by zero
|
 |
|
Re: Capture division by zero
By: Klugier on Sun, 18 October 2020 21:21
|
 |
|
Re: Capture division by zero
By: Klugier on Mon, 26 October 2020 23:51
|
 |
|
Re: Capture division by zero
By: mirek on Tue, 27 October 2020 11:53
|
 |
|
Re: Capture division by zero
By: koldo on Tue, 27 October 2020 17:08
|
 |
|
Re: Capture division by zero
By: Didier on Tue, 27 October 2020 18:58
|
 |
|
Re: Capture division by zero
By: koldo on Tue, 27 October 2020 19:21
|
Goto Forum:
Current Time: Wed Apr 30 16:19:27 CEST 2025
Total time taken to generate the page: 0.00708 seconds
|