aboutsummaryrefslogtreecommitdiff
path: root/contrib/bc/scripts/sqrt_int_guess.bc
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/bc/scripts/sqrt_int_guess.bc')
-rw-r--r--contrib/bc/scripts/sqrt_int_guess.bc94
1 files changed, 94 insertions, 0 deletions
diff --git a/contrib/bc/scripts/sqrt_int_guess.bc b/contrib/bc/scripts/sqrt_int_guess.bc
new file mode 100644
index 000000000000..925b7af7e103
--- /dev/null
+++ b/contrib/bc/scripts/sqrt_int_guess.bc
@@ -0,0 +1,94 @@
+#! /usr/bin/bc -l
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2018-2024 Gavin D. Howard and contributors.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+# Adjust this number to try ranges above different powers of 10.
+max = 0
+
+n = (1 << max)
+
+# Uncomment this to test the high part of the ranges.
+#n += (1 - (1 >> 10))
+
+n
+
+# Loop from the start number to the next power of 10.
+for (i = n; i < (n$ << 1); i += 1)
+{
+ # This is the lower limit.
+ t1 = sqrt(1/(3*i))
+
+ l = length(i$)/2
+
+ print "i: ", i, "\n"
+ #print "l: ", l, "\n"
+
+ if (l$ != l)
+ {
+ # Limit between 2.4 and 3.
+ limit = 2.7 << (l$ * 2)
+ #print "limit: ", limit, "\n"
+
+ if (i >= limit)
+ {
+ t2 = 1/(i >> (l$)) * 2
+ }
+ else
+ {
+ t2 = 1/(i >> (l$))
+ }
+ }
+ else
+ {
+ # Limit between 3.8-ish and 4.8
+ limit = 4.3 << (l$ * 2 - 1)
+ #print "limit: ", limit, "\n"
+
+ if (i >= limit)
+ {
+ t2 = 1/(i >> (l$ - 1)) * 8
+ }
+ else
+ {
+ t2 = 1/(i >> (l$ - 1)) * 4
+ }
+ }
+
+ # This is the upper limit.
+ t3 = sqrt(5/(3*i))
+
+ # This is true when the guess is in between the limits.
+ good = (t1 < t2 && t2 < t3)
+
+ print t1, " < ", t2, " < ", t3, ": ", good, "\n"
+
+ # Error if we have a problem.
+ if (!good) sqrt(-1)
+}
+
+halt