What is the literal expression of NaN in Neo4j Cypher? - neo4j

How do I express NaN as a literal in a Cypher query?
Situation
I have a NaN value in a database:
match (a) with max(a.CONCENTRATION) as m return m
will return
m
---
NaN
Cypher reference in Neo4j mentioned that this is possible as the result of special value being stored:
The special value java.lang.Double.NaN is regarded as being larger than all other numbers.
What I tried
However, now that it's in there, I don't know how to match them in search, because you get the following error
input
match (a) where a.CONCENTRATION = NaN return a limit 10
error
Variable `NaN` not defined (line 1, column 35 (offset: 34))
Other references
Cypher reference in Neo4j doesn't mention NaN literal, unless I missed it.
I've googled 'Cypher NaN' but the closest thing I got is how to add inf/NaN, which wasn't directly addressed (How to add infinity, NaN, or null values to a double[] property on a node in Cypher/Neo4j).

[UPDATE 2]
Neo4j 5.0 introduced:
the float literals Inf, Infinity, and NaN.
the isNaN() function for determining whether the specified value is NaN.
[ORIGINAL]
There is no way to specify the literal, but this should work:
MATCH (a)
WHERE TOFLOAT(a.CONCENTRATION) <> a.CONCENTRATION
RETURN a
LIMIT 10;
TOFLOAT() will return NULL if the argument cannot be converted (as needed) to a number. But, even if the argument can be converted, the result would not equal the argument unless it was numeric to begin with.
[UPDATE 1]
#chaserino's nice new answer prompted me to do a little more experimentation.
Although there is still no literal for NaN, Infinity, and -Infinity, I determined that Cypher can generate those values in neo4j version 3.4.0+ (I did not test earlier versions). You can then use those values for comparison purposes.
For example, this query shows how to generate those values:
RETURN 0.0/0.0, 1.0/0.0, -1.0/0.0
And here is the result:
╒═════════╤═════════╤══════════╕
│"0.0/0.0"│"1.0/0.0"│"-1.0/0.0"│
╞═════════╪═════════╪══════════╡
│NaN │Infinity │-Infinity │
└─────────┴─────────┴──────────┘
NOTE: For Infinity, you can actually use any positive numerator, and for -Infinity, you can use any negative numerator.

This works in Neo4j 3.5:
MATCH (a)
WHERE TOSTRING(a.CONCENTRATION) = 'NaN'
RETURN a
LIMIT 10;

Related

Obtaining numerical values from the mnewton function of the Maxima program

