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