Skip to content

Commit 7a0c63b

Browse files
authored
Add files via upload
1 parent b2e99c1 commit 7a0c63b

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

leibniz_fixedpoint.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (C) <2024>, Gary Sims
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without
5+
// modification, are permitted provided that the following conditions are met:
6+
//
7+
// 1. Redistributions of source code must retain the above copyright notice, this
8+
// list of conditions and the following disclaimer.
9+
// 2. Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14+
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20+
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
//
24+
25+
#include <stdio.h>
26+
#include <stdint.h>
27+
28+
#define FIXED_FACTOR 1000000000000000000
29+
#define FIXED_PLACES 18
30+
31+
void print_fixed_point_r(uint64_t n, int i) {
32+
uint64_t num = n;
33+
int c;
34+
35+
c = num % 10;
36+
num = num / 10;
37+
if((i<FIXED_PLACES) || (num>0))
38+
print_fixed_point_r(num, i + 1);
39+
if (i == FIXED_PLACES) {
40+
if(num==0)
41+
printf("0");
42+
printf(".");
43+
}
44+
printf("%c", c + '0');
45+
}
46+
47+
void print_fixed_point(uint64_t n) {
48+
print_fixed_point_r(n, 1);
49+
}
50+
51+
uint64_t set_fixed_point_int(uint64_t n) {
52+
return n * FIXED_FACTOR;
53+
}
54+
55+
// decimal expansion for positive fixed point rational numbers
56+
uint64_t fixed_point_dec_expansion(uint64_t dividend, uint64_t divisor) {
57+
uint64_t result = 0;
58+
if (divisor == 0)
59+
return 0;
60+
61+
uint64_t quotient = dividend / divisor;
62+
result = result + quotient;
63+
result = result * 10;
64+
65+
// Start calculating the fractional part
66+
uint64_t remainder = dividend % divisor;
67+
68+
for (int i = 0; i < FIXED_PLACES-1; i++) {
69+
// Multiply the remainder by 10 for the next digit
70+
remainder = remainder * 10;
71+
int digit = remainder / divisor;
72+
result = result + digit;
73+
result = result * 10;
74+
remainder = remainder % divisor;
75+
}
76+
return result;
77+
}
78+
79+
#define ITERATIONS 10000000
80+
81+
void main(void) {
82+
uint64_t pi = set_fixed_point_int(1);
83+
84+
int series = 3;
85+
for(int i=0;i<ITERATIONS;i++) {
86+
pi = pi - fixed_point_dec_expansion(1, series);
87+
pi = pi + fixed_point_dec_expansion(1, series+2);
88+
series = series + 4;
89+
}
90+
91+
print_fixed_point(pi*4); printf("\n");
92+
}

0 commit comments

Comments
 (0)