Download or view sqrtNearestIntegerTest.frink in plain text format
/** This tests the sqrtNearestInteger function. */
// Try near powers of 2 first
for bits=3 to 100
{
for offset = -5 to 5
{
n = 2^bits + offset
i = sqrtNearestInteger[n]
if (i^2 > n) or ((i+1)^2 <= n)
println["Error at $n, i=$i"]
}
}
// Random numbers of varying magnitudes that will exercise the int, long,
// and BigDecimal versions.
for tries = 1 to 100 million
{
bits = random[1,100]
n = randomBits[bits]
i = sqrtNearestInteger[n]
if (i^2 > n) or ((i+1)^2 <= n)
println["Error at $n, i=$i"]
}
Download or view sqrtNearestIntegerTest.frink in plain text format
This is a program written in the programming language Frink.
For more information, view the Frink
Documentation or see More Sample Frink Programs.
Alan Eliasen was born 20107 days, 23 hours, 24 minutes ago.