How to resolve the algorithm Test integerness step by step in the Racket programming language
How to resolve the algorithm Test integerness step by step in the Racket programming language
Table of Contents
Problem Statement
Mathematically,
This means that each of those larger sets, and the data types used to represent them, include some integers. 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. For the purposes of this task, integerness means that a number could theoretically be represented as an integer at no loss of precision (given an infinitely wide integer type). In other words: Optionally, make your code accept a tolerance 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 round-off errors from previous calculations. For example, a float value of 0.9999999998 might actually be intended to represent the integer 1. (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.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Test integerness step by step in the Racket programming language
Source code in the racket programming language
#lang racket
(require tests/eli-tester)
(test ;; known representations of integers:
;; - as exacts
(integer? -1) => #t
(integer? 0) => #t
(integer? 1) => #t
(integer? 1234879378539875943875937598379587539875498792424323432432343242423432432) => #t
(integer? -1234879378539875943875937598379587539875498792424323432432343242423432432) => #t
(integer? #xff) => #t
;; - as inexacts
(integer? -1.) => #t
(integer? 0.) => #t
(integer? 1.) => #t
(integer? 1234879378539875943875937598379587539875498792424323432432343242423432432.) => #t
(integer? #xff.0) => #t
;; - but without a decimal fractional part
(integer? -1.1) => #f
;; - fractional representation
(integer? -42/3) => #t
(integer? 0/1) => #t
(integer? 27/9) => #t
(integer? #xff/f) => #t
(integer? #b11111111/1111) => #t
;; - but obviously not fractions
(integer? 5/7) => #f
; - as scientific
(integer? 1.23e2) => #t
(integer? 1.23e120) => #t
; - but not with a small exponent
(integer? 1.23e1) => #f
; - complex representations with 0 imaginary component
; ℤ is a subset of the sets of rational and /real/ numbers and
(integer? 1+0i) => #t
(integer? (sqr 0+1i)) => #t
(integer? 0+1i) => #f
;; oh, there's so much else that isn't an integer:
(integer? "woo") => #f
(integer? "100") => #f
(integer? (string->number "22/11")) => #t ; just cast it!
(integer? +inf.0) => #f
(integer? -inf.0) => #f
(integer? +nan.0) => #f ; duh! it's not even a number!
(integer? -NaN.0) => #f
(integer? pi) => #f
)
You may also check:How to resolve the algorithm String concatenation step by step in the Nim programming language
You may also check:How to resolve the algorithm Delete a file step by step in the VAX Assembly programming language
You may also check:How to resolve the algorithm Munching squares step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Guess the number step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the Go programming language