113 lines
3.4 KiB
Plaintext
113 lines
3.4 KiB
Plaintext
Mathematically,
|
|
* the [[wp:Integer|integers]] '''Z''' are included in the [[wp:Rational number|rational numbers]] '''Q''',
|
|
* which are included in the [[wp:Real number|real numbers]] '''R''',
|
|
* which can be generalized to the [[wp:Complex number|complex numbers]] '''C'''.
|
|
|
|
|
|
This means that each of those larger sets, and the data types used to represent them, include some integers.
|
|
|
|
{{task heading}}
|
|
|
|
Given a rational, real, or complex number of any type, test whether it is mathematically an integer.
|
|
|
|
Your code should handle all numeric data types commonly used in your programming language.
|
|
|
|
Discuss any limitations of your code.
|
|
|
|
{{task heading|Definition}}
|
|
|
|
For the purposes of this task, integerness means that a number could theoretically be represented as an integer at no loss of precision ''<small>(given an infinitely wide integer type)</small>''.<br>
|
|
In other words:
|
|
|
|
{| class="wikitable"
|
|
|-
|
|
! Set
|
|
! Common representation
|
|
! C++ type
|
|
! Considered an integer...
|
|
|-
|
|
| rational numbers '''Q'''
|
|
| [[wp:Rational data type|fraction]]
|
|
| <code>std::ratio</code>
|
|
| ...if its denominator is 1 (in reduced form)
|
|
|-
|
|
| rowspan=2 | real numbers '''Z'''<br><small>''(approximated)''</small>
|
|
| [[wp:Fixed-point arithmetic|fixed-point]]
|
|
|
|
|
| ...if it has no non-zero digits after the decimal point
|
|
|-
|
|
| [[wp:Floating point|floating-point]]
|
|
| <code>float</code>, <code>double</code>
|
|
| ...if the number of significant decimal places of its mantissa isn't greater than its exponent
|
|
|-
|
|
| complex numbers '''C'''
|
|
| [[wp:Complex data type|pair of real numbers]]
|
|
| <code>std::complex</code>
|
|
| ...if its real part is considered an integer and its imaginary part is zero
|
|
|}
|
|
|
|
{{task heading|Extra credit}}
|
|
|
|
Optionally, make your code accept a <code>tolerance</code> parameter for fuzzy testing. The tolerance is the maximum amount by which the number may differ from the nearest integer, to still be considered an integer.
|
|
|
|
This is useful in practice, because when dealing with approximate numeric types (such as floating point), there may already be [[wp:Round-off error|round-off errors]] from previous calculations. For example, a float value of <code>0.9999999998</code> might actually be intended to represent the integer <code>1</code>.
|
|
|
|
{{task heading|Test cases}}
|
|
|
|
{| class="wikitable"
|
|
|-
|
|
! colspan=2 | Input
|
|
! colspan=2 | Output
|
|
! rowspan=2 | Comment
|
|
|-
|
|
! <small>Type</small>
|
|
! <small>Value</small>
|
|
! <small><tt>exact</tt></small>
|
|
! <small><tt>tolerance = 0.00001</tt></small>
|
|
|-
|
|
| rowspan=3 | decimal
|
|
| <code>25.000000</code>
|
|
| colspan=2 | true
|
|
|
|
|
|-
|
|
| <code>24.999999</code>
|
|
| false
|
|
| true
|
|
|
|
|
|-
|
|
| <code>25.000100</code>
|
|
| colspan=2 | false
|
|
|
|
|
|-
|
|
| rowspan=4 | floating-point
|
|
| <code>-2.1e120</code>
|
|
| colspan=2 | true
|
|
| This one is tricky, because in most languages it is too large to fit into a native integer type.<br>It is, nonetheless, mathematically an integer, and your code should identify it as such.
|
|
|-
|
|
| <code>-5e-2</code>
|
|
| colspan=2 | false
|
|
|
|
|
|-
|
|
| <code>NaN</code>
|
|
| colspan=2 | false
|
|
|
|
|
|-
|
|
| <code>Inf</code>
|
|
| colspan=2 | false
|
|
| This one is debatable. If your code considers it an integer, that's okay too.
|
|
|-
|
|
| rowspan=2 | complex
|
|
| <code>5.0+0.0i</code>
|
|
| colspan=2 | true
|
|
|
|
|
|-
|
|
| <code>5-5i</code>
|
|
| colspan=2 | false
|
|
|
|
|
|}
|
|
|
|
(The types and notations shown in these tables are merely examples – you should use the native data types and number literals of your programming language and standard library. Use a different set of test-cases, if this one doesn't demonstrate all relevant behavior.)
|
|
|
|
<hr>
|
|
|