3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2018-2023 Gavin D. Howard and contributors.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
34 # This extracts a bit and takes it out of the original value.
36 # Here, I am getting a bit to say whether we should have a value that is less
38 bits = divmod(bits, 2, negpow[])
40 # Get a bit that will say whether the value should be an exact square.
41 bits = divmod(bits, 2, square[])
43 # See below. This is to help bias toward small numbers.
46 # I want to bias toward small numbers, so let's give a 50 percent chance to
47 # values below 16 or so.
48 bits = divmod(bits, 2, small[])
50 # Let's keep raising the power limit by 2^4 when the bit is zero.
54 bits = divmod(bits, 2, small[])
59 # Okay, this is the starting number.
60 num = irand(limit) + 1
62 # Figure out if we should have (more) fractional digits.
63 bits = divmod(bits, 2, extra_digits[])
67 # Okay, I lied. If we need a perfect square, square now.
70 # If we need extra digits, we need to multiply by an even power of 10.
73 extra = (irand(8) + 1) * 2
80 # If we need a number less than 1, just take the inverse, which will still
81 # be a perfect square.
84 scale = length(num) + 5
100 # If we need extra digits.
103 # Add up to 32 decimal places.
104 num += frand(irand(32) + 1)
107 # If we need a value less than 1...
110 # Move right until the number is
115 bits = divmod(bits, 2, zero_scale[])
117 # Do we want a zero scale?
127 print "sqrt(", num, ")\n"