Skip to main content

6.3) The Numeric Class


The numeric class is a super-class used for both integers (whole numbers, e.g. -1, 0, 43, 2550) and floating point numbers (a number with a fractional part, e.g. -43.0, -2.5, 1/4., 1.26E-6). It comprises six different sub-classes for integers, and two more generic sub-classes, single and double.

By default, Matlab uses double-precision to store numerical values. The double class can represent numbers from -2E+308 to +2E+308.

There may be circumstances when you don't want to use the default precision:

  • when you are limited by computer memory (single-precision numbers require 32bits of memory whilst doubles are 64 bits). Note that single precision has less precision and range.
  • when your values are better represented by integers. For example, if you are generating array indices or representing image pixels with grey-scale values between 0 (black) and 255 (white)

In this case you might choose to store numbers at single precision. You do this when you define your number:

[matlab]
>> x=single(33.456)
[/matlab]

Or you can convert between different numeric classes:

[matlab]
>> B=single(A) % convert A to a single precision number
>> C=int8(A) % convert A to an 8-bit signed integer
[/matlab]