wood burning stoves 2.0*
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Lost precision in Double variable" Watch "Lost precision in Double variable" New topic
Author

Lost precision in Double variable

Trish Hartnett
Greenhorn

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

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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Greenhorn

Joined: Apr 29, 2003
Posts: 18
hello,

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.

Trish.
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:
 
 
subject: Lost precision in Double variable