Test integerness: Difference between revisions
Content added Content deleted
m (→{{header|Nim}}) |
(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: | Line 1,680: | ||
{{out}}<pre>{False,False,True,False}</pre> |
{{out}}<pre>{False,False,True,False}</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
A taxonomy of Nim number types: |
|||
SomeInteger: integer types, signed or unsigned, 8,16,32,or 64 bits |
::– SomeInteger: integer types, signed or unsigned, 8,16,32,or 64 bits |
||
SomeFloat: floating point, 32 or 64 bits |
::– SomeFloat: floating point, 32 or 64 bits |
||
SomeNumber: SomeInteger or SomeFloat |
::– SomeNumber: SomeInteger or SomeFloat |
||
Rational: rational type; a numerator and denominator, of any (identical) SomeInteger type |
::– Rational: rational type; a numerator and denominator, of any (identical) SomeInteger type |
||
Complex: complex type; real and imaginary of any (identical) SomeFloat type |
::– Complex: complex type; real and imaginary of any (identical) SomeFloat type |
||
<lang nim>import complex, rationals, math, fenv, sugar |
<lang nim>import complex, rationals, math, fenv, sugar |
||
func |
func isInteger[T: Complex | Rational | SomeNumber](x: T; tolerance = 0f64): bool = |
||
when T is Complex: |
when T is Complex: |
||
x.im == 0 and x.re. |
x.im == 0 and x.re.isInteger |
||
elif T is Rational: |
elif T is Rational: |
||
x.dup(reduce).den == 1 |
x.dup(reduce).den == 1 |
||
Line 1,703: | Line 1,704: | ||
true |
true |
||
# Floats. |
|||
assert not NaN.is_integer |
|||
assert not |
assert not NaN.isInteger |
||
assert not INF.isInteger # Indeed, "ceil(INF) - INF" is NaN. |
|||
assert not -5e-2.is_integer |
|||
assert -2. |
assert not (-5e-2).isInteger |
||
assert ( |
assert (-2.1e120).isInteger |
||
assert |
assert 25.0.isInteger |
||
assert 24.999999. |
assert not 24.999999.isInteger |
||
assert 24.999999.isInteger(tolerance = 0.00001) |
|||
⚫ | |||
assert not ( |
assert not (1f64 + epsilon(float64)).isInteger |
||
⚫ | |||
#rationals |
|||
# Rationals. |
|||
⚫ | |||
assert ( |
assert not (5 // 3).isInteger |
||
assert ( |
assert (9 // 3).isInteger |
||
⚫ | |||
#unsigned |
|||
# Unsigned integers. |
|||
assert (3'u).is_integer |
|||
assert 3u.isInteger |
|||
#complex |
|||
# Complex numbers. |
|||
⚫ | |||
assert ( |
assert not (1.0 + im 1.0).isInteger |
||
⚫ | |||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |