You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Marcos Mendez <ma...@jitisoft.com> on 2013/05/08 23:55:55 UTC

atomic updates w/ double field

Hi,

I'm using solr 4.0 and I'm using an atomic update to increment a tdouble 3 times with the same value (99.4). The third time it is incremented the values comes out to 298.20000000000005. Has anyone seen this error or how to fix it? Maybe I should use the regular double instead of a tdouble?

1 x "weight_td":{"set":0.0}

 3 x "weight_td":{"inc":99.4}

Schema information:

<dynamicField name="*_d"  type="double"          indexed="true"  stored="true"/>
<dynamicField name="*_td"    type="tdouble"  indexed="true"  stored="true"/>
<fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="tdouble" class="solr.TrieDoubleField" precisionStep="8" positionIncrementGap="0"/>



Re: atomic updates w/ double field

Posted by Chris Hostetter <ho...@fucit.org>.
: I'm using solr 4.0 and I'm using an atomic update to increment a tdouble 
: 3 times with the same value (99.4). The third time it is incremented the 
: values comes out to 298.20000000000005. Has anyone seen this error or 
: how to fix it? Maybe I should use the regular double instead of a 
: tdouble?

this is the general nature of floating point math in most langauges -- 
including java...

http://stackoverflow.com/questions/322749/retain-precision-with-doubles-in-java
http://www.ibm.com/developerworks/java/library/j-math2/index.html

: 1 x "weight_td":{"set":0.0}
: 3 x "weight_td":{"inc":99.4}

public final class Temp {
    public static double val = 0.0D;

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println(i + ") " + val);
            val += 99.4;
        }
    }
}
// OUTPUT...
// 0) 0.0
// 1) 99.4
// 2) 198.8
// 3) 298.20000000000005
// 4) 397.6





-Hoss