Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Java arithmetic

Subject: Java arithmetic

From: Luis

Date: 6 Nov, 2012 19:50:08

Message: 1 of 5

How can I call the Java primitive operators from Matlab? For example, I would like to determine what the Java answer to "9576890767L * 12345L". I think all I need to do this is access to the Java binary operator for long multiplication. Can this be done directly from Matlab?

Clearly one way around it is to create a small static Java class that exposes all the relevant operators as static methods. Is this the only way to access these operations from Matlab?

Thanks.

Subject: Java arithmetic

From: Yair Altman

Date: 7 Nov, 2012 07:04:08

Message: 2 of 5

"Luis O'Shea" wrote in message <k7bplg$ejp$1@newscl01ah.mathworks.com>...
> How can I call the Java primitive operators from Matlab? For example, I would like to determine what the Java answer to "9576890767L * 12345L". I think all I need to do this is access to the Java binary operator for long multiplication. Can this be done directly from Matlab?
>
> Clearly one way around it is to create a small static Java class that exposes all the relevant operators as static methods. Is this the only way to access these operations from Matlab?
>
> Thanks.

AFAIK this can only be done in a Java class.

Yair Altman
http://UndocumentedMatlab.com
Read my Matlab-Java programming book
 

Subject: Java arithmetic

From: Steven_Lord

Date: 7 Nov, 2012 14:56:04

Message: 3 of 5



"Luis " <blue.loshea.red@removethisandcolors.gmail.com> wrote in message
news:k7bplg$ejp$1@newscl01ah.mathworks.com...
> How can I call the Java primitive operators from Matlab? For example, I
> would like to determine what the Java answer to "9576890767L * 12345L". I
> think all I need to do this is access to the Java binary operator for long
> multiplication. Can this be done directly from Matlab?
>
> Clearly one way around it is to create a small static Java class that
> exposes all the relevant operators as static methods. Is this the only
> way to access these operations from Matlab?

Looking at the description of Java's LONG data type, the MATLAB equivalent
seems to be the int64 class.

http://www.mathworks.com/help/matlab/ref/int64.html

We added native 64-bit integer arithmetic in release R2010b. From the
Release Notes:

"Core MATLAB arithmetic functions now support int64 and uint64 classes
natively. Functions added are plus (+), minus (), uminus (), times (.*),
rdivide (./), ldivide (.\), power (.^), rem, mod, bitcmp, any, all, sum,
diff, colon (:), sign, accumarray, and bsxfun."

Since then we have added additional functions that are supported on 64-bit
integers (the Release Notes specifically mention the bitwise functions
BITAND, BITOR, etc. in release R2012b but there may have been others I don't
remember off the top of my head.)

When I perform the computation in the first line of your message with INT64
and with Symbolic Math Toolbox's arbitrary-precision arithmetic
functionality I receive the same result.

>> int64(9576890767)*int64(12345)
ans =
      118226716518615

>> sym(9576890767)*sym(12345)
ans =
118226716518615

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Java arithmetic

From: Luis O'Shea

Date: 7 Nov, 2012 16:33:08

Message: 4 of 5

"Yair Altman" wrote in message <k7d158$rhg$1@newscl01ah.mathworks.com>...
> "Luis O'Shea" wrote in message <k7bplg$ejp$1@newscl01ah.mathworks.com>...
> > How can I call the Java primitive operators from Matlab? For example, I would like to determine what the Java answer to "9576890767L * 12345L". I think all I need to do this is access to the Java binary operator for long multiplication. Can this be done directly from Matlab?
> >
> > Clearly one way around it is to create a small static Java class that exposes all the relevant operators as static methods. Is this the only way to access these operations from Matlab?
> >
> > Thanks.
>
> AFAIK this can only be done in a Java class.

As far as I can tell when you call Java methods from Matlab that return a long (say) they behave in Matlab as if they returned a double. In my case this was a show-stopper. In the end I solved my problems by using John D'Errico excellent Variable Precision Integer Arithmetic library (http://www.mathworks.com/matlabcentral/fileexchange/22725-variable-precision-integer-arithmetic).

Subject: Java arithmetic

From: Luis O'Shea

Date: 7 Nov, 2012 16:48:10

Message: 5 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <k7dsq4$2ab$1@newscl01ah.mathworks.com>...
>
> When I perform the computation in the first line of your message with INT64
> and with Symbolic Math Toolbox's arbitrary-precision arithmetic
> functionality I receive the same result.
>
> >> int64(9576890767)*int64(12345)
> ans =
> 118226716518615
>
> >> sym(9576890767)*sym(12345)
> ans =
> 118226716518615

Thanks Steve. I gave a random (and bad example) in my original post. I should have given an example that illustrated Matlab's "saturation" semantics for integer types, as opposed to modular arithmetic. Compare

>> intmax('int64') + int64(1) - intmax('int64')
ans =
                    0

with
    System.out.println((9223372036854775807L + 1L) - 9223372036854775807L); // prints 1

Thanks!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us