Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 20 down vote accepted

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.

share|improve this answer
3  
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). –  William Shakespeare May 6 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 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 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 at 14:44
    
@Karl I don't know Perl but can you please clarify what is a sigil? –  Geek May 9 at 6:51

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.

share|improve this answer
2  
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 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 at 15:32
2  
This is just wrong. –  Andrew Medico May 6 at 18:49
1  
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 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 at 18: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.

share|improve this answer

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.

share|improve this answer
    
This allows concepts like illegal numbers and illegal primes - numerical representations of "illegal" programs. –  Andrew May 6 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 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 at 7:57
    
@david.pfx: A string when viewed as an array of characters is not scalar. –  MSalters May 6 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 at 10:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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