Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] switching from float to Decimal

Started by cignox1 Dec 15, 2005 at 5:39 PM 6 replies 1.1k views
Original Post
cignox1
cignox1
Hi, I have done a simple financial application to let my gf easily keep a track of her money without using excel. I used float to represent the values, but precision issues appeared while testing the app. I thought to keep using float and rounding the values to the second decimal place whenever I display them, but I'm not sure about the reliability of this solution, so I thought to use Decimal. My problem is that Decimal does not have an '=' operator nor a copy constructor, and I dont know what problem should I expect replacing floats with Decimals in 2000+ lines. What I'm not sure about is the semantical behaviour when I use reference passing method (used by Decimal) instead than the value copy used by float. I don't know what major issues can this lead to. Should I keep using float? If not, there are guidelines to make this switch not so painful? Thank you!
Iarus
Iarus
...or you could use double
etothex
etothex
You shouldn't use floating point numbers in financial applications, regardless of precision.

My suggestion would be to just use integers, multiplied by 100. (i.e., $1.45 => 145) Then you can do divide/modulo tricks to extract the cents part and the dollars part when you need them. Display is a little more complicated, but not too much.
cignox1
cignox1
Quote:
Original post by etothex
You shouldn't use floating point numbers in financial applications, regardless of precision.

My suggestion would be to just use integers, multiplied by 100. (i.e., $1.45 => 145) Then you can do divide/modulo tricks to extract the cents part and the dollars part when you need them. Display is a little more complicated, but not too much.


Thank you. This would remove the problem of the methods call. A bit tricky and unelegant, but I suppose that I should have thought to the numbers type before even starting with the project.
Thank you again!
Since I'm not so used to financial applications, how should I round the results of division? Truncation, rounding or user defined?
rate++
etothex
etothex
Hmm, I would try to avoid any divisions except for ones which extract the different components. Division means rounding which means precision errors which means you're back where you started.

Otherwise, how you handle division depends on what you are doing. The one case I can think of is if you want to display averages(in which case cents precision can be +/- 1).

In that case convert to float and divide.
cignox1
cignox1
Quote:
Original post by etothex
Hmm, I would try to avoid any divisions except for ones which extract the different components. Division means rounding which means precision errors which means you're back where you started.

Otherwise, how you handle division depends on what you are doing. The one case I can think of is if you want to display averages(in which case cents precision can be +/- 1).

In that case convert to float and divide.


Yes, The program features some simple analisys tool, like average as you said, that require divisions. But I have other cases when I need to make multiplications (like when I convert the currency used to another).
Then I will use floats to perform those computyations.
A last question: How many decimal places do I need? Do 2 be enaught? How many are commonly used?
Xai
Xai
one thing worth noting is that when financial applications split money into pools, they cannot seperately compute each pool reliably, one pool must receive the left over amounts ...

for instance, if you are divings $10.00 into thirds, you cannot just say

a = 1000 / 3
b = 1000 / 3
c = 1000 / 3

you have to say something more like

a = 1000 / 3
b = 1000 / 3
c = 1000 - a - b

which of course is not how you would actually write the algortihms, but shows the concept.

as for how many decimal places, it depends on the app. Many apps can stand to only be accurate to the nearest cent, but you need a VERY CLEAR definable policy for how rounding is done, and where the favor goes (diving $0.05 by 2 yields 3 and 2 cents, but you need to have a way to determine which party gets the extra cent).

One application I saw called these concepts "portions" to make it clear that all REAL money moves into portions, it is never actually divided (mathematically).

Some apps needs tenths of cents, and I have even seen one which needed thousandths of cents.

Now many types of non-real money are mathematical in nature, such as computations of yields, etc. These types of values follow normal matimatical rules.
cignox1
cignox1
Ha, I really hate those fields of computers programming :-) I really hope that my future work wont be involved in such things (hurray for computer graphic!).

Well, I suppose that for the purposes of my program loosing some cents wont be a big problem (I will ask to my girlfriend if she agree). Since I spent many hours doing all the graphics by hand and then coloring them with Gimp, I don't feel like not finishing the project.
I think that I would use 2 decimal places. IIRC my bank rounds the values to the 2 decimal place (I guess they truncate, bu I'll check and I can use the same rule). This should give enaugh precision.

Thank to everybody!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.