RosettaCodeData/Task/Test-integerness/Pluto/test-integerness.pluto

38 lines
997 B
Plaintext

require "complex"
require "rat"
require "table2"
local fmt = require "fmt"
local tests1 = {25.000000, 24.999999, 25.000100}
local tests2 = {-2.1e120}
local tests3 = {-5e-2, 0/0, 1/0}
local tests4 = {complex.of(5, 0), complex.of(5, -5)}
local tests5 = {rat.of(24, 8), rat.of(-5, 1), rat.of(17, 2)}
local tests6 = tests1:joined({-5e-2})
print("Using exact arithmetic:\n")
for tests1 as t do
fmt.print(" %-9.6f is integer? %s", t, t % 1 == 0)
end
print()
for tests2 as t do
fmt.print(" %-9g is integer? %s", t, t % 1 == 0)
end
for tests3 as t do
fmt.print(" %-9.6f is integer? %s", t, t % 1 == 0)
end
print()
for tests4 as t do
fmt.print(" %-9s is integer? %s", t, t:getReal() % 1 == 0 and t:getImag() == 0)
end
print()
for tests5 as t do
fmt.print(" %-9s is integer? %s", t, t:isint())
end
print("\nWithin a tolerance of 0.00001:\n")
local tol = 0.00001
for tests6 as t do
local d = math.abs(t - math.round(t))
fmt.print(" %9.6f is integer? %s", t, d <= tol)
end