libsvm icon indicating copy to clipboard operation
libsvm copied to clipboard

Java: Error in number formatting during scaling

Open hmf opened this issue 8 years ago • 0 comments

I have executed the following two commands:

$java -cp libsvm.jar svm_scale -l 0 -y -1.0 1.0 -s /tmp/SVMScaleRange_341a7b72-40d9-420f-8a9c-f67e47b65eaa /home/hmf/git/adw/data/libsvm/a1a > a1a.scaled
$java -cp libsvm.jar svm_scale -l 0 -y -1.0 1.0 -r /tmp/SVMScaleRange_341a7b72-40d9-420f-8a9c-f67e47b65eaa /home/hmf/git/adw/data/libsvm/a1a.t > a1a.t.scaled
Exception in thread "main" java.lang.NumberFormatException: For input string: "-1,000000000000000"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at svm_scale.run(svm_scale.java:251)
    at svm_scale.main(svm_scale.java:348)

The problem is that in svm_scale:288 a formatter is being used:

Formatter formatter = new Formatter(new StringBuilder());

So the current locale is used. In my case this means that floating point numbers use a comma as decimal separator. So a comma is written but parsing expects a period. I think we have several options here:

  1. Change the use of the code to use a simple toString() conversion (issues with precision?)
  2. Force the locale using a String formatter (see [1])
  3. Force the applications locale (see [1])
  4. Use the constructor Formatter(Locale l, Appendable a) and use Locale.US

I think (4) is trivial and the way to go.

TIA

[1] https://stackoverflow.com/questions/5236056/force-point-as-decimal-separator-in-java

hmf avatar Jul 14 '17 12:07 hmf