xref: /freebsd/contrib/bc/scripts/sqrt_int_guess.bc (revision f4fbc49d201f81c481a33fac6ba28e19faf96260)
1*f4fbc49dSStefan Eßer#! /usr/bin/bc -l
2*f4fbc49dSStefan Eßer#
3*f4fbc49dSStefan Eßer# SPDX-License-Identifier: BSD-2-Clause
4*f4fbc49dSStefan Eßer#
5*f4fbc49dSStefan Eßer# Copyright (c) 2018-2023 Gavin D. Howard and contributors.
6*f4fbc49dSStefan Eßer#
7*f4fbc49dSStefan Eßer# Redistribution and use in source and binary forms, with or without
8*f4fbc49dSStefan Eßer# modification, are permitted provided that the following conditions are met:
9*f4fbc49dSStefan Eßer#
10*f4fbc49dSStefan Eßer# * Redistributions of source code must retain the above copyright notice, this
11*f4fbc49dSStefan Eßer#   list of conditions and the following disclaimer.
12*f4fbc49dSStefan Eßer#
13*f4fbc49dSStefan Eßer# * Redistributions in binary form must reproduce the above copyright notice,
14*f4fbc49dSStefan Eßer#   this list of conditions and the following disclaimer in the documentation
15*f4fbc49dSStefan Eßer#   and/or other materials provided with the distribution.
16*f4fbc49dSStefan Eßer#
17*f4fbc49dSStefan Eßer# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*f4fbc49dSStefan Eßer# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*f4fbc49dSStefan Eßer# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*f4fbc49dSStefan Eßer# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*f4fbc49dSStefan Eßer# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*f4fbc49dSStefan Eßer# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*f4fbc49dSStefan Eßer# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*f4fbc49dSStefan Eßer# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*f4fbc49dSStefan Eßer# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*f4fbc49dSStefan Eßer# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*f4fbc49dSStefan Eßer# POSSIBILITY OF SUCH DAMAGE.
28*f4fbc49dSStefan Eßer#
29*f4fbc49dSStefan Eßer
30*f4fbc49dSStefan Eßer# Adjust this number to try ranges above different powers of 10.
31*f4fbc49dSStefan Eßermax = 0
32*f4fbc49dSStefan Eßer
33*f4fbc49dSStefan Eßern = (1 << max)
34*f4fbc49dSStefan Eßer
35*f4fbc49dSStefan Eßer# Uncomment this to test the high part of the ranges.
36*f4fbc49dSStefan Eßer#n += (1 - (1 >> 10))
37*f4fbc49dSStefan Eßer
38*f4fbc49dSStefan Eßern
39*f4fbc49dSStefan Eßer
40*f4fbc49dSStefan Eßer# Loop from the start number to the next power of 10.
41*f4fbc49dSStefan Eßerfor (i = n; i < (n$ << 1); i += 1)
42*f4fbc49dSStefan Eßer{
43*f4fbc49dSStefan Eßer	# This is the lower limit.
44*f4fbc49dSStefan Eßer	t1 = sqrt(1/(3*i))
45*f4fbc49dSStefan Eßer
46*f4fbc49dSStefan Eßer	l = length(i$)/2
47*f4fbc49dSStefan Eßer
48*f4fbc49dSStefan Eßer	print "i: ", i, "\n"
49*f4fbc49dSStefan Eßer	#print "l: ", l, "\n"
50*f4fbc49dSStefan Eßer
51*f4fbc49dSStefan Eßer	if (l$ != l)
52*f4fbc49dSStefan Eßer	{
53*f4fbc49dSStefan Eßer		# Limit between 2.4 and 3.
54*f4fbc49dSStefan Eßer		limit = 2.7 << (l$ * 2)
55*f4fbc49dSStefan Eßer		#print "limit: ", limit, "\n"
56*f4fbc49dSStefan Eßer
57*f4fbc49dSStefan Eßer		if (i >= limit)
58*f4fbc49dSStefan Eßer		{
59*f4fbc49dSStefan Eßer			t2 = 1/(i >> (l$)) * 2
60*f4fbc49dSStefan Eßer		}
61*f4fbc49dSStefan Eßer		else
62*f4fbc49dSStefan Eßer		{
63*f4fbc49dSStefan Eßer			t2 = 1/(i >> (l$))
64*f4fbc49dSStefan Eßer		}
65*f4fbc49dSStefan Eßer	}
66*f4fbc49dSStefan Eßer	else
67*f4fbc49dSStefan Eßer	{
68*f4fbc49dSStefan Eßer		# Limit between 3.8-ish and 4.8
69*f4fbc49dSStefan Eßer		limit = 4.3 << (l$ * 2 - 1)
70*f4fbc49dSStefan Eßer		#print "limit: ", limit, "\n"
71*f4fbc49dSStefan Eßer
72*f4fbc49dSStefan Eßer		if (i >= limit)
73*f4fbc49dSStefan Eßer		{
74*f4fbc49dSStefan Eßer			t2 = 1/(i >> (l$ - 1)) * 8
75*f4fbc49dSStefan Eßer		}
76*f4fbc49dSStefan Eßer		else
77*f4fbc49dSStefan Eßer		{
78*f4fbc49dSStefan Eßer			t2 = 1/(i >> (l$ - 1)) * 4
79*f4fbc49dSStefan Eßer		}
80*f4fbc49dSStefan Eßer	}
81*f4fbc49dSStefan Eßer
82*f4fbc49dSStefan Eßer	# This is the upper limit.
83*f4fbc49dSStefan Eßer	t3 = sqrt(5/(3*i))
84*f4fbc49dSStefan Eßer
85*f4fbc49dSStefan Eßer	# This is true when the guess is in between the limits.
86*f4fbc49dSStefan Eßer	good = (t1 < t2 && t2 < t3)
87*f4fbc49dSStefan Eßer
88*f4fbc49dSStefan Eßer	print t1, " < ", t2, " < ", t3, ": ", good, "\n"
89*f4fbc49dSStefan Eßer
90*f4fbc49dSStefan Eßer	# Error if we have a problem.
91*f4fbc49dSStefan Eßer	if (!good) sqrt(-1)
92*f4fbc49dSStefan Eßer}
93*f4fbc49dSStefan Eßer
94*f4fbc49dSStefan Eßerhalt
95