3

I'm having some problems with MATLAB and 64-bit integers. I want to have a mask equivalent to 2^63-1 (all ones except the MSB), but MATLAB just seems to round everything.

>> mask_fraction = uint64(9223372036854775807)

mask_fraction = 9223372036854775808 % This is 2^63 again, not 2^63-1!

Similarly,

  >> uint64(2^63)

    ans =  9223372036854775808       


    >> uint64(2^63-1)

    ans =  9223372036854775808

Another one of my attempts simply doesn't work:

>> uint64(2^63) - 1
??? Undefined function or method 'minus' for input arguments of type 'uint64'.

Thoughts?

horchler
  • 18,384
  • 4
  • 37
  • 73
Mewa
  • 502
  • 1
  • 9
  • 24
  • I observe the same behavior on a 32-bit system running Matlab 7 R14 – Buck Thorn Aug 04 '13 at 20:23
  • 1
    @BasSwinckels To check what operations are possible with a class uint64, try e.g. `methods(uint64(1))`. Compare that to plain `methods(1)` – Buck Thorn Aug 04 '13 at 20:26
  • On my system, `methods(uint64(1))` displays 73 methods including minus. What does it show for you and Mewa? This might be an issue of 32bit/64bit systems, but I would assume that they implemented subtraction on all systems ... – Bas Swinckels Aug 04 '13 at 20:36
  • @BasSwinckels On R14 there are 38 methods, no minus. They never got around to it? – Buck Thorn Aug 04 '13 at 21:46
  • @BasSwinckels, I don't have minus either, I have R2010a. My version is 64 bit. – Mewa Aug 05 '13 at 06:32
  • 1
    @TryHard @BasSwinckels Support for arithmetic on `int64` and`uint64` types was introduced in R2010b (for both 32- and 64-bit versions of MATLAB). – Sam Roberts Aug 05 '13 at 09:21

4 Answers4

4

@BasSwinckels correctly points out one issue. I'll address another.

The first non-exactly representable double-precision floating point integer is 2^53+1. When you pass in expressions to the uint64 function they are evaluated as doubles before being cast to uint64. If these expressions evaluate to a double precision integer that is not exactly representable, you'll see behavior like you described. This is exactly why uint64(2^63-1) and uint64(2^63) both return 9223372036854775808. All powers of two can be safely represented in double precision, so uint64(2^63)-1 or uint64(2^63)-uint64(1) are what you should use (once you figure out your other issue).

horchler
  • 18,384
  • 4
  • 37
  • 73
3

I don't see the problems you report. On my computer (Matlab R2012b on 64-bit Ubuntu12.04):

>> mask_fraction = uint64(9223372036854775807)

mask_fraction =

  9223372036854775807

>> uint64(2^63) - 1

ans =

  9223372036854775807

Do you maybe run an older Matlab version?

I also find the 'undefined function or method minus ...' error a bit suspicious. Do you have uint64 aliased to some other function? Try clear uint64 first ...

Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62
  • I have Matlab R2010a, so maybe it is a version issue. It seems like I'm not the only one having issues with the 'minus' error as shown by someone else in comments to my original post. There is no 'minus' method implemented if I type in methods(uint64(1)). – Mewa Aug 05 '13 at 06:31
1

It seems from the other comments that Matlab does not implement the minus method for class uint64 in some older versions. The ugly workaround below works for me in R2011b:

>> bitset(intmax('uint64'), 64, 0)

ans =

  9223372036854775807
Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62
  • Yeah, I used the int64 workaround as well, but a bit different. For future reference, this works: `mask_fraction = typecast(cast(2^63, 'int64'), 'uint64');` – Mewa Aug 05 '13 at 06:35
1

I don't know if it is supported by the version of MATLAB you are using but this might help

mask_fraction = uint64(intmax('int64'))

which returns

mask_fraction =
  9223372036854775807

2^63-1 is the maximum value for int64.

Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52
  • Yeah, I used the int64 workaround as well, but a bit differently. This worked for me: `mask_fraction = typecast(cast(2^63, 'int64'), 'uint64');` – Mewa Aug 05 '13 at 06:36