Skip to content

Commit 01d1af4

Browse files
author
Victor kariuki
committed
changed root to recursion instead of while
1 parent a5bb240 commit 01d1af4

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

third_party/math/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ To use the `pakeji hisabati` package follow the steps below:
2020
Example of calling the package methods:
2121
```nuru
2222
andika(hisabati.e())
23-
2423
## What is in
2524
This package covers a wide range of mathematical operations, including `basic arithmetic`, `trigonometry`, `exponential and logarithmic functions`, `rounding and comparison operations`, as well as some `utility and array operations`.
2625

third_party/math/hisabati.nr

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pakeji hisabati{
6060

6161
// @.EPSILON
6262
EPSILON = unda() {
63-
rudisha 2.220446049250313e-16;
63+
rudisha 0.0000000000000002220446049250313;
6464
}
6565

6666
// Methods
@@ -80,12 +80,12 @@ pakeji hisabati{
8080
}
8181

8282
// Define the precision for the approximation.
83-
fanya precision = 1e-10;
83+
fanya precision = 0.0000000001;
8484

8585
// Initial guess for the angle in radians (between 0 and π).
86-
fanya angle = @.PI / 4;
86+
fanya angle = @.PI() / 4;
8787

88-
wakati(true) {
88+
wakati(kweli) {
8989
fanya cosAngle = @.cos(angle);
9090
fanya error = @.abs(cosAngle - x);
9191

@@ -144,12 +144,12 @@ pakeji hisabati{
144144
kama(x == 0) {
145145
rudisha 0;
146146
} // arctan(0) is 0
147-
kama(x == Infinity) {
147+
kama(x == 1/0) {
148148
rudisha @.PI / 2;
149149
}
150-
kama(x == -Infinity) {
150+
kama(x == -1/0) {
151151
rudisha - @.PI / 2;
152-
} // arctan(-Infinity) is -π/2
152+
} // arctan(-Inf) is -π/2
153153

154154
// Use the Taylor series expansion for arctan(x)
155155
// arctan(x) = x - (x^3) / 3 + (x^5) / 5 - (x^7) / 7 + ...
@@ -158,7 +158,7 @@ pakeji hisabati{
158158
fanya term = x * x;
159159
fanya sign = -1;
160160

161-
wakati(true) {
161+
wakati(kweli) {
162162
fanya currentTerm = sign * (term / n);
163163

164164
kama(currentTerm == 0) {
@@ -209,17 +209,17 @@ pakeji hisabati{
209209
}
210210

211211
//root(x, n), calculates the nth root of a number using the Newton-Raphson method.
212-
root = unda(x, n) {
212+
root = unda(x, n) {
213213
fanya guess = x / 2; // Initial guess
214-
fanya tolerance = 1e-10; // Tolerance for convergence
214+
fanya tolerance = 0.0000000001; // Tolerance for convergence
215215

216-
wakati(true) {
217-
fanya nextGuess = ((n - 1) * guess + x / guess ** n - 1) / n;
218-
kama(@.abs(nextGuess - guess) < tolerance) {
219-
rudisha nextGuess;
220-
}
221-
guess = nextGuess;
216+
fanya calculateNthRoot = unda(x, n, guess, tolerance) {
217+
fanya nextGuess = ((n - 1) * guess + x / (guess ** (n - 1))) / n;
218+
kama (abs(nextGuess - guess) < tolerance) {rudisha nextGuess};
219+
rudisha calculateNthRoot(x, n, nextGuess, tolerance);
222220
}
221+
222+
rudisha calculateNthRoot(x, n, guess, tolerance)
223223
}
224224

225225
//ceil(x), rounds up to the smallest integer greater than or equal to a given number.
@@ -235,11 +235,12 @@ pakeji hisabati{
235235
}
236236

237237
//cos(x), calculates the cosine of an angle in radians using the Taylor series.
238-
cos = unda(x, terms = 10) {
238+
cos = unda(x) {
239239
// Initialize the result
240240
fanya n = 0;
241+
fanya terms = 10;
241242
fanya result = 0;
242-
243+
243244
wakati(n < terms) {
244245
// Calculate the numerator and denominator for the nth term
245246
fanya numerator = 0;
@@ -248,10 +249,11 @@ pakeji hisabati{
248249
} sivyo {
249250
numerator = -x ** (2 * n + 1);
250251
}
251-
fanya denominator = @.factorial(2 * n);
252+
andika(@.factorial(2 * n));
253+
//fanya denominator = @.factorial(2 * n);
252254

253255
// Add the nth term to the result
254-
result += numerator / denominator;
256+
//result += numerator / denominator;
255257

256258
n++;
257259
}
@@ -358,11 +360,11 @@ pakeji hisabati{
358360
}
359361

360362
kama(x == -1) {
361-
rudisha(-Infinity);
363+
rudisha(-Inf);
362364
}
363365

364-
kama(x == Infinity) {
365-
rudisha Infinity;
366+
kama(x == Inf) {
367+
rudisha Inf;
366368
}
367369

368370
kama(x == 0) {
@@ -403,7 +405,7 @@ pakeji hisabati{
403405
//max(numbers), finds the maximum value in a list of numbers.
404406
max = unda(numbers) {
405407
// Initialize a variable to store the largest number
406-
fanya largest = -Infinity;
408+
fanya largest = -Inf;
407409

408410
// Iterate through the numbers and update 'largest' kama a larger number is found
409411
kwa num ktk numbers {
@@ -412,14 +414,14 @@ pakeji hisabati{
412414
}
413415
}
414416

415-
// rudisha the largest number (or -Infinity kama there are no parameters)
417+
// rudisha the largest number (or -Inf kama there are no parameters)
416418
rudisha largest;
417419
}
418420

419421
//min(numbers), finds the minimum value in a list of numbers.
420422
min = unda(numbers) {
421423
kama(numbers.length == 0) {
422-
rudisha Infinity;
424+
rudisha Inf;
423425
}
424426

425427
fanya minVal = numbers[0];
@@ -507,7 +509,7 @@ pakeji hisabati{
507509
fanya guess = x / 2;
508510
fanya tolerance = 1e-7; // Tolerance for approximation
509511

510-
wakati(true) {
512+
wakati(kweli) {
511513
fanya nextGuess = 0.5 * (guess + x / guess);
512514

513515
// Check kama the guess is close enough to the actual square root
@@ -533,9 +535,9 @@ pakeji hisabati{
533535

534536
//tanh(x), calculates the hyperbolic tangent of a number.
535537
tanh = unda(x) {
536-
kama(x == Infinity) {
538+
kama(x == Inf) {
537539
rudisha 1;
538-
} au kama(x == -Infinity) {
540+
} au kama(x == -Inf) {
539541
rudisha - 1;
540542
} sivyo {
541543
fanya expX = @.exp(x);

0 commit comments

Comments
 (0)