exp-rs 0.2.0

no_std expression parser, compiler, and evaluation engine for math expressions designed for embedded, with qemu examples
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
//! Built-in mathematical functions for expression evaluation.
//!
//! This module provides the implementation of all built-in functions that can be used
//! in expressions. These include common mathematical operations like trigonometric functions,
//! logarithms, exponentials, and more. The functions handle special cases like division
//! by zero and out-of-range inputs gracefully by returning appropriate values like NaN
//! or infinity.
//!
//! All functions use the `libm` crate for their implementations, which ensures
//! compatibility with no_std environments. Depending on the selected floating-point
//! precision (f32 or f64, controlled by the "f32" feature), different versions of the
//! math functions are used.

#[cfg(all(feature = "libm", feature = "f32"))]
use libm::{
    acosf as libm_acos, asinf as libm_asin, atan2f as libm_atan2, atanf as libm_atan,
    ceilf as libm_ceil, cosf as libm_cos, coshf as libm_cosh, expf as libm_exp,
    floorf as libm_floor, log10f as libm_log10, logf as libm_ln, powf as libm_pow,
    sinf as libm_sin, sinhf as libm_sinh, sqrtf as libm_sqrt, tanf as libm_tan, tanhf as libm_tanh,
};

#[cfg(all(feature = "libm", not(feature = "f32")))]
use libm::{
    acos as libm_acos, asin as libm_asin, atan as libm_atan, atan2 as libm_atan2,
    ceil as libm_ceil, cos as libm_cos, cosh as libm_cosh, exp as libm_exp, floor as libm_floor,
    log as libm_ln, log10 as libm_log10, pow as libm_pow, sin as libm_sin, sinh as libm_sinh,
    sqrt as libm_sqrt, tan as libm_tan, tanh as libm_tanh,
};

use crate::Real;

// When libm feature is not enabled, provide our own implementations
#[cfg(all(not(feature = "libm"), test))]
mod internal_math {
    use crate::Real;

    // When not using libm, provide implementations only for tests
    // In real embedded environments, these functions would need to be
    // registered explicitly by the user

    // Trigonometric functions
    pub fn libm_sin(x: Real) -> Real {
        x.sin()
    }
    pub fn libm_cos(x: Real) -> Real {
        x.cos()
    }
    pub fn libm_tan(x: Real) -> Real {
        x.tan()
    }
    pub fn libm_asin(x: Real) -> Real {
        x.asin()
    }
    pub fn libm_acos(x: Real) -> Real {
        x.acos()
    }
    pub fn libm_atan(x: Real) -> Real {
        x.atan()
    }
    pub fn libm_atan2(y: Real, x: Real) -> Real {
        y.atan2(x)
    }

    // Hyperbolic functions
    pub fn libm_sinh(x: Real) -> Real {
        x.sinh()
    }
    pub fn libm_cosh(x: Real) -> Real {
        x.cosh()
    }
    pub fn libm_tanh(x: Real) -> Real {
        x.tanh()
    }

    // Exponential and logarithmic functions
    pub fn libm_exp(x: Real) -> Real {
        x.exp()
    }
    pub fn libm_ln(x: Real) -> Real {
        x.ln()
    }
    pub fn libm_log10(x: Real) -> Real {
        x.log10()
    }

    // Power and root functions
    pub fn libm_pow(x: Real, y: Real) -> Real {
        x.powf(y)
    }
    pub fn libm_sqrt(x: Real) -> Real {
        x.sqrt()
    }

    // Rounding functions
    pub fn libm_ceil(x: Real) -> Real {
        x.ceil()
    }
    pub fn libm_floor(x: Real) -> Real {
        x.floor()
    }
}

// Import our math functions when libm is disabled
#[cfg(all(not(feature = "libm"), test))]
use internal_math::*;

/// Dummy function that panics when called.
///
/// This function is used internally as a placeholder for uninitialized functions.
/// It should never be called in normal operation.
pub fn dummy(_: Real, _: Real) -> Real {
    panic!("called dummy!")
}

/// Returns the maximum of two values.
///
/// # Parameters
///
/// * `a` - First value
/// * `b` - Second value
///
/// # Returns
///
/// The larger of `a` and `b`.
pub fn max(a: Real, b: Real) -> Real {
    if a > b { a } else { b }
}

/// Adds two values.
///
/// # Parameters
///
/// * `a` - First value
/// * `b` - Second value
///
/// # Returns
///
/// The sum of `a` and `b`.
pub fn add(a: Real, b: Real) -> Real {
    a + b
}

