Calling Java from Octave with double arguments loses precision - octave

I'm running into a strange issue calling a Java library from Octave. I'm using the JSON library from http://www.json.org/java/. Here's an example Java class:
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());
}
}
and the corresponding code in Octave (I'm using 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
When I compile and run the Java code, I of course get
{"wrapped":3.1415926,"primitive":3.1415926}
With Octave, on my OS X laptop, I get the same thing.
$ 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}
But on my Ubuntu 12.04 VM, with Octave installed from ppa:octave/stable, I get:
$ 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}
Somehow the java.lang.Double object turns into an integer, or at least the fractional part gets stripped off. What is going on here?

Related

Fixing GNU Octave without science repo in openSUSE

I am a user of openSUSE 13.2. While updating the openSUSE, the system stopped working properly (for example, unable to open firefox, or software management in yast2. Snapper does not work to fix that). So I used DVD openSUSE 13.2 to upgrade (update) and it managed to fix it. However, some applications such as GNU Octave is not working. I am able to open it and run it but it gives me errors with compiling example code. Errors such as missing libraries etc. I do not want to jump to openSUSE 15 with so much hassle with it. I just want to fix Octave for now. However, science repository for openSUSE 13.2 is no longer active. I have GNU Octave v. 3.8 and if possible I would like to update octave to latest version with all the octave packages available. How I do it without science repository available for openSUSE 13.2. Is it okay to use repo of other openSUSE version say SLE?
Below is a sample code:
x = -10:0.1:10;
c=pi
d=exp(0)
f1 = sin(x);
f2 = c.^(1/4)* x.*exp((-1/2)*x.^2);
plot(x,f1,'-r',x,f2,'-b')
title ("psi(x) at n=0 and n=1 for x = -10:0.1:10");
xlabel ("x");
ylabel ("psi(x)");
text (0.6, 1.2, "psi at n=0");
text (-0.6, -0.8, "psi at n=1");
legend ('psi-0(x)','psi-1(x)');
and the GNU Octave output is
error: /usr/lib64/octave/3.8.2/oct/x86_64-suse-linux-gnu/PKG_ADD: /usr/lib64/octave/3.8.2/oct/x86_64-suse-linux-gnu/__init_fltk__.oct: failed to load: /usr/lib64/octave/3.8.2/oct/x86_64-suse-linux-gnu/__init_fltk__.oct: undefined symbol: _ZN9Fl_Window6cursorE9Fl_Cursor
error: called from:
error: /usr/lib64/octave/3.8.2/oct/x86_64-suse-linux-gnu/PKG_ADD at line 6, column 1
GNU Octave, version 3.8.2
Copyright (C) 2014 John W. Eaton and others.
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-suse-linux-gnu".
For information about changes from previous versions, type 'news'.
warning: function /home/adam/OctaveExamples/waitbar.m shadows a core library function
warning: function /home/adam/OctaveExamples/plot3.m shadows a core library function
plot1
c = 3.1416
d = 1
error: default graphics toolkit 'fltk' is not available!
error: base_graphics_toolkit::initialize: invalid graphics toolkit
error: called from:
error: /usr/share/octave/3.8.2/m/plot/util/figure.m at line 94, column 9
error: /usr/share/octave/3.8.2/m/plot/util/gcf.m at line 63, column 9
error: /usr/share/octave/3.8.2/m/plot/util/newplot.m at line 113, column 8
error: /usr/share/octave/3.8.2/m/plot/draw/plot.m at line 219, column 9
error: /home/adam/OctaveExamples/plot1.m at line 6, column 1
the code was working before the openSUSE mulfunctioned. After the fix, octave seems to be broken.

source code of GNU Octave function: mldivide

Where can I find source code of mldivide of GNU Octave? Is it implemented as C++ code or is it m-file? Typing which mldivide in GNU Octave command line returns mldivide is a built-in function. I searched but I couldn't find the source code.
On octave 3.8.2 at least, you get a bit more information.
octave-cli-3.8.2:2> which mldivide
'mldivide' is a built-in function from the file libinterp/corefcn/data.cc
this file can be found on the octave repository. That specific function is on line 6083:
DEFUN (mldivide, args, ,
"-*- texinfo -*-\n\
#deftypefn {Built-in Function} {} mldivide (#var{x}, #var{y})\n\
Return the matrix left division of #var{x} and #var{y}.\n\
This function and #w{#tcode{x #xbackslashchar{} y}} are equivalent.\n\
#seealso{mrdivide, ldivide, rdivide}\n\
#end deftypefn")
{
return binary_op_defun_body (octave_value::op_ldiv, args);
}
If you are comfortable going through the headers yourself to track down where magic happens, you should clone the repository to do so. If not, the octave doxygen docs may be helpful but have little more than the method and class names.

ILMerge Error Windows Phone 8 Unresolved assembly reference

I am trying to merge some .net assemblies for Windows Phone 8 using ILMerge.exe (2.12.0803). On their own all assemblies work fine. I can successfully merge assembles which do not contain references to the Windows.Networking namespace as follows:
.\ILMerge.exe /lib:"%ProgramFiles(x86)%\Reference
Assemblies\Microsoft\Framework\WindowsPhone\v8.0"
/out:.\WP8\%1\MathToolbox.dll .\%1\MathBase.dll .\%1\PolyFunctions.dll
As soon as I try to include a DLL which uses the Windows.Networking namspace as follows:
.\ILMerge.exe /lib:"%ProgramFiles(x86)%\Reference
Assemblies\Microsoft\Framework\WindowsPhone\v8.0"
/out:.\WP8\%1\MathToolbox.dll .\%1\MathBase.dll .\%1\PolyFunctions.dll
.\%1\RemoteTools.dll
I am recieving the following exception:
An exception occurred during merging: Unresolved assembly reference
not allowed: Windows. at
System.Compiler.Ir2md.GetAssemblyRefIndex(AssemblyNode assembly) at
System.Compiler.Ir2md.GetTypeRefIndex(TypeNode type) at
System.Compiler.Ir2md.VisitReferencedType(TypeNode type) at
System.Compiler.Ir2md.VisitClass(Class Class) at
System.Compiler.Ir2md.VisitModule(Module module) at
System.Compiler.Ir2md.SetupMetadataWriter(String debugSymbolsLocation)
at System.Compiler.Ir2md.WritePE(Module module, String
debugSymbolsLocation, BinaryWriter writer) at
System.Compiler.Writer.WritePE(String location, Boolean
writeDebugSymbols, Module module, Boolean delayS ign, String
keyFileName, String keyName) at
System.Compiler.Writer.WritePE(CompilerParameters compilerParameters,
Module module) at ILMerging.ILMerge.Merge() at
ILMerging.ILMerge.Main(String[] args)
Having done some reading this exception suggets ILMerge is unable to locate the Windows.Networking assemblies. I have attempted adding various different flags to ILMERGE including:
.\ILMerge.exe /lib:"%ProgramFiles(x86)%\Reference
Assemblies\Microsoft\Framework\WindowsPhone\v8.0" /targetplatform:v4,"%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71"
/out:.\WP8\%1\MathToolbox.dll .\%1\MathBase.dll .\%1\PolyFunctions.dll
.\%1\RemoteTools.dll
But still get the same exception. If anyone has any suggestions that would be greatly appreciated.
I don't think ILMerge actually works with other than pure .NET assemblies, especially when Windows Phone 8 and WinRT projects uses referenced native libraries - that might be that not found "Windows" unresolved assembly.
The error indicates that ILMerge cannot find the platform's Windows.winmd. When compiling a Windows Phone or Windows Store project, you compile against two main things; The Platform SDK (WinRT) and a .NET SDK. Any tools that run over these assemblies will need both.
In addition to passing the .NET assembly folder, try passing "C:\Program Files (x86)\Windows Phone Kits\8.0\Windows Metadata directory" as a lib argument.
Make note, ILMerge is based on an older version of CCI (http://ccimetadata.codeplex.com/) so even after doing above, may still have trouble consuming the WinRT metadata format which is slightly different to .NET's.

Every Assembly program using the Win32 API's print function equivalents crashes on startup. How can I fix this?

I tried messing around with Win32 binaries lately (this is for a big project of mine).
So after some weeks of research, I now have a solid understanding of how Assembly works, how it is converted into binary code and how x86/x64 opcodes work.
The last piece to the puzzle is figuring out how to properly call Win32 API methods.
I actually asked a question on here in relation to this, and the answer I got was, I should try and compile an Assembly or C program that does this. So I went ahead and tried this in Assembly (I'm using FASM by the way):
format PE console
entry start
section '.idata' import data readable writable
include 'win32a.inc'
library kernel,'kernel32.dll'
import kernel,\
GetStdHandle,'GetStdHandle',\
WriteConsoleA,'WriteConsoleA'
section '.data' data readable writable
string db 'Hello!', 0h
output dd ?
section '.code' code readable executable
start: push -11
call GetStdHandle
pushd 0
pushd output
pushd 7
pushd string
pushd eax
call WriteConsoleA
This is one of the many versions of this code actually. The main problem is, when I call methods like "ExitProcess", generally other functions from the kernel32.dll library, things seem to work out. It's the IO functions that bug me...
I don't understand what's wrong with this code, I don't get any compile-time errors, though when I run it, it just crashes.
So my next idea was, since this didn't work, to try the same in C.
I'm using Cygwin as a compiler and linker...
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MessageBox(NULL, "Hello, world!", "Test", MB_OK);
return 0;
}
This code yielded the same result, the application crashed.
Now, I am not looking for any C/C++ code. My original question of interest was to know how calling extern library function looks like in x86/x64 binary (assembled) code. But I would be very thankful for any resources regarding this topic.
Thank you in advance.
-Tom S.
Your problem is that you need to call ExitProcess at the end to properly end the process. Since you are not doing that, the code currently will continue executing and eventually segfaults because it attempts to execute junk bytes.

How to use mole manually in Visual Studio Express?

I searched for tags "Moles" and "Visual Studio Express", and found no questions. That probably means I'm the only person trying to use Express with Moles :)
In Express, there are no options such as "Add moles assembly", or "Add mscorlib".
I've managed to get the tutorial for the DateTime Y2Kbug working after manually adding a couple of references that would normally be handled by VS. So, that means that I can mole any of the pre-moled stuff in System or mscorlib, which will be useful.
My problem is that I cannot seem to mole any of my own classes or methods in the SUT. I don't believe that the compiler sees any of the .moles file that contains the assemblies to mole.
I proved (?) this by attempting to exclude DateTime from the Y2kbug tutorial, using:
<MoleGeneration>
<Types>
<Remove FullName="DateTime!" />
</Types>
</MoleGeneration>
But the "bug" was asserted as usual :)
Is it possible to generate non-System moles as I need them by using something I saw in this forum: stackoverflow.com/questions/6272217/microsoft-moles-dynamically-instrument
Call Microsoft.Moles.Framework.Moles.MoleRuntime.SetMole(Delegate _stub, object _receiver, MethodInfo method);
If I use the "SetMole" method, does that mean that I don't need "assembly" files which are not seen anyway? If so, would it be possible to give some examples of the use of "SetMole". (I'm not a techie.)
As suggested by Mike Christian, I used moles.exe OrigValueP.dll. This created OrigValueP.Moles.dll in the MolesAssemblies subfolder.
Add OrigValueP.Moles.dll as a reference to the test project.
View OrigValueP.Moles in the Object Browser. I now see MOrigValue.AllInstances. So that's a huge step forward.
However, I don't see any Moles equivalent of my method:
public string TestString() { return "Original value"; }
[Deleted lines relating to the command, "moles.exe OrigValueP.Moles.dll", because MC suggested I'm "attempting to mole the mole assembly".]
So, I have one question (which I will continue to try to answer myself): Why was there no Moled method for TestString()?
The Moles documentation states Moles is compatible with Visual Studio 2008 Professional or Visual Studio 2010 Professional or better. http://research.microsoft.com/en-us/projects/pex/molestutorial.docx
Don't forget that executing the moles compiler via command line requires that you perform compilation in the following order:
1. Target assemblies
2. Moles (against the target assemblies)
3. Test assemblies
The Moles Manual provides the following command line instruction:
http://research.microsoft.com/en-us/projects/pex/molesmanual.docx
Code Generation Command Line The Moles framework includes a command-line application that can generate the code by following the
instructions in .moles files or directly from assemblies. By default,
the command-line tool compiles the stub types and mole types into an
assembly and places it into a MolesAssemblies subfolder. The following
list shows examples of typical usage.
Generate mole types and stub types for a particular
assembly moles.exe assembly.dll Generate mole types and stub types
from an existing .moles file moles.exe assembly.moles Generate
mole types and stub types under a particular namespace moles.exe
assembly .dll /nf:MyNamespace Get detailed help about usage of
moles.exe moles.exe help
[This whole answer has been appended to the original question. Sorry for all the mess.]