Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Lost precision in Double variable

Trish Hartnett
Greenhorn
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
before.

I'm trying to add key value pairs into a SortedMap, where the key is a
Double.
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
(0.0).

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

Trish.

Steve Morrow
Ranch Hand
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
Marshal
Posts: 24212
35
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.

Trish Hartnett
Greenhorn
Posts: 18
hello,

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.

Trish.

Steve Morrow
Ranch Hand
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: