136

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.

1
  • Fun fact: The XML platform I spend my days in recently (using XQuery) likes to call these Atomic values. I think this is closely related, adding comment for reference/search terms. Commented Nov 30, 2017 at 14:49

6 Answers 6

186

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.

9
  • 17
    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
    Commented May 6, 2014 at 3:14
  • 3
    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.
    – user22815
    Commented May 6, 2014 at 14:43
  • 5
    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.
    – user22815
    Commented May 6, 2014 at 14:44
  • 2
    @Karl I don't know Perl but can you please clarify what is a sigil?
    – Geek
    Commented May 9, 2014 at 6:51
  • 3
    @Geek, it's a symbol you put before a variable, like $variable. See the wikipedia page. Commented May 9, 2014 at 14:28
38

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.

11
  • 18
    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
    Commented May 6, 2014 at 14:21
  • 3
    @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
    Commented May 6, 2014 at 15:32
  • 12
    This is just wrong.
    – nobody
    Commented May 6, 2014 at 18:49
  • 5
    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
    Commented May 6, 2014 at 18:50
  • 4
    I think the "it's just wrong" comment comes from the idea that the answer doesn't make any sense and your response to @VBCPP 's question doesn't answer his question. I sure like the idea of the mnemonic, but you still need to explain the significance of, for example, an integer being on a scale. Might as well be talking about putting a cat in the TV.
    – turboladen
    Commented Apr 21, 2015 at 4:01
33

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.

0
15

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.

1
  • I think the claims about data fitting in one 'address' (which I take to mean 'memory cell', the thing the address refers to) could do with citation, but +1 for "what a singular value is may be defined differently amongst different languages". The fact that 'a single value' may differ between languages and thus the term 'scalar' can be somewhat context-sensitive is something various other answers have overlooked. (I'll also give you a hypothetical bonus point for having a Blake's 7 themed user icon.)
    – Pharap
    Commented Jan 21 at 3:23
10

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 flattened and represented 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.

6
  • 1
    This allows concepts like illegal numbers and illegal primes - numerical representations of "illegal" programs.
    – Andrew
    Commented May 6, 2014 at 5:00
  • A scalar does not have to be numeric. A string, boolean or date/time is also a scalar.
    – david.pfx
    Commented May 6, 2014 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)? Commented May 6, 2014 at 7:57
  • 2
    @david.pfx: A string when viewed as an array of characters is not scalar.
    – MSalters
    Commented May 6, 2014 at 10:01
  • 2
    @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
    Commented May 6, 2014 at 10:59
2

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"
NAME02="Qadir"
NAME03="Mahnaz"
NAME04="Ayan"
NAME05="Daisy"

Scalar Functions: SQL scalar functions return a single value, based on the input value.

UCASE() - Converts a field to upper case
LCASE() - Converts a field to lower case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.