Jump to content

Test integerness: Difference between revisions

added Factor
m (→‎{{header|Perl 6}}: Combine into a single file for ease of testing)
(added Factor)
Line 828:
:"123" is integer?: false
</pre>
 
=={{header|Factor}}==
The <code>number=</code> word in the <code>math</code> vocabulary comes very close to encapsulating this task. It compares numbers for equality without regard for class, like <code>=</code> would. However, since <code>>integer</code> and <code>round</code> do not specialize on the <code>complex</code> class, we need to handle complex numbers specially. We use <code>>rect</code> to extract the real components of the complex number for further processing.
<lang factor>USING: formatting io kernel math math.functions sequences ;
IN: rosetta-code.test-integerness
 
GENERIC: integral? ( n -- ? )
 
M: real integral? [ ] [ >integer ] bi number= ;
M: complex integral? >rect [ integral? ] [ 0 number= ] bi* and ;
GENERIC# fuzzy-int? 1 ( n tolerance -- ? )
 
M: real fuzzy-int? [ dup round - abs ] dip <= ;
M: complex fuzzy-int? [ >rect ] dip swapd fuzzy-int? swap 0
number= and ;
 
{
25/1
50+2/3
34/73
312459210312903/129381293812491284512951
25.000000
24.999999
25.000100
-2.1e120
-5e-2
0/0. ! NaN
1/0. ! Infinity
C{ 5.0 0.0 }
C{ 5 -5 }
C{ 5 0 }
}
"Number" "Exact int?" "Fuzzy int? (tolerance=0.00001)"
"%-41s %-11s %s\n" printf
[
[ ] [ integral? ] [ 0.00001 fuzzy-int? ] tri
"%-41u %-11u %u\n" printf
] each</lang>
{{out}}
<pre>
Number Exact int? Fuzzy int? (tolerance=0.00001)
25 t t
50+2/3 f f
34/73 f f
312459210312903/129381293812491284512951 f t
25.0 t t
24.999999 f t
25.0001 f f
-2.1e+120 t t
-0.05 f f
NAN: 8000000000000 f f
1/0. f f
C{ 5.0 0.0 } t t
C{ 5 -5 } f f
5 t t
</pre>
 
=={{header|Fortran}}==
===Straightforward===
1,808

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.