2

I'm using java and referring to the "double" datatype. To keep it short, I'm reading some values from standard input that I read in my code as doubles (I would much rather use something like BigInteger but right now it's not possible).

I expect to get double values from the user but sometimes they might input things like: 999999999999999999999999999.9999999999 which I think is beyond an accurate representation for double and get's rounded to 1.0E27 (probably).

I would like to get some pointers on how to detect whether a specific value would be unable to be accurately represented in a double and would require rounding (so that I could refuse that value or other such actions).

Thank you very much

Toon Krijthe
53.6k38 gold badges151 silver badges204 bronze badges
asked Nov 6, 2008 at 13:18

3 Answers 3

4

Most values entered by humans won't be exactly representable with a double.

For instance, do you want to prevent the user from entering 0.1? That's not exactly representable as a double.

To find the scale of the error, you could do something like:

BigDecimal userAsDecimal = new BigDecimal(userInput);
double userAsDouble = double.parseDouble(userInput);
BigDecimal doubleToDecimal = new BigDecimal(userAsDouble);
BigDecimal error = userAsDecimal.subtract(userAsDouble).abs();

Then check whether "error" is tolerably small.

answered Nov 6, 2008 at 13:21
Sign up to request clarification or add additional context in comments.

3 Comments

Indeed you are correct. To be more specific I'll always get positive numbers with at most 10 digits of precision, so my requirment would be to limit the whole part of the number so that it won't cause rounding
I'm afraid I still don't quite understand. Do you mean you'll only actually get whole numbers? If so, use a long. If not, I'm lost again...
I've had a bit of a problem formulating the request Thank you (especially) and everyone else who answered, double is really bad to use in my case ... which I've been complaining a bit for a while :).
2
double orig = GetSomeDouble();
double test1 = orig - 1;
if ( orig == test1 )
 SomethingIsWrong();
double test2 = test1 + 1;
if ( orig != test2 )
 SomethingIsWrong();
answered Nov 6, 2008 at 13:22

1 Comment

Thanks for the snippet .. it is a handy test and a good learning experience for me so to speak. I won't be using it probably because I'll eliminate infinite values .. but that's my problem :). Thanks again
1

You can't use a double to store something precisely in the way you're asking, very rarely a user may enter a number that a double can perfectly represent, but that would be coincidence only. If you need to store the exact number you could use a string so long as you don't need to do any manipulation. If you need more than that then there are probably libraries that are made for that kind of precise math.

answered Nov 6, 2008 at 13:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.