/// Returns the minimum of two values.
///
/// # Parameters
///
/// * `a` - First value
/// * `b` - Second value
///
/// # Returns
///
/// The smaller of `a` and `b`.
pub fn min(a: Real, b: Real) -> Real {
    if a < b { a } else { b }
}

/// Subtracts the second value from the first.
///
/// # Parameters
///
/// * `a` - Value to subtract from
/// * `b` - Value to subtract
///
/// # Returns
///
/// The difference `a - b`.
pub fn sub(a: Real, b: Real) -> Real {
    a - b
}

/// Multiplies two values.
///
/// # Parameters
///
/// * `a` - First value
/// * `b` - Second value
///
/// # Returns
///
/// The product of `a` and `b`.
pub fn mul(a: Real, b: Real) -> Real {
    a * b
}

/// Divides the first value by the second.
///
/// This function handles division by zero gracefully by returning:
/// - NaN for 0/0
/// - Positive infinity for positive/0
/// - Negative infinity for negative/0
///
/// # Parameters
///
/// * `a` - Numerator
/// * `b` - Denominator
///
/// # Returns
///
/// The quotient `a / b`, or appropriate value for division by zero.
pub fn div(a: Real, b: Real) -> Real {
    if b == 0.0 {
        if a == 0.0 {
            #[cfg(feature = "f32")]
            return f32::NAN; // 0/0 is NaN
            #[cfg(not(feature = "f32"))]
            return f64::NAN; // 0/0 is NaN
        } else if a > 0.0 {
            #[cfg(feature = "f32")]
            return f32::INFINITY; // Positive number divided by zero is positive infinity
            #[cfg(not(feature = "f32"))]
            return f64::INFINITY; // Positive number divided by zero is positive infinity
        } else {
            #[cfg(feature = "f32")]
            return f32::NEG_INFINITY; // Negative number divided by zero is negative infinity
            #[cfg(not(feature = "f32"))]
            return f64::NEG_INFINITY; // Negative number divided by zero is negative infinity
        }
    } else {
        a / b
    }
}
pub fn fmod(a: Real, b: Real) -> Real {
    a % b
}
pub fn neg(a: Real, _: Real) -> Real {
    -a
}
pub fn comma(_: Real, b: Real) -> Real {
    b
}
pub fn abs(a: Real, _: Real) -> Real {
    a.abs()
}
#[cfg(feature = "libm")]
pub fn acos(a: Real, _: Real) -> Real {
    if !(-1.0..=1.0).contains(&a) {
        #[cfg(feature = "f32")]
        return f32::NAN; // acos is only defined for inputs between -1 and 1
        #[cfg(not(feature = "f32"))]
        return f64::NAN; // acos is only defined for inputs between -1 and 1
    } else {
        libm_acos(a)
    }
}