I use Maxima for calculations. I solve a system of nonlinear equations using Newton's method (mnewton()). I get the solution in the form of a list:
[[φ2=5.921818183272879,s=5.155870949147037]]
How to get the numerical value of the first (φ2) and second (s) unknown. If I substitute:
x: roz1[1][2]$
I get that x is equal to: s=5.155870949147037
What to do to make x equal to a numerical value only: 5.155870949147037
(without s=).
My code:
Maxima code
I have two ideas. (1) You can call rhs to return the right-hand side of an equation (likewise lhs for the left-hand side). E.g. rhs(s = 123) returns 123.
(2) You can call assoc to find the value associated with s (or any variable) in the mnewton results. E.g. assoc('s, [a = 1, b = 2, s = 3, u = 5]) returns 3.
I like (2) better since it is not necessary to know where in the list is the one that you're interested in.

Wrong value calculated by Delphi

I have a record declared as
T3DVector = record
X,Y,Z: Integer;
end;
One variable V of type T3DVector holds:
V.X= -25052
V.Y= 34165
V.Z= 37730
I then try to the following line. D is declared as Double.
D:= (V.X*V.X) + (V.Y*V.Y) + (V.Z*V.Z);
The return value is: -1076564467 (0xFFFFFFFFBFD4EE0D)
The following code should be equivalent:
D:= (V.X*V.X);
D:= D + (V.Y*V.Y);
D:= D + (V.Z*V.Z);
But this,however, returns 3218402829 (0x00000000BFD4EE0D), which actually is the correct value.
By looking at the high bits, I thought this was an overflow problem. When I turned on overflow checking, the first line halted with the exception "Integer overflow". This is even more confusing to me because D is Double, and I am only storing values into D
Can anyone clarify please ?
The target of an assignment statement has no bearing on how the right side is evaluated. On the right side, all you have are of type Integer, so the expression is evaluated as that type.
If you want the right side evaluated as some other type, then at least one operand must have that type. You witnessed this when you broke the statement into multiple steps, incorporating D into the right side of the expression. The value of V.Y * V.Y is still evaluated as type Integer, but the result is promoted to have type Double so that it matches the type of the other operand in the addition term (D).
The fact that D is a double doesn't affect the type of X, Y and Z. Those are Integers, and are apparently not large enough to store the squares of such large numbers, and their multiplication is what overflows. Why don't you declare them as doubles, too?

Erlang arithmetic operations on ets:select result

I have an ets table which you can imagine there are two columns, "Key" and Value. Value is an integer.
When I tried:
Ans = ets:select(Table_name, MS),
Ans + 1.
where Ans equals to the expected Value.
I got a error:
** exception error: an error occurred when evaluating an arithmetic expression
Can I not do any arithmetic operation on the ets:select return value?
in the ets module spec: select(Tab, MatchSpec) -> [Match], the reurn value is a list of match. If your match specification define one single integer value as return, you will receive a list of integer. In your case, it seems that this list will always have a length of 1 element, if it must be the case, you can write:
[Ans] = ets:select(Table_name, MS),
Ans + 1.
But beware that this code will crash if the returned list is empty or have more than 1 element.
ets:select(Table_name, MS)
will return result in a List.
So instead, the following would work:
[Ans] = ets:select(Table_name, MS),
Ans + 1.

Strange results being returned from vDSP_conv

I've been using the Accelerate framework to do some audio signal processing and I've been using the vDSP_conv function to perform some cross-correlations. Usually, the values returned look like this (left column is the array index, and right column is the value of the array at that index after being returned from vDSP_conv):
125001 1.576556
125002 1.523622
125003 1.439102
125004 1.593097
125005 1.171977
125006 0.020228
125007 -0.988876
125008 -1.526720
125009 -1.056652
125010 -0.181521
125011 -0.029592
125012 0.077848
125013 0.319371
125014 0.080034
125015 -0.629983
But sometimes the results look like this, for no discernible reason:
125001 65531903404620711577128764702720.000000
125002 271523249688835947415863891591168.000000
125003 253191001846134141440285462233088.000000
125004 197376212065818453160643396632576.000000
125005 247836891833411757917279954665472.000000
125006 203601464352748581549908776976384.000000
125007 193256115501319341596977567629312.000000
125008 55431884287617507551879029063680.000000
125009 -242471930502532513482802284462080.000000
125010 -259877560883016098488551924039680.000000
125011 -201496656800953613737511541014528.000000
125012 -240627419186810410707269384667136.000000
125013 -241660441463967832878539113234432.000000
125014 -169626548145197368918504628027392.000000
125015 -157041504634723839288379166425088.000000
I ran the program again after getting these results, and they went back to the original (correct) results. Has anyone else experienced this or have any ideas as to why it's happening?
Probably this is some overflow effect in one of the vectors you are correlating. As the specs say "The length of this vector must be at least N + P - 1." So e.g. if you are doing autocorrelation of vector A of length n, you should first create a vector of length 2*n (say A_extended), copy A into that, and do
vDSP_conv(A_extended, 1, A, 1, &result, 1, n, n)

Might Lua's length operator return a negative index?

The, well, special specification of Lua's length operator made me wonder whether Lua would be "allowed" to return a negative value in a situation like
#{[-5]=1,[-1]=3}
It says:
The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil;
n=-5 and n=-1 would meet this criterion in my example, right?
moreover, if t[1] is nil, n can be zero.
Right, it can be zero, but it's not guaranteed, right?
For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value.
This isn't the case here, so it doesn't apply.
If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).
This is the case here, so again, n=-5 and n=-1 would be valid return values, right?
Can I be entirely certain that Lua always returns 0 for the example table, or any other table containing only negative indices? If (hypothetically) I'd be writing a Lua interpreter and would return either of those values, would I be conforming with the specifications?
Edit
Obviously, the way Lua is implemented, it does not return negative values. I felt the length operator is somewhat underdocumented and I see that Lua 5.2's documentation has changed. It now says:
Unless a __len metamethod is given, the length of a table t is only defined if the table is a sequence, that is, the set of its positive numeric keys is equal to {1..n} for some integer n. In that case, n is its length. Note that a table like
{10, 20, nil, 40}
is not a sequence, because it has the key 4 but does not have the key 3.
So, it now talks about positive numeric keys, that's much clearer. I'm left wiser but not totally happy with the documentation. When it says the "length is only defined if the table is a sequence", it should also state that even if the table is not a sequence a value is returned, but the behavior is undefined. Also, this table looks pretty much like a sequence:
a = setmetatable(
{0},
{
__index = function(t,k)
return k < 10 and k or nil
end
}
)
i = 1
while a[i] do
print(a[i])
i = i+1
end
--[[ prints:
0
2
3
4
5
6
7
8
9
]]
print(#a)
-- prints: 1
However, this is becoming nitpicking as it's pretty clear that it wouldn't make sense to take into account what mess __index might make. And Stackoverflow is certainly not the place to complain about documentation that could be more precise.
As you have noted, the specification of the length operator has changed between 5.1 and 5.2.
Can I be entirely certain that Lua always returns 0 for the example table, or any other table containing only negative indices?
You can for the current reference implementation, which ensures that for ilen defined
function ilen (xs)
local i=0
while xs[i+1] do i=i+1 end
return i
end
we always have #xs >= ilen(xs) - see the definition of luaH_getn in the ltable.c source. But the specification now deliberately does not promise this behaviour: a conformant implementation can return nil or raise an exception for attempts to find the length of tables that are not sequences.
From the text in reference link. The answer is NO.
I think your confusing the fact that if a NIL is found then the length of the table is deemed to be position the NIL was found -1.
Therefore if t(1) is NIL then 1 - 1 = 0 so the table length is 0.
If the length of a table was 5 then the next position or t(6) IS or WOULD BE NIL
The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero.

Resources