QforMortals3/Data Types 101
1.8 Data Types 101
There are q data types to cover nearly all needs but a few basic types are used most frequently. In q3.0 and above, the basic integer type, called long, is a 64 bit signed integer. If you write a literal integer as in the snippets above, q creates a 64 bit signed integer value.
The basic floating point type in q is called float, often known as a “double” in many languages. This is an 8 byte value conforming to the IEEE floating point specification.
Arithmetic operations on integer and float values are pretty much as expected except for division, which is written as (%) since ‘/’ has been preempted for comments (as well as other uses). Sorry, that’s just the way it is. Also note that division always results in a float.
q)2+3 5 q)2.2*3.0 6.6 q)4-2 2 q)4%2 2f
Boolean values in q are stored in a single byte and are denoted as the binary values they really are with an explicit type suffix ‘b’. One way to generate boolean values is to test for equality.
q)42=40+2 1b q)42=43 _
The two most useful temporal types are date and timespan; both represent integral counts. Under the covers, a date is the number of days since the millennium, positive for post and negative for pre.
q)2000.01.01 / this is actually 0 _ q)2014.11.19 / this is actually 5436 _ q)1999.12.31 / this is actually -1 _
Similarly, a time value is represented by a timespan, which is a (long) integer count of the number of nanoseconds since midnight. It is denoted as,
12:00:00.000000000 / this is noon _
One interesting and useful feature of q temporal values is that, as integral values under the covers, they naturally participate in arithmetic. For example, to advance a date five days add 5.
Or to advance a time by one microsecond (i.e., 1000 nanoseconds) add 1000.
q)2000.01.01=0 __ q)12:00:00.000000000=12*60*60*10000000000 _
The treatment of textual data in q is a bit complicated in order to provide optimum flexibility and performance. For now, we will focus on symbols, which derive from their namesake in Scheme and are akin to VARCHAR in SQL or strings in other languages. They are not what q calls strings! Think of symbols as wannabe names: all q names are symbols but not all symbols are names. A symbol is atomic, meaning that it is viewed as an indivisible entity (although we shall see later how to expose the characters inside it).
Symbols are denoted by a leading back-quote (called “back tick” in q- speak) followed by characters. Symbols without embedded blanks or other special characters can be entered literally into the console.
q)`aapl _ q)`jab _ q)`thisisareallylongsymbol _
Since symbols are atoms, any two can be tested for equality.
Reprinted with the author's permission from: q for Mortals Version 3, An Introduction to Q Programming by Jeffry A. Borror.
©2015 Jeffry A. Borror/ q4m LLC