xref: /aosp_15_r20/external/bc/scripts/sqrt_frac_guess.bc (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1*5a6e8488SAndroid Build Coastguard Worker#! /usr/bin/bc
2*5a6e8488SAndroid Build Coastguard Worker#
3*5a6e8488SAndroid Build Coastguard Worker# SPDX-License-Identifier: BSD-2-Clause
4*5a6e8488SAndroid Build Coastguard Worker#
5*5a6e8488SAndroid Build Coastguard Worker# Copyright (c) 2018-2024 Gavin D. Howard and contributors.
6*5a6e8488SAndroid Build Coastguard Worker#
7*5a6e8488SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without
8*5a6e8488SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met:
9*5a6e8488SAndroid Build Coastguard Worker#
10*5a6e8488SAndroid Build Coastguard Worker# * Redistributions of source code must retain the above copyright notice, this
11*5a6e8488SAndroid Build Coastguard Worker#   list of conditions and the following disclaimer.
12*5a6e8488SAndroid Build Coastguard Worker#
13*5a6e8488SAndroid Build Coastguard Worker# * Redistributions in binary form must reproduce the above copyright notice,
14*5a6e8488SAndroid Build Coastguard Worker#   this list of conditions and the following disclaimer in the documentation
15*5a6e8488SAndroid Build Coastguard Worker#   and/or other materials provided with the distribution.
16*5a6e8488SAndroid Build Coastguard Worker#
17*5a6e8488SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*5a6e8488SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*5a6e8488SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*5a6e8488SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*5a6e8488SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*5a6e8488SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*5a6e8488SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*5a6e8488SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*5a6e8488SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*5a6e8488SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*5a6e8488SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE.
28*5a6e8488SAndroid Build Coastguard Worker#
29*5a6e8488SAndroid Build Coastguard Worker
30*5a6e8488SAndroid Build Coastguard Workerscale = 20
31*5a6e8488SAndroid Build Coastguard Worker
32*5a6e8488SAndroid Build Coastguard Worker# Adjust this number to try ranges below different powers of 10.
33*5a6e8488SAndroid Build Coastguard Workershift = 4
34*5a6e8488SAndroid Build Coastguard Worker
35*5a6e8488SAndroid Build Coastguard Worker# Adjust this to try extra digits. For example, a value of one means that one
36*5a6e8488SAndroid Build Coastguard Worker# digit is checked (such as 0.09 through 0.01), a value of two means that two
37*5a6e8488SAndroid Build Coastguard Worker# digits are checked (0.090 through 0.010), etc.
38*5a6e8488SAndroid Build Coastguard Workermax = shift + 2
39*5a6e8488SAndroid Build Coastguard Worker
40*5a6e8488SAndroid Build Coastguard Workern = (9 >> shift)
41*5a6e8488SAndroid Build Coastguard Workerinc = (1 >> max)
42*5a6e8488SAndroid Build Coastguard Workerstop = (1 >> shift)
43*5a6e8488SAndroid Build Coastguard Worker
44*5a6e8488SAndroid Build Coastguard Worker# Uncomment this to test the high part of the ranges.
45*5a6e8488SAndroid Build Coastguard Worker#n += (1 - (1 >> max + 5)) >> shift
46*5a6e8488SAndroid Build Coastguard Worker
47*5a6e8488SAndroid Build Coastguard Workerfor (i = n; i >= stop; i -= inc)
48*5a6e8488SAndroid Build Coastguard Worker{
49*5a6e8488SAndroid Build Coastguard Worker	# This is the lower limit.
50*5a6e8488SAndroid Build Coastguard Worker	t1 = sqrt(1/(3*i))
51*5a6e8488SAndroid Build Coastguard Worker
52*5a6e8488SAndroid Build Coastguard Worker	# Start with the inverse.
53*5a6e8488SAndroid Build Coastguard Worker	t2 = (1/i)
54*5a6e8488SAndroid Build Coastguard Worker
55*5a6e8488SAndroid Build Coastguard Worker	# And take half its length of course.
56*5a6e8488SAndroid Build Coastguard Worker	l = length(t2$)/2
57*5a6e8488SAndroid Build Coastguard Worker
58*5a6e8488SAndroid Build Coastguard Worker	temp = i
59*5a6e8488SAndroid Build Coastguard Worker	odd = 0
60*5a6e8488SAndroid Build Coastguard Worker
61*5a6e8488SAndroid Build Coastguard Worker	# We go by powers of 10 below, but there is a degenerate case: an exact
62*5a6e8488SAndroid Build Coastguard Worker	# power of 10, for which length() will return one digit more. So we check
63*5a6e8488SAndroid Build Coastguard Worker	# for that and fix it.
64*5a6e8488SAndroid Build Coastguard Worker	while (temp < 1)
65*5a6e8488SAndroid Build Coastguard Worker	{
66*5a6e8488SAndroid Build Coastguard Worker		temp <<= 1
67*5a6e8488SAndroid Build Coastguard Worker		odd = !odd
68*5a6e8488SAndroid Build Coastguard Worker	}
69*5a6e8488SAndroid Build Coastguard Worker
70*5a6e8488SAndroid Build Coastguard Worker	if (temp == 1)
71*5a6e8488SAndroid Build Coastguard Worker	{
72*5a6e8488SAndroid Build Coastguard Worker		odd = !odd
73*5a6e8488SAndroid Build Coastguard Worker	}
74*5a6e8488SAndroid Build Coastguard Worker
75*5a6e8488SAndroid Build Coastguard Worker	print "i:  ", i, "\n"
76*5a6e8488SAndroid Build Coastguard Worker	print "t2: ", t2, "\n"
77*5a6e8488SAndroid Build Coastguard Worker	#print "l:  ", l, "\n"
78*5a6e8488SAndroid Build Coastguard Worker	print "odd: ", odd, "\n"
79*5a6e8488SAndroid Build Coastguard Worker
80*5a6e8488SAndroid Build Coastguard Worker	if (odd)
81*5a6e8488SAndroid Build Coastguard Worker	{
82*5a6e8488SAndroid Build Coastguard Worker		# Limit between 6 and 7.5.
83*5a6e8488SAndroid Build Coastguard Worker		limit1 = 6.7 >> (l$ * 2 + 1)
84*5a6e8488SAndroid Build Coastguard Worker
85*5a6e8488SAndroid Build Coastguard Worker		# Limit between 1.5 and 1.83-ish.
86*5a6e8488SAndroid Build Coastguard Worker		limit2 = 1.7 >> (l$ * 2 + 1)
87*5a6e8488SAndroid Build Coastguard Worker		print "limit1: ", limit1, "\n"
88*5a6e8488SAndroid Build Coastguard Worker		print "limit2: ", limit2, "\n"
89*5a6e8488SAndroid Build Coastguard Worker
90*5a6e8488SAndroid Build Coastguard Worker		if (i >= limit1)
91*5a6e8488SAndroid Build Coastguard Worker		{
92*5a6e8488SAndroid Build Coastguard Worker			t2 = (t2 >> l$)
93*5a6e8488SAndroid Build Coastguard Worker		}
94*5a6e8488SAndroid Build Coastguard Worker		else if (i >= limit2)
95*5a6e8488SAndroid Build Coastguard Worker		{
96*5a6e8488SAndroid Build Coastguard Worker			t2 = (t2 >> l$) / 2
97*5a6e8488SAndroid Build Coastguard Worker		}
98*5a6e8488SAndroid Build Coastguard Worker		else
99*5a6e8488SAndroid Build Coastguard Worker		{
100*5a6e8488SAndroid Build Coastguard Worker			t2 = (t2 >> l$) / 4
101*5a6e8488SAndroid Build Coastguard Worker		}
102*5a6e8488SAndroid Build Coastguard Worker	}
103*5a6e8488SAndroid Build Coastguard Worker	else
104*5a6e8488SAndroid Build Coastguard Worker	{
105*5a6e8488SAndroid Build Coastguard Worker		# Limit between 2.4 and 3.
106*5a6e8488SAndroid Build Coastguard Worker		limit = 2.7 >> (l$ * 2)
107*5a6e8488SAndroid Build Coastguard Worker		print "limit: ", limit, "\n"
108*5a6e8488SAndroid Build Coastguard Worker
109*5a6e8488SAndroid Build Coastguard Worker		if (i >= limit)
110*5a6e8488SAndroid Build Coastguard Worker		{
111*5a6e8488SAndroid Build Coastguard Worker			t2 = (t2 >> l$) * 2
112*5a6e8488SAndroid Build Coastguard Worker		}
113*5a6e8488SAndroid Build Coastguard Worker		else
114*5a6e8488SAndroid Build Coastguard Worker		{
115*5a6e8488SAndroid Build Coastguard Worker			t2 = (t2 >> l$)
116*5a6e8488SAndroid Build Coastguard Worker		}
117*5a6e8488SAndroid Build Coastguard Worker	}
118*5a6e8488SAndroid Build Coastguard Worker	#t2 = 1
119*5a6e8488SAndroid Build Coastguard Worker	t3 = sqrt(5/(3*i))
120*5a6e8488SAndroid Build Coastguard Worker	good = (t1 < t2 && t2 < t3)
121*5a6e8488SAndroid Build Coastguard Worker
122*5a6e8488SAndroid Build Coastguard Worker	print t1, " < ", t2, " < ", t3, ": ", good, "\n\n"
123*5a6e8488SAndroid Build Coastguard Worker	if (!good) sqrt(-1)
124*5a6e8488SAndroid Build Coastguard Worker}
125*5a6e8488SAndroid Build Coastguard Worker
126*5a6e8488SAndroid Build Coastguard Workerhalt
127