How to resolve the algorithm Test integerness step by step in the ALGOL 68 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Test integerness step by step in the ALGOL 68 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 ALGOL 68 programming language

Source code in the algol programming language

# set the required precision of LONG LONG values using    #
# "PR precision n PR" if required                         #
PR precision 24 PR

# returns TRUE if v has an integer value, FALSE otherwise #
OP ISINT = ( LONG LONG COMPL v )BOOL:
   IF im OF v /= 0 THEN
       # v has an imaginary part #
       FALSE
   ELSE
       # v has a real part only #
       ENTIER re OF v = v
   FI; # ISINT #

# test ISINT #

PROC test is int = ( LONG LONG COMPLEX v )VOID:
     print( ( re OF v, "_", im OF v, IF ISINT v THEN " is " ELSE " is not " FI, "integral", newline ) );


test is int( 1 );
test is int( 1.00000001 );
test is int( 4 I 3 );
test is int( 4.0 I 0 );
test is int( 123456789012345678901234 )

  

You may also check:How to resolve the algorithm Text processing/1 step by step in the Racket programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the Pascal programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Delphi programming language
You may also check:How to resolve the algorithm Sort numbers lexicographically step by step in the C++ programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the AWK programming language