Test integerness: Difference between revisions

Content added Content deleted
(C solution addition)
(jq)
Line 299: Line 299:
'''Example''':<lang j> isInt 3.14 7 1.4j0 4j0 5j3
'''Example''':<lang j> isInt 3.14 7 1.4j0 4j0 5j3
0 1 0 1 0</lang>
0 1 0 1 0</lang>
=={{header|jq}}==
{{works with|jq|1.4}}

jq does not have builtin support for complex numbers or rationals, but in conformity with
the Rosetta Code page [[Arithmetic/Complex#jq]], we shall assume in the following that the complex number x+iy
has been identified with the array [x,y]. To illustrate how the task can be solved for rationals,
we shall also identify the rational numbers p/q with JSON objects that have the form:
{"type": "rational", "p": p, "q": q}.
<lang jq>def is_integral:
if type == "number" then . == floor
elif type == "array" then
length == 2 and .[1] == 0 and (.[0] | is_integral)
else type == "object"
and .type == "rational"
and .q != 0
and (.q | is_integral)
and ((.p / .q) | is_integral)
end ;</lang>
'''Example''':
<lang jq>(
0, -1, [3,0], {"p": 4, "q": 2, "type": "rational"},
1.1, -1.1, [3,1], {"p": 5, "q": 2, "type": "rational"}
) | "\(.) => \(if is_integral then "integral" else "" end)"</lang>
{{out}}
<lang sh>$ jq -r -n -f is_integral.jq
0 => integral
-1 => integral
[3,0] => integral
{"p":4,"q":2,"type":"rational"} => integral
1.1 =>
-1.1 =>
[3,1] =>
{"p":5,"q":2,"type":"rational"} => </lang>


=={{header|ooRexx}}==
=={{header|ooRexx}}==