Does anyone know what is used to represent a null internally in q for the various integer and real types? The Borror book alludes to NaN but doesn’t elaborate. I know infinities are represented by the biggest and smallest values that can fit in a given type.
Thanks for any info.
q){x!0b vs’ x} -0W -1 0 1 0W 0N
-0W| 1000000000000000000000000000000000000000000000000000000000000001b
-1 | 1111111111111111111111111111111111111111111111111111111111111111b
0 | 0000000000000000000000000000000000000000000000000000000000000000b
1 | 0000000000000000000000000000000000000000000000000000000000000001b
0W | 0111111111111111111111111111111111111111111111111111111111111111b
| 1000000000000000000000000000000000000000000000000000000000000000b
Mohammad Noor
Nulls:
short: 0xFFFF80000
int: 0x80000000
long: 0x8000000000000000
float: log(-1.0) on Windows or (0/0.0) on Linux
Infinity:
short: 0x7FFF
int: 0x7FFFFFFF
long: 0x7FFFFFFFFFFFFFFF
float: -log(0.0) in Windows or (1/0.0) on Linux
source: http://code.kx.com/wiki/Cookbook/InterfacingWithC
http://kx.com/q/c/c/k.h
see section
// nulls(n?) and infinities(w?)
Thank you for the quick replies guys. So does a null then use the same representation that’s used for NaN on the Intel architecture et al.? Thanks.
They are magic numbers on KDB so they are architecture independent (except for the float differences between Windows and Linux).
Just to clarify, a null in KDB is a representation of what IEEE defines as a NaN. As KDB is a dynamically typed language, you have different null representations for the different datatypes. Does this answer your question?
Yes it does Tiago. Thanks for the follow up!
Yes, indeed for float/real “it is” NaN
(there are multiple NaNs
)
For short/int/long it is “INT_MIN”, the most negative, weird number
(http://en.wikipedia.org/wiki/Two’s_complement)
This is a brilliant choice for multiple reasons as far as im concerned
- as it gets rid of the asymmetry of the integer ranges (more negatives than positives)
also this way -0W=neg 0W in hardware
- and the only weird" exception is used for something meaningful
and the hardware already does this for example: 0N=neg 0N
- potentially easy to ignore in a sum, as 0=0N+0N (so just extend to have an even number of 0N in any sum)
(Im sure there are other advantages)
Cheers,
Attila
I get the following results with my 32-bit version:
q)0Wh-1 / short infinity
32766
q)0Wi-1 / int infinity
2147483646
q)0Wj-1 / long infinity
9223372036854775806
q)0W-1 / supposed to be same as int infinity?
9223372036854775806
The first three results were what I expected. But I was under the impression that 0W was identical to an int infinity value and not a equivalent to long infinity. Is this not the case?
Thanks for any additional info!
since 3.0, the default integer size for int literals is 64bit
so 0W is interpreted as 0Wj
Regardless of 32bit or 64bit q, the default type for integers is 64bit.
q)type 0