> What if the user enters in "1.2000000476837158"? Wouldn't they be pissed off
> if the program thought their number was "stupid" and changed it to "1.2"?
A good point, but a highly unlikely scenario in this program. Especially
considering that a user should have no expectation of 16 significant digits.
In this case, anything the user inputs after the 8th decimal point renders
the same binary representation.
> > (I realize I can just leave the field as text, and store the text in the
> > database, parsing the value when it is used. I would like to avoid this
> > because I don't think that storing a number as a string in the database is
> > particularly elegant.)
>
> While not elegant, at least the solution will be correct.
True enough.
I have also considered counting the number of digits the user inputs to the
right of the decimal point. The input precision could be stored, and then
resurrected as a dynamic rounding limit. Again, a lot of work for something
that seems simple.
The other approach that might work capitalizes on the fact that the floating
point representation of a number is always within one bit of the true value.
So, the true value is bounded by floating point values one bit higher and
lower than the given representation. How can these bounds be compared to
figure out the true value?
This basically amounts to losing a bit of precision... Compare the 31 bit
representation of the number to the 32 bit representation, and keep
everything where the two representations are the same. 31 bits is more than
enough for these purposes.
> Are all your numbers rational (i.e. as opposed to real numbers?) If so,
> you might want to look at java.math.BigDecimal.
Well, it is impossible for a human to type in an irrational number as input
(who would have the time (:) so that would work. However, it would still
require me to store a string of integers in the database. So, I might as
well stick with a simpler solution.
Rob
Thomas Schodt - 27 Sep 2005 16:37 GMT
> The other approach that might work capitalizes on the fact that the floating
> point representation of a number is always within one bit of the true value.
[quoted text clipped - 6 lines]
> everything where the two representations are the same. 31 bits is more than
> enough for these purposes.
A double is 64 bit wide (1 sign, 11 exponent, 52 mantissa).
Rob McDonald - 27 Sep 2005 17:16 GMT
> > The other approach that might work capitalizes on the fact that the floating
> > point representation of a number is always within one bit of the true value.
[quoted text clipped - 8 lines]
>
> A double is 64 bit wide (1 sign, 11 exponent, 52 mantissa).
Ok, but the point still holds. (and maybe my precision is being truncated
by the storage in MySql.)
A 64 bit double is only accurate to the 52'nd bit of the mantissa. If I
created a 63 bit floating point number, (1,11,51) it would only be slightly
less accurate than the 64 bit number, and comparing the two values may shed
light on the 'smart' formatting that I want.
Rob
Oliver Wong - 28 Sep 2005 17:42 GMT
>> What if the user enters in "1.2000000476837158"? Wouldn't they be pissed
> off
[quoted text clipped - 5 lines]
> In this case, anything the user inputs after the 8th decimal point renders
> the same binary representation.
If you want your software to be robust, it must perform within
specifications even in "highly unlikely scenarios".
> I have also considered counting the number of digits the user inputs to
> the
> right of the decimal point. The input precision could be stored, and then
> resurrected as a dynamic rounding limit. Again, a lot of work for
> something
> that seems simple.
This will fail if the stored number of significant digits is greater
than the number of digits which are significant in the decimal
representation of a float or double. E.g. if the user enters in a number
with 20 significant digits after the decimal point, you might store the fact
that there's 20 significant decimal digits after the decimal point, but as
soon as you're storing the original value entered by the user itself in a
double, you no longer have those 20 digits.
But yes, this is a lot of work when you could just, for example, store
the numbers as BigDecimals or as Strings.
> The other approach that might work capitalizes on the fact that the
> floating
[quoted text clipped - 3 lines]
> lower than the given representation. How can these bounds be compared to
> figure out the true value?
Impossible. There are an infinite number of rational values between your
two bounds, any of which could have been the "true value".
> This basically amounts to losing a bit of precision... Compare the 31 bit
> representation of the number to the 32 bit representation, and keep
> everything where the two representations are the same. 31 bits is more
> than
> enough for these purposes.
You might need to go back and look at your product's requirements.
Floating points (i.e. float and double) are great when precision isn't
critical. Not so great when rounding errors can lead to disaster (e.g.
dealing with financial data). What do you need for your program to work?
>> Are all your numbers rational (i.e. as opposed to real numbers?) If
> so,
[quoted text clipped - 5 lines]
> require me to store a string of integers in the database. So, I might as
> well stick with a simpler solution.
I vaguely recall Java's BigDecimal type to map cleanly onto SQL's
DECIMAL type. You'd have to consult your documentation to verify this
though.
- Oliver