#[cfg(all(not(feature = "libm"), test))]
pub fn acos(a: Real, _: Real) -> Real {
    if !(-1.0..=1.0).contains(&a) {
        #[cfg(feature = "f32")]
        return f32::NAN;
        #[cfg(not(feature = "f32"))]
        return f64::NAN;
    } else {
        libm_acos(a)
    }
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn acos(_: Real, _: Real) -> Real {
    // In no_std without libm, this function would need to be registered by the user
    panic!("acos requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn asin(a: Real, _: Real) -> Real {
    if !(-1.0..=1.0).contains(&a) {
        #[cfg(feature = "f32")]
        return f32::NAN; // asin is only defined for inputs between -1 and 1
        #[cfg(not(feature = "f32"))]
        return f64::NAN; // asin is only defined for inputs between -1 and 1
    } else {
        libm_asin(a)
    }
}

#[cfg(all(not(feature = "libm"), test))]
pub fn asin(a: Real, _: Real) -> Real {
    if !(-1.0..=1.0).contains(&a) {
        #[cfg(feature = "f32")]
        return f32::NAN;
        #[cfg(not(feature = "f32"))]
        return f64::NAN;
    } else {
        libm_asin(a)
    }
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn asin(_: Real, _: Real) -> Real {
    panic!("asin requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn atan(a: Real, _: Real) -> Real {
    libm_atan(a)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn atan(a: Real, _: Real) -> Real {
    libm_atan(a)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn atan(_: Real, _: Real) -> Real {
    panic!("atan requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn atan2(a: Real, b: Real) -> Real {
    // atan2 takes y,x order (not x,y)
    #[cfg(test)]
    println!("atan2 called with a={}, b={}", a, b);

    let result = libm_atan2(a, b); // Don't swap the arguments

    #[cfg(test)]
    println!("atan2 result: {}", result);

    result
}

#[cfg(all(not(feature = "libm"), test))]
pub fn atan2(a: Real, b: Real) -> Real {
    libm_atan2(a, b)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn atan2(_: Real, _: Real) -> Real {
    panic!("atan2 requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn ceil(a: Real, _: Real) -> Real {
    libm_ceil(a)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn ceil(a: Real, _: Real) -> Real {
    libm_ceil(a)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn ceil(a: Real, _: Real) -> Real {
    // Simple implementation that works without libm
    let i = a as i64 as Real;
    if a > 0.0 && a > i { i + 1.0 } else { i }
}

#[cfg(feature = "libm")]
pub fn cos(a: Real, _: Real) -> Real {
    libm_cos(a)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn cos(a: Real, _: Real) -> Real {
    libm_cos(a)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn cos(_: Real, _: Real) -> Real {
    panic!("cos requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn cosh(a: Real, _: Real) -> Real {
    libm_cosh(a)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn cosh(a: Real, _: Real) -> Real {
    libm_cosh(a)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn cosh(_: Real, _: Real) -> Real {
    panic!("cosh requires libm or custom implementation")
}

pub fn e(_: Real, _: Real) -> Real {
    crate::constants::E
}

#[cfg(feature = "libm")]
pub fn exp(a: Real, _: Real) -> Real {
    libm_exp(a)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn exp(a: Real, _: Real) -> Real {
    libm_exp(a)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn exp(_: Real, _: Real) -> Real {
    panic!("exp requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn floor(a: Real, _: Real) -> Real {
    libm_floor(a)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn floor(a: Real, _: Real) -> Real {
    libm_floor(a)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn floor(a: Real, _: Real) -> Real {
    // Simple implementation that works without libm
    let i = a as i64 as Real;
    if a < 0.0 && a < i { i - 1.0 } else { i }
}

#[cfg(feature = "libm")]
pub fn ln(a: Real, _: Real) -> Real {
    if a <= 0.0 {
        #[cfg(feature = "f32")]
        return f32::NAN; // Natural log of zero or negative number is undefined
        #[cfg(not(feature = "f32"))]
        return f64::NAN; // Natural log of zero or negative number is undefined
    } else {
        libm_ln(a)
    }
}

#[cfg(all(not(feature = "libm"), test))]
pub fn ln(a: Real, _: Real) -> Real {
    if a <= 0.0 {
        #[cfg(feature = "f32")]
        return f32::NAN;
        #[cfg(not(feature = "f32"))]
        return f64::NAN;
    } else {
        libm_ln(a)
    }
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn ln(_: Real, _: Real) -> Real {
    panic!("ln requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn log(a: Real, _: Real) -> Real {
    if a <= 0.0 {
        #[cfg(feature = "f32")]
        return f32::NAN; // Log of zero or negative number is undefined
        #[cfg(not(feature = "f32"))]
        return f64::NAN; // Log of zero or negative number is undefined
    } else {
        libm_log10(a)
    }
}

#[cfg(all(not(feature = "libm"), test))]
pub fn log(a: Real, _: Real) -> Real {
    if a <= 0.0 {
        #[cfg(feature = "f32")]
        return f32::NAN;
        #[cfg(not(feature = "f32"))]
        return f64::NAN;
    } else {
        libm_log10(a)
    }
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn log(_: Real, _: Real) -> Real {
    panic!("log requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn log10(a: Real, _: Real) -> Real {
    if a <= 0.0 {
        #[cfg(feature = "f32")]
        return f32::NAN; // Log10 of zero or negative number is undefined
        #[cfg(not(feature = "f32"))]
        return f64::NAN; // Log10 of zero or negative number is undefined
    } else {
        libm_log10(a)
    }
}

#[cfg(all(not(feature = "libm"), test))]
pub fn log10(a: Real, _: Real) -> Real {
    if a <= 0.0 {
        #[cfg(feature = "f32")]
        return f32::NAN;
        #[cfg(not(feature = "f32"))]
        return f64::NAN;
    } else {
        libm_log10(a)
    }
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn log10(_: Real, _: Real) -> Real {
    panic!("log10 requires libm or custom implementation")
}

pub fn pi(_: Real, _: Real) -> Real {
    crate::constants::PI
}

/// Raises a value to a power.
///
/// This function computes `a` raised to the power of `b` (a^b).
/// It handles various special cases and edge conditions:
///
/// - 0^0 = 1 (by mathematical convention)
/// - Negative base with non-integer exponent returns NaN
/// - Very large exponents that would cause overflow return infinity
/// - Very small values that would cause underflow return 0
///
/// # Parameters
///
/// * `a` - Base value
/// * `b` - Exponent
///
/// # Returns
///
/// The value of `a` raised to the power of `b`.
#[cfg(feature = "libm")]
pub fn pow(a: Real, b: Real) -> Real {
    #[cfg(test)]
    println!("pow function called with a={}, b={}", a, b);

    // Handle special cases
    if a == 0.0 && b == 0.0 {
        return 1.0; // 0^0 = 1 by convention
    }

    if a < 0.0 && b != libm_floor(b) {
        // Negative base with non-integer exponent is not a real number
        #[cfg(feature = "f32")]
        return f32::NAN;
        #[cfg(not(feature = "f32"))]
        return f64::NAN;
    }

    // Check for potential overflow
    #[cfg(feature = "f32")]
    if a.abs() > 1.0 && b > 88.0 {
        return if a > 0.0 {
            f32::INFINITY
        } else {
            f32::NEG_INFINITY
        };
    }

    #[cfg(not(feature = "f32"))]
    if a.abs() > 1.0 && b > 709.0 {
        return if a > 0.0 {
            f64::INFINITY
        } else {
            f64::NEG_INFINITY
        };
    }

    #[cfg(feature = "f32")]
    if a.abs() < 1.0 && b < -88.0 {
        return 0.0; // Underflow to zero
    }

    #[cfg(not(feature = "f32"))]
    if a.abs() < 1.0 && b < -709.0 {
        return 0.0; // Underflow to zero
    }

    libm_pow(a, b)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn pow(a: Real, b: Real) -> Real {
    // Simplified version for tests
    if a == 0.0 && b == 0.0 {
        return 1.0;
    }

    libm_pow(a, b)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn pow(a: Real, b: Real) -> Real {
    // Basic implementation for non-libm, non-test builds
    // Handles only a few special cases
    if b == 0.0 {
        return 1.0;
    }
    if b == 1.0 {
        return a;
    }
    if b == 2.0 {
        return a * a;
    }
    if b == 0.5 && a >= 0.0 {
        return sqrt(a, 0.0);
    }
    if b == -1.0 {
        return 1.0 / a;
    }

    panic!("pow requires libm or custom implementation for general cases")
}

#[cfg(feature = "libm")]
pub fn sin(a: Real, _: Real) -> Real {
    libm_sin(a)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn sin(a: Real, _: Real) -> Real {
    libm_sin(a)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn sin(_: Real, _: Real) -> Real {
    panic!("sin requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn sinh(a: Real, _: Real) -> Real {
    libm_sinh(a)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn sinh(a: Real, _: Real) -> Real {
    libm_sinh(a)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn sinh(_: Real, _: Real) -> Real {
    panic!("sinh requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn sqrt(a: Real, _: Real) -> Real {
    if a < 0.0 {
        #[cfg(feature = "f32")]
        return f32::NAN; // Square root of negative number is NaN
        #[cfg(not(feature = "f32"))]
        return f64::NAN; // Square root of negative number is NaN
    } else {
        libm_sqrt(a)
    }
}

#[cfg(all(not(feature = "libm"), test))]
pub fn sqrt(a: Real, _: Real) -> Real {
    if a < 0.0 {
        #[cfg(feature = "f32")]
        return f32::NAN;
        #[cfg(not(feature = "f32"))]
        return f64::NAN;
    } else {
        libm_sqrt(a)
    }
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn sqrt(a: Real, _: Real) -> Real {
    if a < 0.0 {
        #[cfg(feature = "f32")]
        return f32::NAN;
        #[cfg(not(feature = "f32"))]
        return f64::NAN;
    }

    // Very simplified Newton-Raphson for sqrt approximation
    if a == 0.0 {
        return 0.0;
    }
    let x = a;
    let mut y = 1.0;

    // Just a couple of iterations
    for _ in 0..3 {
        y = 0.5 * (y + x / y);
    }

    y
}

#[cfg(feature = "libm")]
pub fn tan(a: Real, _: Real) -> Real {
    libm_tan(a)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn tan(a: Real, _: Real) -> Real {
    libm_tan(a)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn tan(_: Real, _: Real) -> Real {
    panic!("tan requires libm or custom implementation")
}

#[cfg(feature = "libm")]
pub fn tanh(a: Real, _: Real) -> Real {
    libm_tanh(a)
}

#[cfg(all(not(feature = "libm"), test))]
pub fn tanh(a: Real, _: Real) -> Real {
    libm_tanh(a)
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn tanh(_: Real, _: Real) -> Real {
    panic!("tanh requires libm or custom implementation")
}

pub fn sign(a: Real, _: Real) -> Real {
    if a > 0.0 {
        1.0
    } else if a < 0.0 {
        -1.0
    } else {
        0.0
    }
}

#[cfg(feature = "libm")]
pub fn round(a: Real, _: Real) -> Real {
    #[cfg(feature = "f32")]
    {
        libm::roundf(a)
    }
    #[cfg(not(feature = "f32"))]
    {
        libm::round(a)
    }
}

#[cfg(all(not(feature = "libm"), test))]
pub fn round(a: Real, _: Real) -> Real {
    a.round()
}

#[cfg(all(not(feature = "libm"), not(test)))]
pub fn round(_: Real, _: Real) -> Real {
    panic!("round requires libm or custom implementation")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[should_panic(expected = "called dummy!")]
    fn test_dummy_panics() {
        dummy(1.0, 2.0);
    }

    #[test]
    fn test_sub() {
        assert_eq!(sub(5.0, 3.0), 2.0);
    }

    #[test]
    fn test_mul() {
        assert_eq!(mul(2.0, 3.0), 6.0);
    }

    #[test]
    fn test_div() {
        assert_eq!(div(6.0, 3.0), 2.0);
    }

    #[test]
    fn test_fmod() {
        assert_eq!(fmod(7.0, 4.0), 3.0);
    }

    #[test]
    fn test_neg() {
        assert_eq!(neg(5.0, 0.0), -5.0);
    }

    #[test]
    fn test_comma() {
        assert_eq!(comma(1.0, 2.0), 2.0);
    }

    #[test]
    fn test_abs() {
        assert_eq!(abs(-5.0, 0.0), 5.0);
    }

    #[test]
    fn test_acos() {
        assert!((acos(1.0, 0.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_asin() {
        assert!((asin(0.0, 0.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_atan() {
        assert!((atan(0.0, 0.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_atan2() {
        // Test basic cases with direct function calls
        #[cfg(feature = "f32")]
        assert!((atan2(1.0, 1.0) - core::f32::consts::FRAC_PI_4).abs() < 1e-6);
        #[cfg(not(feature = "f32"))]
        assert!((atan2(1.0, 1.0) - core::f64::consts::FRAC_PI_4).abs() < 1e-10);

        // Test more cases to verify the function works correctly
        // atan2(y, x) where:

        // Quadrant 1: y > 0, x > 0
        assert!(
            (atan2(1.0, 2.0) - 0.4636476090008061).abs() < 1e-10,
            "atan2(1.0, 2.0) should be approximately 0.4636"
        );

        // Quadrant 2: y > 0, x < 0
        #[cfg(feature = "f32")]
        assert!(
            (atan2(1.0, -1.0) - (3.0 * core::f32::consts::FRAC_PI_4)).abs() < 1e-6,
            "atan2(1.0, -1.0) should be approximately 3Ï€/4"
        );
        #[cfg(not(feature = "f32"))]
        assert!(
            (atan2(1.0, -1.0) - (3.0 * core::f64::consts::FRAC_PI_4)).abs() < 1e-10,
            "atan2(1.0, -1.0) should be approximately 3Ï€/4"
        );

        // Quadrant 3: y < 0, x < 0
        #[cfg(feature = "f32")]
        assert!(
            (atan2(-1.0, -1.0) + (3.0 * core::f32::consts::FRAC_PI_4)).abs() < 1e-6,
            "atan2(-1.0, -1.0) should be approximately -3Ï€/4"
        );
        #[cfg(not(feature = "f32"))]
        assert!(
            (atan2(-1.0, -1.0) + (3.0 * core::f64::consts::FRAC_PI_4)).abs() < 1e-10,
            "atan2(-1.0, -1.0) should be approximately -3Ï€/4"
        );

        // Quadrant 4: y < 0, x > 0
        #[cfg(feature = "f32")]
        assert!(
            (atan2(-1.0, 1.0) + core::f32::consts::FRAC_PI_4).abs() < 1e-6,
            "atan2(-1.0, 1.0) should be approximately -Ï€/4"
        );
        #[cfg(not(feature = "f32"))]
        assert!(
            (atan2(-1.0, 1.0) + core::f64::consts::FRAC_PI_4).abs() < 1e-10,
            "atan2(-1.0, 1.0) should be approximately -Ï€/4"
        );

        // Special cases
        assert!(
            (atan2(0.0, 1.0) - 0.0).abs() < 1e-10,
            "atan2(0.0, 1.0) should be 0"
        );

        #[cfg(feature = "f32")]
        assert!(
            (atan2(1.0, 0.0) - core::f32::consts::FRAC_PI_2).abs() < 1e-6,
            "atan2(1.0, 0.0) should be π/2"
        );
        #[cfg(not(feature = "f32"))]
        assert!(
            (atan2(1.0, 0.0) - core::f64::consts::FRAC_PI_2).abs() < 1e-10,
            "atan2(1.0, 0.0) should be π/2"
        );

        #[cfg(feature = "f32")]
        assert!(
            (atan2(0.0, -1.0) - core::f32::consts::PI).abs() < 1e-6,
            "atan2(0.0, -1.0) should be π"
        );
        #[cfg(not(feature = "f32"))]
        assert!(
            (atan2(0.0, -1.0) - core::f64::consts::PI).abs() < 1e-10,
            "atan2(0.0, -1.0) should be π"
        );

        #[cfg(feature = "f32")]
        assert!(
            (atan2(-1.0, 0.0) + core::f32::consts::FRAC_PI_2).abs() < 1e-6,
            "atan2(-1.0, 0.0) should be -Ï€/2"
        );
        #[cfg(not(feature = "f32"))]
        assert!(
            (atan2(-1.0, 0.0) + core::f64::consts::FRAC_PI_2).abs() < 1e-10,
            "atan2(-1.0, 0.0) should be -Ï€/2"
        );

        // Print debug information
        println!("atan2(1.0, 2.0) = {}", atan2(1.0, 2.0));
        println!("atan2(2.0, 1.0) = {}", atan2(2.0, 1.0));
    }

    #[test]
    fn test_ceil() {
        assert_eq!(ceil(2.3, 0.0), 3.0);
    }

    #[test]
    fn test_cos() {
        assert!((cos(0.0, 0.0) - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_cosh() {
        assert!((cosh(0.0, 0.0) - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_e() {
        #[cfg(feature = "f32")]
        assert!((e(0.0, 0.0) - core::f32::consts::E).abs() < 1e-6);
        #[cfg(not(feature = "f32"))]
        assert!((e(0.0, 0.0) - core::f64::consts::E).abs() < 1e-10);
    }

    #[test]
    fn test_exp() {
        #[cfg(feature = "f32")]
        assert!((exp(1.0, 0.0) - core::f32::consts::E).abs() < 1e-6);
        #[cfg(not(feature = "f32"))]
        assert!((exp(1.0, 0.0) - core::f64::consts::E).abs() < 1e-10);
    }

    #[test]
    fn test_floor() {
        assert_eq!(floor(2.7, 0.0), 2.0);
    }

    #[test]
    fn test_ln() {
        #[cfg(feature = "f32")]
        assert!((ln(core::f32::consts::E, 0.0) - 1.0).abs() < 1e-6);
        #[cfg(not(feature = "f32"))]
        assert!((ln(core::f64::consts::E, 0.0) - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_log() {
        assert!((log(1000.0, 0.0) - 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_log10() {
        assert!((log10(1000.0, 0.0) - 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_pi() {
        #[cfg(feature = "f32")]
        assert!((pi(0.0, 0.0) - core::f32::consts::PI).abs() < 1e-6);
        #[cfg(not(feature = "f32"))]
        assert!((pi(0.0, 0.0) - core::f64::consts::PI).abs() < 1e-10);
    }

    #[test]
    fn test_pow() {
        assert_eq!(pow(2.0, 3.0), 8.0);
    }

    #[test]
    fn test_sin() {
        assert!((sin(0.0, 0.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_sinh() {
        assert!((sinh(0.0, 0.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_sqrt() {
        assert_eq!(sqrt(4.0, 0.0), 2.0);
    }

    #[test]
    fn test_tan() {
        assert!((tan(0.0, 0.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_tanh() {
        assert!((tanh(0.0, 0.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_sign() {
        assert_eq!(sign(5.0, 0.0), 1.0);
        assert_eq!(sign(-3.0, 0.0), -1.0);
        assert_eq!(sign(0.0, 0.0), 0.0);
    }
}