@@ -325,7 +325,6 @@ namespace steppable::__internals::arithmetic
325
325
// 2 x
326
326
result = subtract (static_cast <std::string>(constants::PI_OVER_2), result, 0 );
327
327
}
328
- result = roundOff (result, decimals);
329
328
330
329
// Convert the result as needed.
331
330
switch (mode)
@@ -342,7 +341,7 @@ namespace steppable::__internals::arithmetic
342
341
343
342
if (_x.front () == ' -' )
344
343
result = " -" + result;
345
- return result;
344
+ return roundOff ( result, decimals) ;
346
345
}
347
346
348
347
std::string asin (const std::string& x, const int decimals, const int mode)
@@ -355,49 +354,21 @@ namespace steppable::__internals::arithmetic
355
354
if (compare (abs (x, 0 ), " 0" , 0 ) == " 2" )
356
355
return " 0" ;
357
356
358
- auto x2 = power (x, " 2" , 0 );
359
- auto x3 = multiply (x2, x, 0 );
360
- auto x5 = multiply (x3, x2, 0 );
361
- auto x7 = multiply (x5, x2, 0 );
362
- std::string onePlusX;
363
- std::string oneMinusX;
364
- std::string sqrtOnePlusX;
365
- std::string sqrtOneMinusX;
366
- std::string result;
367
-
368
- // For x <= 0.5, use Taylor series.
369
- // 3 5 7
370
- // x 3x 5x
371
- // arcsin(x) = x + --- + ---- + -----
372
- // 6 40 112
373
- if (compare (abs (x, 0 ), " 0.5" , 0 ) != " 1" )
374
- {
375
- result = add (x, divide (x3, " 6" , 0 ), 0 );
376
- result = add (result, divide (multiply (x5, " 3" , 0 ), " 40" , 0 ), 0 );
377
- result = add (result, divide (multiply (x7, " 5" , 0 ), " 112" , 0 ), 0 );
378
- goto out; // NOLINT(cppcoreguidelines-avoid-goto)
379
- }
357
+ // / x
358
+ // | 1
359
+ // | ---------------- dy
360
+ // | /------|
361
+ // | / 2
362
+ // | \/ 1 - y
363
+ // / 0
364
+ auto integrand = [&](const std::string& y) {
365
+ auto y2 = power (y, " 2" , 0 );
366
+ auto oneMinusY2 = subtract (" 1" , y2, 0 );
367
+ auto denominator = root (oneMinusY2, " 2" , decimals + 1 );
368
+ return divide (" 1" , denominator, 0 , decimals + 1 );
369
+ };
370
+ auto result = calculus::romberg (integrand, " 0" , x, 10 , decimals + 2 );
380
371
381
- // Othman, S. B.; Bagul, Y. J. An Innovative Method for Approximating Arcsine Function. Preprints 2022,
382
- // 2022070388. https://doi.org/10.20944/preprints202207.0388.v1
383
- //
384
- // /-----| /-----| 5 3
385
- // arcsin(x) = 2 * 0.510774109 * (\/ 1 + x − \/ 1 − x ) + (0.239x − 0.138x + 0.005x)
386
- //
387
- // /-----| /-----| 5 3
388
- // = 1.021548218 * (\/ 1 + x − \/ 1 − x ) + (0.239x − 0.138x + 0.005x)
389
-
390
- onePlusX = add (" 1" , x, 0 );
391
- oneMinusX = subtract (" 1" , x, 0 );
392
- sqrtOnePlusX = root (onePlusX, " 2" , static_cast <long >(decimals) * 2 );
393
- sqrtOneMinusX = root (oneMinusX, " 2" , static_cast <long >(decimals) * 2 );
394
-
395
- result = multiply (" 1.021548218" , subtract (sqrtOnePlusX, sqrtOneMinusX, 0 ), 0 );
396
- result = add (result,
397
- add (multiply (" 0.239" , x5, 0 ), subtract (multiply (" 0.138" , x3, 0 ), multiply (" 0.005" , x, 0 ), 0 ), 0 ),
398
- 0 );
399
-
400
- out:
401
372
switch (mode)
402
373
{
403
374
case 1 :
@@ -486,7 +457,7 @@ int main(int _argc, const char* _argv[])
486
457
Utf8CodePage _;
487
458
ProgramArgs program (_argc, _argv);
488
459
program.addPosArg (' c' , $(" trig" , " 47dcf91b-847c-48f0-9889-f5ce1b6831e3" ), false );
489
- program.addPosArg (' n' , $(" trig" , " bcd0a3e9-3d89-4921-94b3-d7533d60911f" ), false );
460
+ program.addPosArg (' n' , $(" trig" , " bcd0a3e9-3d89-4921-94b3-d7533d60911f" ));
490
461
program.addKeywordArg (" mode" , 0 , $(" trig" , " 03fdd1f2-6ea5-49d4-ac3f-27f01f04a518" ));
491
462
program.addKeywordArg (" decimals" , 5 , $(" trig" , " d1df3b60-dac1-496c-99bb-ba763dc551df" ));
492
463
program.addSwitch (" profile" , false , $(" trig" , " 162adb13-c4b2-4418-b3df-edb6f9355d64" ));
0 commit comments