Skip to content

Commit ca79468

Browse files
committed
A whole bunch of tests for trig functions
1 parent 0bf498b commit ca79468

File tree

3 files changed

+157
-4
lines changed

3 files changed

+157
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
# About `rhai-sci`
66

77
This crate provides some basic scientific computing utilities for the [`Rhai`](https://rhai.rs/) scripting language,
8-
inspired by languages
9-
like MATLAB, Octave, and R. For a complete API reference, check [the docs](https://docs.rs/rhai-sci).
8+
inspired by languages like MATLAB, Octave, and R. For a complete API reference,
9+
check [the docs](https://docs.rs/rhai-sci).
1010

1111
# Install
1212

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ mod sets;
2828
pub use sets::set_functions;
2929
mod validate;
3030
pub use validate::validation_functions;
31-
3231
mod trig;
3332
pub use trig::trig_functions;
3433

src/trig.rs

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ pub mod trig_functions {
352352
/// ```typescript
353353
/// assert_eq(csch(0.0), inf)
354354
/// ```
355+
/// ```typescript
356+
/// assert_eq(csch(10.0), 1.0/sinh(10.0))
357+
/// ```
358+
/// ```typescript
359+
/// assert_eq(csch(pi/2), 1.0/sinh(pi/2))
360+
/// ```
355361
#[rhai_fn(name = "csch")]
356362
pub fn csch(radians: FLOAT) -> FLOAT {
357363
1.0 / FLOAT::sinh(radians)
@@ -361,6 +367,12 @@ pub mod trig_functions {
361367
/// ```typescript
362368
/// assert_eq(cschd(0.0), inf)
363369
/// ```
370+
/// ```typescript
371+
/// assert_eq(cschd(10.0), 1.0/sinhd(10.0))
372+
/// ```
373+
/// ```typescript
374+
/// assert_eq(cschd(90.0), 1.0/sinhd(90.0))
375+
/// ```
364376
#[rhai_fn(name = "cschd")]
365377
pub fn cschd(degrees: FLOAT) -> FLOAT {
366378
1.0 / FLOAT::sinh(deg2rad(degrees))
@@ -370,6 +382,12 @@ pub mod trig_functions {
370382
/// ```typescript
371383
/// assert_eq(acsch(inf), 0.0)
372384
/// ```
385+
/// ```typescript
386+
/// assert_eq(acsch(1.0), asinh(1.0))
387+
/// ```
388+
/// ```typescript
389+
/// assert_eq(acsch(-1.0), asinh(-1.0))
390+
/// ```
373391
#[rhai_fn(name = "acsch")]
374392
pub fn acsch(x: FLOAT) -> FLOAT {
375393
FLOAT::asinh(1.0 / x)
@@ -379,102 +397,238 @@ pub mod trig_functions {
379397
/// ```typescript
380398
/// assert_eq(acschd(inf), 0.0)
381399
/// ```
400+
/// ```typescript
401+
/// assert_eq(acschd(1.0), asinhd(1.0))
402+
/// ```
403+
/// ```typescript
404+
/// assert_eq(acschd(-1.0), asinhd(-1.0))
405+
/// ```
382406
#[rhai_fn(name = "acschd")]
383407
pub fn acschd(x: FLOAT) -> FLOAT {
384408
rad2deg(FLOAT::asinh(1.0 / x))
385409
}
386410

387411
/// Returns the secant of the argument given in radians
412+
/// ```typescript
413+
/// assert_eq(sec(0.0), 1.0);
414+
/// ```
415+
/// ```typescript
416+
/// assert_eq(sec(pi/2), 1/cos(pi/2));
417+
/// ```
418+
/// ```typescript
419+
/// assert_eq(sec(pi), -1.0);
388420
#[rhai_fn(name = "sec")]
389421
pub fn sec(radians: FLOAT) -> FLOAT {
390422
1.0 / FLOAT::cos(radians)
391423
}
392424

393425
/// Returns the secant of the argument given in degrees
426+
/// ```typescript
427+
/// assert_eq(secd(0.0), 1.0);
428+
/// ```
429+
/// ```typescript
430+
/// assert_eq(secd(90.0), 1/cosd(90.0));
431+
/// ```
432+
/// ```typescript
433+
/// assert_eq(secd(180.0), -1.0);
434+
/// ```
394435
#[rhai_fn(name = "secd")]
395436
pub fn secd(degrees: FLOAT) -> FLOAT {
396437
1.0 / FLOAT::cos(deg2rad(degrees))
397438
}
398439

399440
/// Returns the inverse secant in radians
441+
/// ```typescript
442+
/// assert_eq(asec(1.0), 0.0);
443+
/// ```
444+
/// ```typescript
445+
/// assert_eq(asec(-1.0), pi);
446+
/// ```
447+
/// ```typescript
448+
/// assert_eq(asec(0.5), acos(2.0));
449+
/// ```
400450
#[rhai_fn(name = "asec")]
401451
pub fn asec(x: FLOAT) -> FLOAT {
402452
FLOAT::acos(1.0 / x)
403453
}
404454

405455
/// Returns the inverse secant in degrees
456+
/// ```typescript
457+
/// assert_eq(asecd(1.0), 0.0);
458+
/// ```
459+
/// ```typescript
460+
/// assert_eq(asecd(-1.0), 180.0);
461+
/// ```
462+
/// ```typescript
463+
/// assert_eq(asecd(0.5), acosd(2.0));
464+
/// ```
406465
#[rhai_fn(name = "asecd")]
407466
pub fn asecd(x: FLOAT) -> FLOAT {
408467
rad2deg(FLOAT::acos(1.0 / x))
409468
}
410469

411470
/// Returns the hyperbolic secant of the argument given in radians
471+
/// ```typescript
472+
/// assert_eq(sech(0.0), 1.0);
473+
/// ```
474+
/// ```typescript
475+
/// assert_eq(sech(10.0), 1.0/cosh(10.0));
476+
/// ```
477+
/// ```typescript
478+
/// assert_eq(sech(pi/2), 1.0/cosh(pi/2));
479+
/// ```
412480
#[rhai_fn(name = "sech")]
413481
pub fn sech(radians: FLOAT) -> FLOAT {
414482
1.0 / FLOAT::cosh(radians)
415483
}
416-
417484
/// Returns the hyperbolic secant of the argument given in degrees
485+
/// ```typescript
486+
/// assert_eq(sechd(0.0), 1.0);
487+
/// ```
488+
/// ```typescript
489+
/// assert_eq(sechd(10.0), 1.0/coshd(10.0));
490+
/// ```
491+
/// ```typescript
492+
/// assert_eq(sechd(90.0), 1.0/coshd(90.0));
493+
/// ```
418494
#[rhai_fn(name = "sechd")]
419495
pub fn sechd(degrees: FLOAT) -> FLOAT {
420496
1.0 / FLOAT::cosh(deg2rad(degrees))
421497
}
422498

423499
/// Returns the inverse hyperbolic secant in radians
500+
/// ```typescript
501+
/// assert_eq(asech(1.0), 0.0);
502+
/// ```
503+
/// ```typescript
504+
/// assert_eq(asech(0.5), acosh(2.0));
505+
/// ```
506+
/// ```typescript
507+
/// assert_eq(asech(0.1), acosh(10.0));
508+
/// ```
424509
#[rhai_fn(name = "asech")]
425510
pub fn asech(x: FLOAT) -> FLOAT {
426511
FLOAT::acosh(1.0 / x)
427512
}
428513

429514
/// Returns the inverse hyperbolic secant of the argument in degrees
515+
/// ```typescript
516+
/// assert_eq(asechd(1.0), 0.0);
517+
/// ```
430518
#[rhai_fn(name = "asechd")]
431519
pub fn asechd(x: FLOAT) -> FLOAT {
432520
rad2deg(FLOAT::acosh(1.0 / x))
433521
}
434522

435523
/// Returns the cotangent of the argument given in radians
524+
/// ```typescript
525+
/// assert_approx_eq(cot(pi/4), 1.0, 1e-10);
526+
/// ```
527+
/// ```typescript
528+
/// assert_approx_eq(cot(pi/2), 0.0, 1e-10);
529+
/// ```
530+
/// ```typescript
531+
/// assert_approx_eq(cot(3*pi/4), -1.0, 1e-10);
532+
/// ```
436533
#[rhai_fn(name = "cot")]
437534
pub fn cot(radians: FLOAT) -> FLOAT {
438535
1.0 / FLOAT::tan(radians)
439536
}
440537

441538
/// Returns the cotangent of the argument given in degrees
539+
/// ```typescript
540+
/// assert_approx_eq(cotd(45.0), 1.0, 1e-10);
541+
/// ```
542+
/// ```typescript
543+
/// assert_approx_eq(cotd(90.0), 0.0, 1e-10);
544+
/// ```
545+
/// ```typescript
546+
/// assert_approx_eq(cotd(135.0), -1.0, 1e-10);
547+
/// ```
442548
#[rhai_fn(name = "cotd")]
443549
pub fn cotd(degrees: FLOAT) -> FLOAT {
444550
1.0 / FLOAT::tan(deg2rad(degrees))
445551
}
446552

447553
/// Returns the inverse of the cotangent in radians
554+
/// ```typescript
555+
/// assert_eq(acot(1.0), pi/4);
556+
/// ```
557+
/// ```typescript
558+
/// assert_eq(acot(-1.0), -pi/4);
559+
/// ```
560+
/// ```typescript
561+
/// assert_eq(acot(0.0), pi/2);
562+
/// ```
448563
#[rhai_fn(name = "acot")]
449564
pub fn acot(x: FLOAT) -> FLOAT {
450565
FLOAT::atan(1.0 / x)
451566
}
452567

453568
/// Returns the inverse of the cotangent in degrees
569+
/// ```typescript
570+
/// assert_eq(acotd(1.0), 45.0);
571+
/// ```
572+
/// ```typescript
573+
/// assert_eq(acotd(-1.0), -45.0);
574+
/// ```
575+
/// ```typescript
576+
/// assert_eq(acotd(0.0), 90.0);
577+
/// ```
454578
#[rhai_fn(name = "acotd")]
455579
pub fn acotd(x: FLOAT) -> FLOAT {
456580
rad2deg(FLOAT::atan(1.0 / x))
457581
}
458582

459583
/// Returns the hyperbolic cotangent of the argument given in radians
584+
/// ```typescript
585+
/// assert_approx_eq(coth(1.0), cosh(1.0)/sinh(1.0), 1e-10);
586+
/// ```
587+
/// ```typescript
588+
/// assert_approx_eq(coth(0.5), cosh(0.5)/sinh(0.5), 1e-10);
589+
/// ```
590+
/// ```typescript
591+
/// assert_approx_eq(coth(0.1), cosh(0.1)/sinh(0.1), 1e-10);
592+
/// ```
460593
#[rhai_fn(name = "coth")]
461594
pub fn coth(radians: FLOAT) -> FLOAT {
462595
1.0 / FLOAT::tanh(radians)
463596
}
464597

465598
/// Returns the hyperbolic cotangent of the argument given in degrees
599+
/// ```typescript
600+
/// assert_approx_eq(cothd(1.0), coshd(1.0)/sinhd(1.0), 1e-10);
601+
/// ```
602+
/// ```typescript
603+
/// assert_approx_eq(cothd(0.5), coshd(0.5)/sinhd(0.5), 1e-10);
604+
/// ```
605+
/// ```typescript
606+
/// assert_approx_eq(cothd(0.1), coshd(0.1)/sinhd(0.1), 1e-10);
607+
/// ```
466608
#[rhai_fn(name = "cothd")]
467609
pub fn cothd(degrees: FLOAT) -> FLOAT {
468610
1.0 / FLOAT::tanh(deg2rad(degrees))
469611
}
470612

471613
/// Returns the inverse hyperbolic cotangent of the argument in radians
614+
/// ```typescript
615+
/// assert_eq(acoth(1.0), atanh(1.0));
616+
/// ```
617+
/// ```typescript
618+
/// assert_eq(acoth(-1.0), atanh(-1.0));
619+
/// ```
472620
#[rhai_fn(name = "acoth")]
473621
pub fn acoth(x: FLOAT) -> FLOAT {
474622
FLOAT::atanh(1.0 / x)
475623
}
476624

477625
/// Returns the inverse hyperbolic cotangent of the argument in degrees
626+
/// ```typescript
627+
/// assert_eq(acothd(1.0), atanhd(1.0));
628+
/// ```
629+
/// ```typescript
630+
/// assert_eq(acothd(-1.0), atanhd(-1.0));
631+
/// ```
478632
#[rhai_fn(name = "acothd")]
479633
pub fn acothd(x: FLOAT) -> FLOAT {
480634
rad2deg(FLOAT::atanh(1.0 / x))

0 commit comments

Comments
 (0)