I've had an idea for a class that, instead of storing a number as a single floating-point value, stores the exact value as two or more integers (in cases such as division, fractional exponents, or irrational numbers such as pi or e). The class would have it's own mathematical functions as methods, modifying the internal values and possibly storing additional values as needed, and returning a double. The point would be to always have the exact value, only rounding when returning a value. This sounds like a good idea to me, but i haven't seen it done before. Does it exist and i simply haven't seen it, or is there some inherent flaw with this idea?

Recommended Answers

BigDecimal not work for you?

Jump to Post

All 5 Replies

Re: Idea for Value class with perfect accuracy 80 80

BigDecimal not work for you?

Re: Idea for Value class with perfect accuracy 80 80

Well, when i looked at it before i posted this, it looked like it meant something completely different. I knew it existed somewhere. Thanks!

Re: Idea for Value class with perfect accuracy 80 80

Big Decimal still won't give an exact value for rational numbers such as 1/3, so your idea may still be useful if you store numbers as exact fractions.

Re: Idea for Value class with perfect accuracy 80 80

It makes sense that a BigDecimal couldn't return a fraction, but it does store an exact value, right? The only reason i can think of for it having it's own arithmetic methods would be that it stores it's values in a different way that would allow for exact values, but doesn't work with the regular "a=b+c".

Re: Idea for Value class with perfect accuracy 80 80

but it does store an exact value, right?

Apparently not! This extract is from the BigDecimal JavaDoc

In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown.

It looks like it stores numbers as an arbitrarily long sequence of digits, with the decimal point anywhere. That means it can't store exact values for n/3, n/7 etc. So maybe there's still an opportunity for you here...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.