Spelling of ordinal numbers: Difference between revisions

→‎{{header|jq}}: check_ok etc
(→‎{{header|jq}}: check_ok etc)
Line 841:
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Also workworks with gojq and fq, the Go implementations'''
 
One point of interest in the following is that the program checks not
only that its integer input is within the scope of the program but
also that it is within the capability of the C or Go platform.
In particular, `check_ok` checks for the platform's precision of
integer arithmetic.
 
For further remarks on this point, see the comments that
are included in the test data.
<syntaxhighlight lang=jq>
# integer division for precision when using gojq
def idivide($j):
. as $i
| ($i % $j) as $mod
| ($i - $mod) / $j ;
 
def small:
["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
Line 851 ⟶ 865:
["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
 
# Up to 1e18 (quintillion)
def illions:
["", " thousand", " million", " billion"," trillion", " quadrillion", " quintillion"];
 
def illions($i):
if $i >= (illions|length) then "\($i * 3) zeros is beyond the scope of this exercise" | error
else illions[$i]
end;
 
def irregularOrdinals: {
Line 864 ⟶ 884:
};
 
def check_ok:
def ieee754:
9007199254740993==9007199254740992;
if ieee754
then if (. > 0 and (. + 1) == .) or (. < 0 and (. - 1) == .)
then "The number \(.) is too large for this platform" | error
else .
end
else .
end;
# error courtesy of illions/1 if order of magnitude is too large
def say:
check_ok
| { t: "", n: .}
| if .n < 0 then .t = "negative " | .n *= -1 else . end
| if .n < 20
then .t += small[.n]
elif .n < 100
then .t += tens[(.n/ | idivide(10)|floor]
| .s = .n % 10
| if .s > 0 then .t += "-" + small[.s] else . end
elif .n < 1000
then .t += small[(.n/ | idivide(100)|floor] + " hundred"
| .s = .n % 100
| if .s > 0 then .t += " " + (.s|say) else . end
Line 881 ⟶ 914:
| until (.n <= 0;
.p = .n % 1000
| .n = ((.n/ | idivide(1000)|floor)
| if .p > 0
then .ix = (.p|say) + illions[(.i])
| if .sx != "" then .ix += " " + .sx else . end
| .sx = .ix
Line 910 ⟶ 943:
end;
 
# Sample inputs
(1, 2, 3, 4, 5, 11, 65, 100, 101, 272, 23456, 9007199254740991)
| sayOrdinal
8007006005004003,
9007199254740991,
2e12,
# 1e18 is quintillion so gojq should be able to handle 21 9s:
999999999999999999999, # 21 9s
# but not:
1000000000000000000000 # 21 0s
) | sayOrdinal
</syntaxhighlight>
The output using gojq is shown first. The tail of the output
using jq is then shown to illustrate what happens when the program
determines the given integer is too large for jq's built-in support
for integer arithmetic.
{{output}}
'''Using gojq'''
As for [[#Wren|Wren]].
<pre>
first
second
third
fourth
fifth
eleventh
sixty-fifth
one hundredth
one hundred first
two hundred seventy-second
twenty-three thousand four hundred fifty-sixth
eight quadrillion seven trillion six billion five million four thousand third
nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-first
two trillionth
nine hundred ninety-nine quintillion nine hundred ninety-nine quadrillion nine hundred ninety-nine trillion nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-ninth
gojq: error: 21 zeros is beyond the scope of this exercise
</pre>
'''Tail of output using the C implementation'''
<pre>
two trillionth
jq: error (at <unknown>): The number 1e+21 is too large for this platform
</pre>
 
=={{header|Julia}}==
2,472

edits