@@ -60,7 +60,7 @@ pakeji hisabati{
60
60
61
61
// @.EPSILON
62
62
EPSILON = unda () {
63
- rudisha 2.220446049250313e -16 ;
63
+ rudisha 0.0000000000000002220446049250313 ;
64
64
}
65
65
66
66
// Methods
@@ -80,12 +80,12 @@ pakeji hisabati{
80
80
}
81
81
82
82
// Define the precision for the approximation.
83
- fanya precision = 1 e -10 ;
83
+ fanya precision = 0.0000000001 ;
84
84
85
85
// Initial guess for the angle in radians (between 0 and π).
86
- fanya angle = @.PI / 4 ;
86
+ fanya angle = @.PI () / 4 ;
87
87
88
- wakati (true ) {
88
+ wakati (kweli ) {
89
89
fanya cosAngle = @.cos (angle );
90
90
fanya error = @.abs (cosAngle - x );
91
91
@@ -144,12 +144,12 @@ pakeji hisabati{
144
144
kama (x == 0 ) {
145
145
rudisha 0 ;
146
146
} // arctan(0) is 0
147
- kama (x == Infinity ) {
147
+ kama (x == 1 / 0 ) {
148
148
rudisha @.PI / 2 ;
149
149
}
150
- kama (x == -Infinity ) {
150
+ kama (x == -1 / 0 ) {
151
151
rudisha - @.PI / 2 ;
152
- } // arctan(-Infinity ) is -π/2
152
+ } // arctan(-Inf ) is -π/2
153
153
154
154
// Use the Taylor series expansion for arctan(x)
155
155
// arctan(x) = x - (x^3) / 3 + (x^5) / 5 - (x^7) / 7 + ...
@@ -158,7 +158,7 @@ pakeji hisabati{
158
158
fanya term = x * x ;
159
159
fanya sign = -1 ;
160
160
161
- wakati (true ) {
161
+ wakati (kweli ) {
162
162
fanya currentTerm = sign * (term / n );
163
163
164
164
kama (currentTerm == 0 ) {
@@ -209,17 +209,17 @@ pakeji hisabati{
209
209
}
210
210
211
211
//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 ) {
213
213
fanya guess = x / 2 ; // Initial guess
214
- fanya tolerance = 1 e -10 ; // Tolerance for convergence
214
+ fanya tolerance = 0.0000000001 ; // Tolerance for convergence
215
215
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 );
222
220
}
221
+
222
+ rudisha calculateNthRoot (x , n , guess , tolerance )
223
223
}
224
224
225
225
//ceil(x), rounds up to the smallest integer greater than or equal to a given number.
@@ -235,11 +235,12 @@ pakeji hisabati{
235
235
}
236
236
237
237
//cos(x), calculates the cosine of an angle in radians using the Taylor series.
238
- cos = unda (x , terms = 10 ) {
238
+ cos = unda (x ) {
239
239
// Initialize the result
240
240
fanya n = 0 ;
241
+ fanya terms = 10 ;
241
242
fanya result = 0 ;
242
-
243
+
243
244
wakati (n < terms ) {
244
245
// Calculate the numerator and denominator for the nth term
245
246
fanya numerator = 0 ;
@@ -248,10 +249,11 @@ pakeji hisabati{
248
249
} sivyo {
249
250
numerator = -x ** (2 * n + 1 );
250
251
}
251
- fanya denominator = @.factorial (2 * n );
252
+ andika (@.factorial (2 * n ));
253
+ //fanya denominator = @.factorial(2 * n);
252
254
253
255
// Add the nth term to the result
254
- result += numerator / denominator ;
256
+ // result += numerator / denominator;
255
257
256
258
n ++;
257
259
}
@@ -358,11 +360,11 @@ pakeji hisabati{
358
360
}
359
361
360
362
kama (x == -1 ) {
361
- rudisha (-Infinity );
363
+ rudisha (-Inf );
362
364
}
363
365
364
- kama (x == Infinity ) {
365
- rudisha Infinity ;
366
+ kama (x == Inf ) {
367
+ rudisha Inf ;
366
368
}
367
369
368
370
kama (x == 0 ) {
@@ -403,7 +405,7 @@ pakeji hisabati{
403
405
//max(numbers), finds the maximum value in a list of numbers.
404
406
max = unda (numbers ) {
405
407
// Initialize a variable to store the largest number
406
- fanya largest = -Infinity ;
408
+ fanya largest = -Inf ;
407
409
408
410
// Iterate through the numbers and update 'largest' kama a larger number is found
409
411
kwa num ktk numbers {
@@ -412,14 +414,14 @@ pakeji hisabati{
412
414
}
413
415
}
414
416
415
- // rudisha the largest number (or -Infinity kama there are no parameters)
417
+ // rudisha the largest number (or -Inf kama there are no parameters)
416
418
rudisha largest ;
417
419
}
418
420
419
421
//min(numbers), finds the minimum value in a list of numbers.
420
422
min = unda (numbers ) {
421
423
kama (numbers .length == 0 ) {
422
- rudisha Infinity ;
424
+ rudisha Inf ;
423
425
}
424
426
425
427
fanya minVal = numbers [0 ];
@@ -507,7 +509,7 @@ pakeji hisabati{
507
509
fanya guess = x / 2 ;
508
510
fanya tolerance = 1 e-7 ; // Tolerance for approximation
509
511
510
- wakati (true ) {
512
+ wakati (kweli ) {
511
513
fanya nextGuess = 0.5 * (guess + x / guess );
512
514
513
515
// Check kama the guess is close enough to the actual square root
@@ -533,9 +535,9 @@ pakeji hisabati{
533
535
534
536
//tanh(x), calculates the hyperbolic tangent of a number.
535
537
tanh = unda (x ) {
536
- kama (x == Infinity ) {
538
+ kama (x == Inf ) {
537
539
rudisha 1 ;
538
- } au kama (x == -Infinity ) {
540
+ } au kama (x == -Inf ) {
539
541
rudisha - 1 ;
540
542
} sivyo {
541
543
fanya expX = @.exp (x );
0 commit comments