Test integerness: Difference between revisions

Updated to work with Nim 1.4: added parentheses around negative floats. Changed "is_integer" to "isInteger" to follow Nim guidelines. Done miscellaneous minor changes.
(Updated to work with Nim 1.4: added parentheses around negative floats. Changed "is_integer" to "isInteger" to follow Nim guidelines. Done miscellaneous minor changes.)
Line 1,680:
{{out}}<pre>{False,False,True,False}</pre>
=={{header|Nim}}==
aA taxonomy of Nim number types:
 
::– SomeInteger: integer types, signed or unsigned, 8,16,32,or 64 bits
 
::– SomeFloat: floating point, 32 or 64 bits
 
::– SomeNumber: SomeInteger or SomeFloat
 
::– Rational: rational type; a numerator and denominator, of any (identical) SomeInteger type
 
::– Complex: complex type; real and imaginary of any (identical) SomeFloat type
 
<lang nim>import complex, rationals, math, fenv, sugar
 
func is_integerisInteger[T: Complex | Rational | SomeNumber](x: T,; tolerance: float64 = 0.00f64): bool =
when T is Complex:
x.im == 0 and x.re.is_integerisInteger
elif T is Rational:
x.dup(reduce).den == 1
Line 1,703 ⟶ 1,704:
true
 
# Floats.
assert not NaN.is_integer
assert not INF.is_integer #ceil(INF)-INF = NaN.isInteger
assert not INF.isInteger # Indeed, "ceil(INF) - INF" is NaN.
assert not -5e-2.is_integer
assert not (-5e-2).1e120.is_integerisInteger
assert (25-2.01e120).is_integerisInteger
assert not 2425.9999990.is_integerisInteger
assert not 24.999999.is_integer(tolerance = 0.00001)isInteger
assert 24.999999.isInteger(tolerance = 0.00001)
assert not (1.0'f64 + epsilon(float64)).is_integer
assert not (1.0'f321f64 -+ epsilon(float32float64)).is_integerisInteger
assert not (1.0'f641f32 +- epsilon(float64float32)).is_integerisInteger
#rationals
# Rationals.
assert not (5 // 3).is_integer
assert not (95 // 3).is_integerisInteger
assert (-1439 // 133).is_integerisInteger
assert not (5-143 // 313).is_integerisInteger
#unsigned
# Unsigned integers.
assert (3'u).is_integer
assert 3u.isInteger
#complex
# Complex numbers.
assert not (1.0 + im 1.0).is_integer
assert not (51.0 + im 01.0).is_integer</lang>isInteger
assert not (15.0 + im 10.0).is_integerisInteger</lang>
 
=={{header|ooRexx}}==
Anonymous user