MATLAB: Why does matlab use 8 bytes to memorize a number like 1 for example

chardoublenumbe

hi,
matlab uses 8 bytes to memorize a number like 1 as double as 2 bytes to memorize it as char?

Best Answer

By default if you do not tell MATLAB what datatype to use for a number, it uses IEEE 754 Double Precision floating point representation, which is described in more detail at https://en.wikipedia.org/wiki/IEEE_754
When you use char, MATLAB stores the values in UTF-16, which is representation that uses a minimum of two bytes per code point, with a system for indicating that more bytes may be needed. https://en.wikipedia.org/wiki/UTF-16 Code points up to roughly 55295 can be represented in two bytes (and there are another 8192 later ones with direct representation). This includes characters up to 힣 but does not include some alphabets such as Linear B or Phonecian. https://en.wikipedia.org/wiki/Plane_(Unicode)#Supplementary_Multilingual_Plane -- it is enough to hold most living languages.
Character manupulation and storage is a lot easier when you (usually) do not need to worry about the number of bytes needed to represent characters. Single-byte characters would miss out on a lot of commonly used characters -- for example the Greek characters do not start until about code position 913.