我遇到了一个从Octave调用Java库的奇怪问题。我正在使用http://www.json.org/java/中的JSON库。这是一个示例Java类:
import org.json.JSONObject;
public class Test {
public static void main(String[] args) throws Exception {
JSONObject obj = new JSONObject();
obj.put("primitive", 3.1415926);
obj.put("wrapped", new java.lang.Double(3.1415926));
System.out.println(obj.toString());
}
}
和Octave中的相应代码(我使用的是3.8.1):
function test()
javaaddpath('json.jar')
obj = javaObject('org.json.JSONObject');
obj.put('primitive', 3.1415926);
obj.put('wrapped', javaObject('java.lang.Double', 3.1415926));
disp(obj.toString());
end
当我编译并运行Java代码时,我当然得到了
{"wrapped":3.1415926,"primitive":3.1415926}
使用Octave,在我的OS X笔记本电脑上,我得到同样的东西。
$ octave --eval test
warning: function ./test.m shadows a core library function
GNU Octave, version 3.8.1
Copyright (C) 2014 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. For details, type 'warranty'.
Octave was configured for "x86_64-apple-darwin14.0.0".
Additional information about Octave is available at http://www.octave.org.
Please contribute if you find this software useful.
For more information, visit http://www.octave.org/get-involved.html
Read http://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.
{"primitive":3.1415926,"wrapped":3.1415926}
但是在我的Ubuntu 12.04 VM上,从ppa:octave/stable
安装了Octave,我得到了:
$ octave --eval test
warning: function ./test.m shadows a core library function
GNU Octave, version 3.8.1
Copyright (C) 2014 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. For details, type 'warranty'.
Octave was configured for "i686-pc-linux-gnu".
Additional information about Octave is available at http://www.octave.org.
Please contribute if you find this software useful.
For more information, visit http://www.octave.org/get-involved.html
Read http://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.
{"wrapped":3,"primitive":3.1415926}
不知何故,java.lang.Double
对象变为整数,或者至少小数部分被剥离。这是怎么回事?