This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Lost precision in Double variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Lost precision in Double variable" Watch "Lost precision in Double variable" New topic

Lost precision in Double variable

Trish Hartnett

Joined: Apr 29, 2003
Posts: 18
Hello all,

Thank you for taking the time to read my post

I was wondering if any of you might have come across something like this

I'm trying to add key value pairs into a SortedMap, where the key is a
The key is usually a number like 0.01042 which has 6 digit precision.
SortedMap newMap = new TreeMap();
newMap.put(key, value);

The problem is that when I try to do something like
Object key = newMap.getFirstKey();
Double keyDouble = Double.getValue(key);
double timeStamp1 = firstKey.doubleValue();

the timeStamp1 value when printed out is only 1 decimal place precision

Can anyone suggest anything please ? Thanks in advance for any suggestions.

Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Double keyDouble = Double.getValue(key);

How are you getting that to compile? Have you redefined Double somewhere?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Double has no getValue() method, so it's hard for us to see exactly what you're doing here. If you can cut and paste the relevant bit of real code that'd be great.

[Jess in Action][AskingGoodQuestions]
Trish Hartnett

Joined: Apr 29, 2003
Posts: 18

thank you both for replying.
Sorry about the line saying "Double keyDouble = Double.getValue(key);" - that was me getting muddled with something.

What I'm trying to do is:
Double firstKey = (Double)firstRow.firstKey();
double timeStamp1 = firstKey.doubleValue();
Double secondKey = (Double)secondRow.firstKey();
double timeStamp2 = secondKey.doubleValue();
double interval = timeStamp1 - timeStamp2;

where firstRow and secondRow are 2 separate TreeMaps. Each TreeMap contains a timestamp (key) and a number(value). I want to subtract the timestamps to see if there is a particular interval between them. The problem is that the timestamps should be something like 376222.010420 and 37622.020837 (0.010420 apart).

Thanks again, any suggestions gratefully received.

Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

One of the tricks to debugging is to forego your assumptions. Verify that you're actually getting the values you think you are. Example:
I agree. Here's the link:
subject: Lost precision in Double variable
Similar Threads
Can you tell what is wrong with this loop?
Implicit Narrowing When Assigning Literals
problem related to floats
Why 100 * A * B * C is not same as A * B * C * 100 if A, B, C are double?