131

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.

Ryan
  • 1,439
  • 2
  • 10
  • 10
  • 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. – charles ross Nov 30 '17 at 14:49

6 Answers6

177

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.

Karl Bielefeldt
  • 146,727
  • 38
  • 279
  • 479
  • 15
    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 06 '14 at 03: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 06 '14 at 09:59
  • 2
    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. –  May 06 '14 at 14:43
  • 2
    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. –  May 06 '14 at 14:44
  • 1
    @Karl I don't know Perl but can you please clarify what is a *sigil*? – Geek May 09 '14 at 06:51
  • 2
    @Geek, it's a symbol you put before a variable, like `$variable`. See the [wikipedia page](http://en.wikipedia.org/wiki/Sigil_(computer_programming)). – Karl Bielefeldt May 09 '14 at 14:28
  • "It distinguishes a single value like an integer or float from a data structure like an array" Outside perl, an array could represent other "scalar" things, e.g. scalar fields. – 16807 Oct 04 '16 at 21:33
  • So what does it mean if a String is a scalar value? – Aaron Franke Jan 13 '19 at 02:55
  • @AaronFranke that the string is the sole object contained within a wrapper object meant to contain multiple, e.g. an array contains 1 string. The 1 string is read out as a scalar. – TylerH Oct 29 '20 at 19:46
36

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.

Jon Hanna
  • 2,115
  • 12
  • 15
  • 16
    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 06 '14 at 14:21
  • 2
    @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 06 '14 at 15:32
  • 10
    This is just *wrong*. – nobody May 06 '14 at 18:49
  • 3
    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 06 '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 06 '14 at 18:51
  • 3
    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 Apr 21 '15 at 04:01
  • 1
    @JonHanna this will help me remember what 'scalar' means... Thanks. – Luke W Jul 07 '17 at 16:51
  • @turboladen An integer is on a scale between -infinity and +infinity :) Or, more mathematically: An integer is a number in the set described by: {..., −2, −1, 0, 1, 2, ...} – Magne Oct 12 '18 at 11:14
  • This is wrong where it talks about strings. The definition of a scalar is not if it can live on a scale, but if _it can scale a vector_. That's why integers and other numbers are scalars: you can multiply a vector by an int, same as booleans even. But not by a string! What is (1,1) * "abcd"? Strings _are_ vectors. That's why people are saying this answer is wrong. For more info see https://en.wikipedia.org/wiki/Scalar_(mathematics) – hraban Oct 14 '19 at 13:42
  • I think this makes perfect sense as a supplemental "answer" to this question. However, you must first understand what a scale is. But on a scale of 1-10, I think that's pretty easy. Thanks @JonHanna – derick Apr 16 '20 at 01:25
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.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
14

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.

Bert
  • 241
  • 2
  • 3
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.

hotpaw2
  • 7,938
  • 4
  • 21
  • 47
  • 1
    This allows concepts like [illegal numbers](http://en.wikipedia.org/wiki/Illegal_number) and [illegal primes](http://en.wikipedia.org/wiki/Illegal_prime) - numerical representations of "illegal" programs. – Andrew May 06 '14 at 05:00
  • A scalar does not have to be numeric. A string, boolean or date/time is also a scalar. – david.pfx May 06 '14 at 06: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 06 '14 at 07:57
  • 1
    @david.pfx: A string when viewed as an array of characters is not scalar. – MSalters May 06 '14 at 10:01
  • 1
    @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 06 '14 at 10:59
  • Scaler is a (non-excluding) property of how one operates on the data (addition, comparison, etc.) But there may be other non-scaler ways to view or operate on any given chunk of data. So "what it means" depends on the observer or operator at some given point in time. – hotpaw2 May 20 '14 at 20:10
1

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
Premraj
  • 836
  • 10
  • 17