954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Idea for Value class with perfect accuracy

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?

tboz203
Newbie Poster
7 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

BigDecimal not work for you?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

tboz203
Newbie Poster
7 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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".

tboz203
Newbie Poster
7 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
but it does store an exact value, right?


Apparently not! This extract is from the BigDecimal JavaDocIn 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...

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: