Test integerness: Difference between revisions

m
→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics
(Added XPL0 example.)
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
Line 1,776:
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
In most cases the builtin works prety well, with Phix automatically storing integer results as such.
 
<lang Phix>?integer(3.5+3.5) -- true
<!--<lang Phix>-->
?integer(3.5+3.4) -- false</lang>
<span style="color: #0000FF;">?</span><span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3.5</span><span style="color: #0000FF;">+</span><span style="color: #000000;">3.5</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- true</span>
<span style="color: #0000FF;">?</span><span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3.5</span><span style="color: #0000FF;">+</span><span style="color: #000000;">3.4</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- false</span>
<!--</lang>-->
 
The round function takes an inverted precision, so 1000000 means to the nearest 0.000001 and 100000 means the nearest 0.00001
 
<lang Phix>?integer(round(24.999999,1000000)) -- false
<!--<lang Phix>-->
?integer(round(24.999999,100000)) -- true</lang>
<span style="color: #0000FF;">?</span><span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">24.999999</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000000</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- false</span>
<span style="color: #0000FF;">?</span><span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">24.999999</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100000</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- true</span>
<!--</lang>-->
 
By default the inverted precision of round is 1, and that does exactly what you'd expect.
 
<lang Phix>?equal(-2.1e120,round(-2.1e120)) -- true
<!--<lang Phix>-->
?equal(-2.15,round(-2.15)) -- false</lang>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">equal</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2.1e120</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">round</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2.1e120</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- true</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">equal</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2.15</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">round</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2.15</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- false</span>
<!--</lang>-->
 
Technically though, -2.1e120 is way past precision limits, as next, so declaring it integer is deeply flawed...
It is not only way too big to fit in an integer, but also simply too big to actually have a fractional part.
Obviously using bigatoms would "solve" this, as long as I was prepared to wait for it to wade through the 120+
digits of precision needed, that is compared to the mere 19 or so that the raw physical hardware can manage.
 
<lang Phix>?equal(-2.1e120,-2.1e120+PI) -- true!!</lang>
<!--<lang Phix>-->
Phix considers both nan and inf as not an integer, and does not support complex numbers (as a primitive type). Two final examples:
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">equal</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2.1e120</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2.1e120</span><span style="color: #0000FF;">+</span><span style="color: #000000;">PI</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- true!!</span>
<lang Phix>?integer(-5e-2) -- false
<!--</lang>-->
?integer(25.000000) -- true</lang>
 
Phix considers both nan and inf as not an integer, and does not support complex numbers (as a primitive type, though there is a builtins/complex.e, not an autoinclude). Two final examples:
 
<!--<lang Phix>-->
<span style="color: #0000FF;">?</span><span style="color: #004080;">integer</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">5e-2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- false</span>
<span style="color: #0000FF;">?</span><span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">25.000000</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- true</span>
<!--</lang>-->
 
=={{header|PicoLisp}}==
7,813

edits