What does it mean when data is scalar?

I don't know what scalar means exactly, but I'm trying to see if I'm thinking about it correctly. Does scalar relate to arbitrariness where the type of data could be any type, or a system is not able to know what the data is in advance.

-

The term "scalar" comes from linear algebra, where it is used to differentiate a single number from a vector or matrix. The meaning in computing is similar. It distinguishes a single value like an integer or float from a data structure like an array. This distinction is very prominent in Perl, where the `\$` sigil (which resembles an 's') is used to denote a scalar variable and an `@` sigil (which resembles an 'a') denotes an array. It doesn't have anything to do with the type of the element itself. It could be a number, character, string, or object. What matters to be called a scalar is that there is one of them.

-
This is a good example of how the same term may mean different things in different contexts. In languages like C or Pascal, scalar refers to simple "building block" types like `int` or `char`, not structured types like objects (even if there's only one). – Caleb May 6 '14 at 3:14
@Caleb: I'd disagree, the term "scalar" in C context is not well defined and may very well cover `int*`. The pointer is a scalar (single value), after all. – MSalters May 6 '14 at 9:59
I would also add that in addition to arrays, a scalar is not a compound data structure such as a struct or class. The difference being an array is multiple elements of the same type, while a structure is multiple elements of arbitrary types. – Snowman May 6 '14 at 14:43
Also, a string could be a non-scalar value. Languages such as Perl treat it as a scalar, but in any C-based language I would argue it is an array or object. – Snowman May 6 '14 at 14:44
@Karl I don't know Perl but can you please clarify what is a sigil? – Geek May 9 '14 at 6:51

A Scalar is simply a variable that holds an individual value. For purposes of this discussion, let's assume that a scalar is a single number, rather than a collection of numbers.

For example the result of a SQL query that returns a number instead of a tuple, as does the ExecuteScalar() method in the SQLCommand class, which returns the value of the first column of the first row in the result set returned by the query. It's typically used to retrieve an aggregate value such as a COUNT or AVERAGE, the ID of a new record, or the number of records processed by a query.

-

A supplemental mnemonic, to Karl Bielefeldt's great answer:

A simple way of thinking about it, is "can this be on a scale?"

An integer can be on a scale.

A fixed-size integer can be on a scale, e.g. from -2147483648 to 2147483647.

A real number can be on a scale.

A character, boolean, or fixed-precision decimal can all be on a scale. Even a string can be on a scale (we use such in sorting).

Hence "scalar".

A database row cannot be on a scale. A complex number cannot be on a scale. An object representing an email message cannot be on a scale. An array, vector or matrix cannot be on a scale.

-
If a string can be on a scale, why not an array? "can this be on a scale?" does not give intuitive results, for me at least. – VBCPP May 6 '14 at 14:21
@VBCPP where on a scale containing {1, 2} does {2, 1} sit? Before or after? Now, granted we can enforce an ordering, but there isn't a clear ordering. Bear in mind also, that I'm trying to offer an easy way to think about it (Karl Bielefeldt's answer does a good job at giving a more precise answer IMO, and I wouldn't try to better it, just suppplement). That said, there is an element of context here. A string may or may not be considered scalar depending on context; in SQL it's scalar for a good reason, in C it isn't, also for a good reason. – Jon Hanna May 6 '14 at 15:32
This is just wrong. – Andrew Medico May 6 '14 at 18:49
I disagree that it's "just wrong", obviously, though I think Karl Bielefeldt should have the tick. This is a mnemotic that's too long to be a comment, that is the better answer. – Jon Hanna May 6 '14 at 18:50
Hmm. I can't delete an accepted answer. I only want to delete it because it's accepted! – Jon Hanna May 6 '14 at 18:51

A scalar is a simple single numeric value (as in 1, 2/3, 3.14, etc.), usually integer, fixed point, or float (single or double), as opposed to an array, structure, object, complex vector (real plus imaginary or magnitude plus angle components), higher dimensional vector or matrix (etc.) data type that contains more than one single numeric value.

However, note that a large very complex data type of the sort that can also be represent in 8-bit bytes of computer memory can also be represented as one single very long/large binary scalar number. Turing used this technique to represent entire computer programs as just one scalar number.

-
This allows concepts like illegal numbers and illegal primes - numerical representations of "illegal" programs. – Andrew May 6 '14 at 5:00
A scalar does not have to be numeric. A string, boolean or date/time is also a scalar. – david.pfx May 6 '14 at 6:42
So what if I make a date complex class based on fields of several scalars (for instance an int for the month, one the year and one for the day)? – Pierre Arlaud May 6 '14 at 7:57
@david.pfx: A string when viewed as an array of characters is not scalar. – MSalters May 6 '14 at 10:01
@MSalters: Of course, and an integer viewed as an array of bits isn't either. Anything that can be stored and retrieved as a single immutable anonymous value is a scalar and most languages allow you to do that with string, date, complex, point etc, even if they can also be viewed in other ways. – david.pfx May 6 '14 at 10:59

As is the case with many terms in computing; the origin of the word relates to more physical properties. The term Scalar is relatively old in computing. Its definition is less strict these days. When you store data in computer memory this data can either fit in one address (1 byte*) or not. When it did, it was called scalar, when it didn't it was called a composite. Mainly because CPUs could only handle one address/piece of data (= 1 byte) at a time. As stated by @Karl Bielefeldt; the term was indeed taken from algebra.

We call a string a string because it is a string of chars. A char is/was a scalar, while a string is/was a composite. Storing 1 piece of data (a datum) in multiple addresses blurred the line somewhat. Think of it like this: When a CPU could process a datum in one instruction, it was scalar.

These days a scalar is any singular value, and what a singular value is may be defined differently amongst different languages. integers, floats, chars, strings, booleans and enums are -for the most part- considered scalars these days. Arrays, lists, trees, objects, etc. are not.

(* I say 1 byte to keep things clear, but technically I am talking about the days when 6 bits were more commonly used on punch cards for example and later on magnetic strips)

Disclaimer: I am unable to find any references of this on the internet, I got the information at school and from old books, amongst which (I think): Mathematical Tables and other Aids to Computation from 1944. That being said, my memory is not what it used to be, so if anyone can amend/confirm or deny my answer, it would be nice.

-

The word scalar derives from the Latin word scalaris, an adjectival form of scala (Latin for "ladder"). The English word "scale" also comes from scala. Source

A Scalar is a variable that holds an individual value.

For example:

Scalar Variable: Say that you are trying to represent the names of various students as a set of variables. Each of the individual variables is a scalar variable as follow

``````NAME01="Zara"
``````UCASE() - Converts a field to upper case