How to resolve the algorithm Set of real numbers step by step in the zkl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Set of real numbers step by step in the zkl programming language
Table of Contents
Problem Statement
All real numbers form the uncountable set ℝ. Among its subsets, relatively simple are the convex sets, each expressed as a range between two real numbers a and b where a ≤ b. There are actually four cases for the meaning of "between", depending on open or closed boundary: Note that if a = b, of the four only [a, a] would be non-empty. Task Implementation notes Optional work |sin(π x)| > 1/2 is the same as n + 1/6 < x < n + 5/6 for all integers n; your program does not need to derive this by itself.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Set of real numbers step by step in the zkl programming language
Source code in the zkl programming language
class RealSet{
fcn init(fx){ var [const] contains=fx; }
fcn holds(x){ contains(x) }
fcn __opAdd(rs){ RealSet('wrap(x){ contains(x) or rs.contains(x) }) }
fcn __opSub(rs){ RealSet('wrap(x){ contains(x) and not rs.contains(x) }) }
fcn intersection(rs) { RealSet('wrap(x){ contains(x) and rs.contains(x) }) }
}
tester := TheVault.Test.UnitTester.UnitTester();
// test union
s:=RealSet(fcn(x){ 0.0 < x <= 1.0 }) +
RealSet(fcn(x){ 0.0 <= x < 1.0 });
tester.testRun(s.holds(0.0),Void,True,__LINE__);
tester.testRun(s.holds(1.0),Void,True,__LINE__);
tester.testRun(s.holds(2.0),Void,False,__LINE__);
// test difference
s1 := RealSet(fcn(x){ 0.0 <= x < 3.0 }) -
RealSet(fcn(x){ 0.0 < x < 1.0 });
tester.testRun(s1.holds(0.0),Void,True,__LINE__);
tester.testRun(s1.holds(0.5),Void,False,__LINE__);
tester.testRun(s1.holds(1.0),Void,True,__LINE__);
tester.testRun(s1.holds(2.0),Void,True,__LINE__);
s2 := RealSet(fcn(x){ 0.0 <= x < 3.0 }) -
RealSet(fcn(x){ 0.0 <= x <= 1.0 });
tester.testRun(s2.holds(0.0),Void,False,__LINE__);
tester.testRun(s2.holds(1.0),Void,False,__LINE__);
tester.testRun(s2.holds(2.0),Void,True,__LINE__);
// test intersection
s := RealSet(fcn(x){ 0.0 <= x < 2.0 }).intersection(
RealSet(fcn(x){ 1.0 < x <= 2.0 }));
tester.testRun(s.holds(0.0),Void,False,__LINE__);
tester.testRun(s.holds(1.0),Void,False,__LINE__);
tester.testRun(s.holds(2.0),Void,False,__LINE__);
You may also check:How to resolve the algorithm Middle three digits step by step in the J programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the OCaml programming language
You may also check:How to resolve the algorithm Anti-primes step by step in the Odin programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Neko programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the PHP programming language