Skip to content

Instantly share code, notes, and snippets.

@lhuemill
Last active October 19, 2020 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lhuemill/af0b2a6e31df74ed7346d6c46a31a0a6 to your computer and use it in GitHub Desktop.
Save lhuemill/af0b2a6e31df74ed7346d6c46a31a0a6 to your computer and use it in GitHub Desktop.
PI Half Angle
0 0 2
1 1 3.
2 2 3.1
3 2 3.1
4 3 3.14
5 3 3.14
6 4 3.141
7 4 3.141
8 4 3.141
9 6 3.14159
10 6 3.14159
11 7 3.141592
12 8 3.1415926
13 8 3.1415926
14 9 3.14159265
15 9 3.14159265
16 10 3.141592653
17 10 3.141592653
18 11 3.1415926535
19 11 3.1415926535
20 11 3.1415926535
21 13 3.141592653589
22 13 3.141592653589
23 13 3.141592653589
24 15 3.14159265358979
25 15 3.14159265358979
26 16 3.141592653589793
27 17 3.1415926535897932
28 17 3.1415926535897932
29 17 3.1415926535897932
30 18 3.14159265358979323
31 19 3.141592653589793238
32 20 3.1415926535897932384
33 20 3.1415926535897932384
34 21 3.14159265358979323846
35 21 3.14159265358979323846
36 22 3.141592653589793238462
37 23 3.1415926535897932384626
38 23 3.1415926535897932384626
39 24 3.14159265358979323846264
40 25 3.141592653589793238462643
41 25 3.141592653589793238462643
42 25 3.141592653589793238462643
43 26 3.1415926535897932384626433
44 27 3.14159265358979323846264338
45 28 3.141592653589793238462643383
46 28 3.141592653589793238462643383
47 28 3.141592653589793238462643383
48 29 3.1415926535897932384626433832
49 29 3.1415926535897932384626433832
50 29 3.1415926535897932384626433832
51 31 3.141592653589793238462643383279
52 31 3.141592653589793238462643383279
53 31 3.141592653589793238462643383279
54 33 3.14159265358979323846264338327950
55 33 3.14159265358979323846264338327950
56 33 3.14159265358979323846264338327950
57 34 3.141592653589793238462643383279502
58 35 3.1415926535897932384626433832795028
59 36 3.14159265358979323846264338327950288
60 36 3.14159265358979323846264338327950288
61 37 3.141592653589793238462643383279502884
62 37 3.141592653589793238462643383279502884
63 37 3.141592653589793238462643383279502884
64 39 3.14159265358979323846264338327950288419
65 39 3.14159265358979323846264338327950288419
66 40 3.141592653589793238462643383279502884197
67 41 3.1415926535897932384626433832795028841971
68 41 3.1415926535897932384626433832795028841971
69 41 3.1415926535897932384626433832795028841971
70 43 3.141592653589793238462643383279502884197169
71 43 3.141592653589793238462643383279502884197169
72 43 3.141592653589793238462643383279502884197169
73 43 3.141592653589793238462643383279502884197169
74 43 3.141592653589793238462643383279502884197169
75 46 3.141592653589793238462643383279502884197169399
76 46 3.141592653589793238462643383279502884197169399
77 46 3.141592653589793238462643383279502884197169399
78 47 3.1415926535897932384626433832795028841971693993
79 48 3.14159265358979323846264338327950288419716939937
80 48 3.14159265358979323846264338327950288419716939937
81 49 3.141592653589793238462643383279502884197169399375
82 49 3.141592653589793238462643383279502884197169399375
83 50 3.1415926535897932384626433832795028841971693993751
84 51 3.14159265358979323846264338327950288419716939937510
85 51 3.14159265358979323846264338327950288419716939937510
86 52 3.141592653589793238462643383279502884197169399375105
87 53 3.1415926535897932384626433832795028841971693993751058
88 53 3.1415926535897932384626433832795028841971693993751058
89 54 3.14159265358979323846264338327950288419716939937510582
90 54 3.14159265358979323846264338327950288419716939937510582
91 54 3.14159265358979323846264338327950288419716939937510582
92 54 3.14159265358979323846264338327950288419716939937510582
93 56 3.1415926535897932384626433832795028841971693993751058209
94 57 3.14159265358979323846264338327950288419716939937510582097
95 57 3.14159265358979323846264338327950288419716939937510582097
96 57 3.14159265358979323846264338327950288419716939937510582097
97 59 3.1415926535897932384626433832795028841971693993751058209749
98 59 3.1415926535897932384626433832795028841971693993751058209749
99 60 3.14159265358979323846264338327950288419716939937510582097494
100 61 3.141592653589793238462643383279502884197169399375105820974944
101 61 3.141592653589793238462643383279502884197169399375105820974944
102 61 3.141592653589793238462643383279502884197169399375105820974944
103 62 3.1415926535897932384626433832795028841971693993751058209749445
104 63 3.14159265358979323846264338327950288419716939937510582097494459
105 64 3.141592653589793238462643383279502884197169399375105820974944592
106 64 3.141592653589793238462643383279502884197169399375105820974944592
107 64 3.141592653589793238462643383279502884197169399375105820974944592
108 65 3.1415926535897932384626433832795028841971693993751058209749445923
109 66 3.14159265358979323846264338327950288419716939937510582097494459230
110 66 3.14159265358979323846264338327950288419716939937510582097494459230
111 67 3.141592653589793238462643383279502884197169399375105820974944592307
112 68 3.1415926535897932384626433832795028841971693993751058209749445923078
113 68 3.1415926535897932384626433832795028841971693993751058209749445923078
114 69 3.14159265358979323846264338327950288419716939937510582097494459230781
115 70 3.141592653589793238462643383279502884197169399375105820974944592307816
116 70 3.141592653589793238462643383279502884197169399375105820974944592307816
117 70 3.141592653589793238462643383279502884197169399375105820974944592307816
118 71 3.1415926535897932384626433832795028841971693993751058209749445923078164
119 72 3.14159265358979323846264338327950288419716939937510582097494459230781640
120 73 3.141592653589793238462643383279502884197169399375105820974944592307816406
121 73 3.141592653589793238462643383279502884197169399375105820974944592307816406
122 73 3.141592653589793238462643383279502884197169399375105820974944592307816406
123 74 3.1415926535897932384626433832795028841971693993751058209749445923078164062
124 75 3.14159265358979323846264338327950288419716939937510582097494459230781640628
125 76 3.141592653589793238462643383279502884197169399375105820974944592307816406286
126 76 3.141592653589793238462643383279502884197169399375105820974944592307816406286
127 76 3.141592653589793238462643383279502884197169399375105820974944592307816406286
128 77 3.1415926535897932384626433832795028841971693993751058209749445923078164062862
129 77 3.1415926535897932384626433832795028841971693993751058209749445923078164062862
130 78 3.14159265358979323846264338327950288419716939937510582097494459230781640628620
131 78 3.14159265358979323846264338327950288419716939937510582097494459230781640628620
132 78 3.14159265358979323846264338327950288419716939937510582097494459230781640628620
133 78 3.14159265358979323846264338327950288419716939937510582097494459230781640628620
134 81 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899
135 82 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998
136 82 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998
137 83 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986
138 83 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986
139 84 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862
140 84 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862
141 84 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862
142 86 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280
143 86 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280
144 87 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803
145 87 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803
146 88 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034
147 89 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348
148 89 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348
149 90 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482
150 91 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825
151 91 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825
152 92 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253
153 92 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253
154 93 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534
155 93 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534
156 94 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342
157 95 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421
158 95 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421
159 96 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211
160 96 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211
161 97 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117
162 98 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170
163 98 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170
164 99 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706
165 99 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706
166 99 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706
167 100 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#!/usr/bin/env python
"""Digits of PI Calculator
Copyright 2017 Louis Huemiller
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Calculates the value of PI, to a specified number of digits (default 100).
Value is calculated by starting with a 4-sided regular polygon (a square)
within a circle with a radius of 1. It then iteratively doubles the number
of sides, until there are enough sides to calculate PI to the requested
number of digits. Calculates approximately 0.6 decimal digits of PI per
iteration.
Command-line Options:
--digits - Digits of PI to be calculated (default: 100)
--digits_guard - Number of additional guard digits. Used to handle
rounding within calculation (default: 100).
"""
from __future__ import print_function
from __future__ import division
import argparse
import decimal
from decimal import Decimal
numerically_unstable_msg = '''
---- Numerically Unstable ---
Use --digits_guard to increase the number of guard digits.
'''
class right_triangle(object):
@staticmethod
def other_side(s1, hypot = Decimal(1.0)):
'''
Calculates and returns the length of the other side of
a right-triangle, given the length of a side and the length
of the hypotenuse.
'''
squared_difference = (hypot * hypot) - (s1 * s1)
return pow(squared_difference, Decimal(0.5)) # Square root
@staticmethod
def half_angle(s1, hypot = Decimal(1.0)):
'''
Given the length of the hypotenuse plus a side opposed to a non-right
angle, calculates and returns the length of the side if the non-right
angle was halved.
'''
f = s1 * (Decimal(1) / hypot)
f = pow((Decimal(1.0) - right_triangle.other_side(f))
/ Decimal(2.0), Decimal(0.5))
return f * hypot
def num_digits_in_common(val1, val2):
'''
Calculates and returns the number of digits in common between
two numbers of type decimal. Returns 0 if the first digit, signs,
or exponent don't match.
'''
t1 = val1.as_tuple()
t2 = val2.as_tuple()
if t1.sign != t2.sign: return 0
if t1.exponent != t2.exponent: return 0
for n1 in range(len(t1.digits)):
if n1 >= len(t2.digits): break
if t1.digits[n1] != t2.digits[n1]: break
return n1
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='''
Calculates the value of PI, to a specified number of digits.
Iteratively uses a trigonometric half-angle identity, to
determine the circumference of a polygon with twice the
number of sides as the previous iteration.
''')
parser.add_argument('-d', '--digits', type=int, default=100,
help='Number of PI digits to calculate')
parser.add_argument('-g', '--digits_guard', type=int, default=100,
help='Number of guard digits, to handle rounding')
args = parser.parse_args()
decimal.getcontext().prec = args.digits + args.digits_guard
# Start with a 4-side regular polygon, a square.
f = Decimal(1.0)
sides = Decimal(4.0)
num_digits = 0
prev_num_digits = 0
iteration = 0
while num_digits < args.digits:
# Double the number of sides.
f = right_triangle.half_angle(f);
sides *= 2
# Calculate a lower and upper bound for the value of PI.
pi_lower = (f * sides) / Decimal(2)
pi_upper = (((Decimal(1) / right_triangle.other_side(f)) * f)
* sides) / Decimal(2)
# Determine and display the number of leading digits in common between
# the lower and upper bounds.
num_digits = num_digits_in_common(pi_lower, pi_upper)
print('%8i %10s %.*s'
% (iteration, num_digits, num_digits + 1, pi_lower) )
# Still numerically stable?
if num_digits < prev_num_digits:
print(numerically_unstable_msg)
raise SystemExit(1)
prev_num_digits = num_digits
iteration += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment