aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math/number/frac.lux
blob: c3203335a88425b554d0be8d1a187686d2abdcd9 (plain)
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
(.using
 [library
  [lux (.except nat int rev)
   ["@" target]
   [abstract
    [hash (.only Hash)]
    [monoid (.only Monoid)]
    [equivalence (.only Equivalence)]
    [codec (.only Codec)]
    [predicate (.only Predicate)]
    [order (.only Order)]
    [monad (.only do)]]
   [control
    ["[0]" maybe]
    ["[0]" try (.only Try)]]
   [data
    ["[0]" text]]
   [macro
    ["^" pattern]]]]
 ["[0]" //
  ["[1][0]" i64]
  ["[1][0]" nat]
  ["[1][0]" int]
  ["[1][0]" rev]])

(with_template [<name> <value>]
  [(def .public <name>
     <value>)]

  [e   +2.7182818284590452354]
  ... ["π is wrong!" by Bob Palais](https://www.math.utah.edu/~palais/pi.html)
  [pi  +3.14159265358979323846]
  ... [The Tau Manifesto](https://tauday.com/tau-manifesto)
  [tau +6.28318530717958647692]
  )

(for @.old
     (these (with_template [<name> <method>]
              [(def .public (<name> it)
                 (-> Frac Frac)
                 (<method> it))]

              [cos   "jvm invokestatic:java.lang.Math:cos:double"]
              [sin   "jvm invokestatic:java.lang.Math:sin:double"]
              [tan   "jvm invokestatic:java.lang.Math:tan:double"]

              [acos  "jvm invokestatic:java.lang.Math:acos:double"]
              [asin  "jvm invokestatic:java.lang.Math:asin:double"]
              [atan  "jvm invokestatic:java.lang.Math:atan:double"]
              
              [exp   "jvm invokestatic:java.lang.Math:exp:double"]
              [log   "jvm invokestatic:java.lang.Math:log:double"]
              
              [ceil  "jvm invokestatic:java.lang.Math:ceil:double"]
              [floor "jvm invokestatic:java.lang.Math:floor:double"]

              [root_2 "jvm invokestatic:java.lang.Math:sqrt:double"]
              [root_3 "jvm invokestatic:java.lang.Math:cbrt:double"]
              )
            (def .public (pow param subject)
              (-> Frac Frac Frac)
              ("jvm invokestatic:java.lang.Math:pow:double,double" subject param)))

     @.jvm
     (these (def !double
              (template (_ value)
                [(|> value
                     (as (Primitive "java.lang.Double"))
                     "jvm object cast")]))
            
            (def !frac
              (template (_ value)
                [(|> value
                     "jvm object cast"
                     (is (Primitive "java.lang.Double"))
                     (as Frac))]))
            
            (with_template [<name> <method>]
              [(def .public <name>
                 (-> Frac Frac)
                 (|>> !double
                      ["D"]
                      ("jvm member invoke static" [] "java.lang.Math" <method> [])
                      !frac))]

              [cos   "cos"]
              [sin   "sin"]
              [tan   "tan"]

              [acos  "acos"]
              [asin  "asin"]
              [atan  "atan"]
              
              [exp   "exp"]
              [log   "log"]
              
              [ceil  "ceil"]
              [floor "floor"]
              
              [root_2 "sqrt"]
              [root_3 "cbrt"]
              )
            
            (def .public (pow param subject)
              (-> Frac Frac Frac)
              (|> ("jvm member invoke static" [] "java.lang.Math" "pow" []
                   ["D" (!double subject)] ["D" (!double param)])
                  !frac)))

     @.js
     (these (with_template [<name> <method>]
              [(def .public <name>
                 (-> Frac Frac)
                 (|>> []
                      ("js apply" ("js constant" <method>))
                      (as Frac)))]

              [cos   "Math.cos"]
              [sin   "Math.sin"]
              [tan   "Math.tan"]

              [acos  "Math.acos"]
              [asin  "Math.asin"]
              [atan  "Math.atan"]
              
              [exp   "Math.exp"]
              [log   "Math.log"]
              
              [ceil  "Math.ceil"]
              [floor "Math.floor"]

              [root_2 "Math.sqrt"]
              [root_3 "Math.cbrt"]
              )
            
            (def .public (pow param subject)
              (-> Frac Frac Frac)
              (as Frac ("js apply" ("js constant" "Math.pow") [subject param]))))

     @.python
     (these (with_template [<name> <method>]
              [(def .public <name>
                 (-> Frac Frac)
                 (|>> []
                      ("python object do" <method> ("python import" "math"))
                      (as Frac)))]

              [cos   "cos"]
              [sin   "sin"]
              [tan   "tan"]

              [acos  "acos"]
              [asin  "asin"]
              [atan  "atan"]
              
              [exp   "exp"]
              [log   "log"]
              
              [ceil  "ceil"]
              [floor "floor"]

              [root_2 "sqrt"]
              )
            
            (def .public (pow param subject)
              (-> Frac Frac Frac)
              (as Frac ("python object do" "pow" ("python import" "math") [subject param])))

            (def .public (root_3 it)
              (-> Frac Frac)
              (if ("lux f64 <" +0.0 it)
                (|> it
                    ("lux f64 *" -1.0)
                    (..pow ("lux f64 /" +3.0 +1.0))
                    ("lux f64 *" -1.0))
                (|> it
                    (..pow ("lux f64 /" +3.0 +1.0))))))

     @.lua
     (these (with_template [<name> <method>]
              [(def .public <name>
                 (-> Frac Frac)
                 (|>> []
                      ("lua apply" ("lua constant" <method>))
                      (as Frac)))]

              [cos   "math.cos"]
              [sin   "math.sin"]
              [tan   "math.tan"]

              [acos  "math.acos"]
              [asin  "math.asin"]
              [atan  "math.atan"]
              
              [exp   "math.exp"]
              [log   "math.log"]
              
              [ceil  "math.ceil"]
              [floor "math.floor"]

              [root_2 "math.sqrt"]
              )
            
            (def .public (pow param subject)
              (-> Frac Frac Frac)
              ("lua power" param subject))

            (def .public (root_3 it)
              (-> Frac Frac)
              (if ("lux f64 <" +0.0 it)
                (|> it
                    ("lux f64 *" -1.0)
                    (..pow ("lux f64 /" +3.0 +1.0))
                    ("lux f64 *" -1.0))
                (|> it
                    (..pow ("lux f64 /" +3.0 +1.0))))))

     @.ruby
     (these (with_template [<name> <method>]
              [(def .public <name>
                 (-> Frac Frac)
                 (|>> []
                      ("ruby apply" ("ruby constant" <method>))
                      (as Frac)))]

              [cos   "Math.cos"]
              [sin   "Math.sin"]
              [tan   "Math.tan"]

              [acos  "Math.acos"]
              [asin  "Math.asin"]
              [atan  "Math.atan"]
              
              [exp   "Math.exp"]
              [log   "Math.log"]
              
              [root_2 "Math.sqrt"]
              [root_3 "Math.cbrt"]
              )

            (with_template [<name> <method>]
              [(def .public (<name> it)
                 (-> Frac Frac)
                 (|> ("ruby object do" <method> it [])
                     (as Int)
                     ("lux i64 f64")))]

              [ceil  "ceil"]
              [floor "floor"]
              )

            (def .public (pow param subject)
              (-> Frac Frac Frac)
              (as Frac ("ruby object do" "**" subject [param]))))

     @.php
     (these (with_template [<name> <method>]
              [(def .public <name>
                 (-> Frac Frac)
                 (|>> ("php apply" ("php constant" <method>))
                      (as Frac)))]

              [cos   "cos"]
              [sin   "sin"]
              [tan   "tan"]

              [acos  "acos"]
              [asin  "asin"]
              [atan  "atan"]
              
              [exp   "exp"]
              [log   "log"]

              [ceil  "ceil"]
              [floor "floor"]
              
              [root_2 "sqrt"]
              )

            (def .public (pow param subject)
              (-> Frac Frac Frac)
              (as Frac ("php apply" ("php constant" "pow") subject param)))

            (def .public root_3
              (-> Frac Frac)
              (..pow ("lux f64 /" +3.0 +1.0))))

     @.scheme
     (these (with_template [<name> <method>]
              [(def .public <name>
                 (-> Frac Frac)
                 (|>> ("scheme apply" ("scheme constant" <method>))
                      (as Frac)))]

              [cos   "cos"]
              [sin   "sin"]
              [tan   "tan"]

              [acos  "acos"]
              [asin  "asin"]
              [atan  "atan"]
              
              [exp   "exp"]
              [log   "log"]

              [ceil  "ceiling"]
              [floor "floor"]
              
              [root_2 "sqrt"]
              )

            (def .public (pow param subject)
              (-> Frac Frac Frac)
              (as Frac ("scheme apply" ("scheme constant" "expt") subject param)))

            (def .public root_3
              (-> Frac Frac)
              (..pow ("lux f64 /" +3.0 +1.0))))
     )

(def .public (round it)
  (-> Frac Frac)
  (let [floored (floor it)
        diff ("lux f64 -" floored it)]
    (cond ("lux f64 <" diff +0.5)
          ("lux f64 +" +1.0 floored)
          
          ("lux f64 <" -0.5 diff)
          ("lux f64 +" -1.0 floored)
          
          ... else
          floored)))

(def .public (atan_2 x y)
  (-> Frac Frac Frac)
  (cond ("lux f64 <" x +0.0)
        (..atan ("lux f64 /" x y))

        ("lux f64 <" +0.0 x)
        (if (or ("lux f64 <" y +0.0)
                ("lux f64 =" +0.0 y))
          (|> y ("lux f64 /" x) atan ("lux f64 +" pi))
          (|> y ("lux f64 /" x) atan ("lux f64 -" pi)))

        ... ("lux f64 =" +0.0 x)
        (cond ("lux f64 <" y +0.0)
              (|> pi ("lux f64 /" +2.0))
              
              ("lux f64 <" +0.0 y)
              (|> pi ("lux f64 /" -2.0))
              
              ... ("lux f64 =" +0.0 y)
              ("lux f64 /" +0.0 +0.0))))

(def .public (log_by base it)
  (-> Frac Frac Frac)
  ("lux f64 /"
   (..log base)
   (..log it)))

(def .public (factorial it)
  (-> Nat Nat)
  (loop (again [acc 1
                it it])
    (if (//nat.> 1 it)
      (again (//nat.* it acc) (-- it))
      acc)))

(def .public (hypotenuse catA catB)
  (-> Frac Frac Frac)
  (..pow +0.5 ("lux f64 +"
               (..pow +2.0 catA)
               (..pow +2.0 catB))))

... Hyperbolic functions
... https://en.wikipedia.org/wiki/Hyperbolic_function#Definitions
(with_template [<name> <comp> <inverse>]
  [(def .public (<name> it)
     (-> Frac Frac)
     (|> (..exp it) (<comp> (..exp ("lux f64 *" -1.0 it))) ("lux f64 /" +2.0)))

   (def .public (<inverse> it)
     (-> Frac Frac)
     (|> +2.0 ("lux f64 /" (|> (..exp it) (<comp> (..exp ("lux f64 *" -1.0 it)))))))]

  [sinh "lux f64 -" csch]
  [cosh "lux f64 +" sech]
  )

(with_template [<name> <top> <bottom>]
  [(def .public (<name> it)
     (-> Frac Frac)
     (let [e+ (exp it)
           e- (exp ("lux f64 *" -1.0 it))
           sinh' (|> e+ ("lux f64 -" e-))
           cosh' (|> e+ ("lux f64 +" e-))]
       (|> <top> ("lux f64 /" <bottom>))))]

  [tanh sinh' cosh']
  [coth cosh' sinh']
  )

... https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions#Definitions_in_terms_of_logarithms
(with_template [<name> <comp>]
  [(def .public (<name> it)
     (-> Frac Frac)
     (|> it (pow +2.0) (<comp> +1.0) (pow +0.5) ("lux f64 +" it) log))]

  [asinh "lux f64 +"]
  [acosh "lux f64 -"]
  )

(with_template [<name> <base> <diff>]
  [(def .public (<name> it)
     (-> Frac Frac)
     (let [it+ (|> <base> ("lux f64 +" <diff>))
           it- (|> <base> ("lux f64 -" <diff>))]
       (|> it+ ("lux f64 /" it-) log ("lux f64 /" +2.0))))]

  [atanh +1.0 it]
  [acoth it +1.0]
  )

(with_template [<name> <op>]
  [(def .public (<name> it)
     (-> Frac Frac)
     (let [it^2 (|> it (pow +2.0))]
       (|> +1.0 (<op> it^2) (pow +0.5) ("lux f64 +" +1.0) ("lux f64 /" it) log)))]

  [asech "lux f64 -"]
  [acsch "lux f64 +"]
  )

(with_template [<name> <op>]
  [(def .public (<name> param subject)
     (-> Frac Frac Bit)
     (<op> param subject))]

  [= "lux f64 ="]
  [< "lux f64 <"]
  )

(def .public (<= reference sample)
  (-> Frac Frac Bit)
  (or ("lux f64 <" reference sample)
      ("lux f64 =" reference sample)))

(def .public (> reference sample)
  (-> Frac Frac Bit)
  ("lux f64 <" sample reference))

(def .public (>= reference sample)
  (-> Frac Frac Bit)
  (or ("lux f64 <" sample reference)
      ("lux f64 =" sample reference)))

(with_template [<comparison> <name>]
  [(def .public <name>
     (Predicate Frac)
     (<comparison> +0.0))]

  [..> positive?]
  [..< negative?]
  [..= zero?]
  )

(with_template [<name> <op>]
  [(def .public (<name> param subject)
     (-> Frac Frac Frac)
     (<op> param subject))]

  [+ "lux f64 +"]
  [- "lux f64 -"]
  [* "lux f64 *"]
  [/ "lux f64 /"]
  [% "lux f64 %"]
  )

(def .public (/% param subject)
  (-> Frac Frac [Frac Frac])
  [(../ param subject)
   (..% param subject)])

(def .public opposite
  (-> Frac Frac)
  (..* -1.0))

(def .public (abs it)
  (-> Frac Frac)
  (if (..< +0.0 it)
    (..* -1.0 it)
    it))

(def .public (signum it)
  (-> Frac Frac)
  (cond (..= +0.0 it) +0.0
        (..< +0.0 it) -1.0
        ... else
        +1.0))

(def min_exponent -1022)
(def max_exponent (//int.frac +1023))

(with_template [<name> <test>]
  [(def .public (<name> left right)
     (-> Frac Frac Frac)
     (if (<test> right left)
       left
       right))]

  [min ..<]
  [max ..>]
  )

(def .public nat
  (-> Frac Nat)
  (|>> "lux f64 i64" .nat))

(def .public int
  (-> Frac Int)
  (|>> "lux f64 i64"))

(def mantissa_size Nat 52)
(def exponent_size Nat 11)

(def frac_denominator
  (|> -1
      ("lux i64 right-shift" ..exponent_size)
      "lux i64 f64"))

(def .public rev
  (-> Frac Rev)
  (|>> ..abs
       (..% +1.0)
       (..* ..frac_denominator)
       "lux f64 i64"
       ("lux i64 left-shift" ..exponent_size)))

(with_template [<name> <numerator>]
  [(def .public <name>
     Frac
     (../ +0.0 <numerator>))]

  [not_a_number      +0.0]
  [positive_infinity +1.0]
  )

(def .public negative_infinity
  Frac
  (..* -1.0 ..positive_infinity))

(def .public (not_a_number? it)
  (-> Frac Bit)
  (not (..= it it)))

(def .public (number? it)
  (-> Frac Bit)
  (not (or (..not_a_number? it)
           (..= ..positive_infinity it)
           (..= ..negative_infinity it))))

(def .public equivalence
  (Equivalence Frac)
  (implementation
   (def (= left right)
     (or (..= left right)
         (and (..not_a_number? left)
              (..not_a_number? right))))))

(def .public order
  (Order Frac)
  (implementation
   (def equivalence ..equivalence)
   (def < ..<)))

(def .public smallest
  Frac
  (..pow (//int.frac (//int.- (.int ..mantissa_size) ..min_exponent))
         +2.0))

(def .public biggest
  Frac
  (let [f2^-52 (..pow (//nat.frac (//nat.- ..mantissa_size 0)) +2.0)
        f2^+1023 (..pow ..max_exponent +2.0)]
    (|> +2.0
        (..- f2^-52)
        (..* f2^+1023))))

(with_template [<name> <composite> <identity>]
  [(def .public <name>
     (Monoid Frac)
     (implementation
      (def identity <identity>)
      (def composite <composite>)))]

  [addition       ..+   +0.0]
  [multiplication ..*   +1.0]
  [minimum        ..min ..biggest]
  [maximum        ..max (..* -1.0 ..biggest)]
  )

(def .public decimal
  (Codec Text Frac)
  (implementation
   (def (encoded x)
     (case x
       -0.0 (let [output ("lux f64 encode" x)]
              (if (text.starts_with? "-" output)
                output
                ("lux text concat" "+" output)))
       _ (if (..< +0.0 x)
           ("lux f64 encode" x)
           ("lux text concat" "+" ("lux f64 encode" x)))))

   (def (decoded input)
     (case ("lux f64 decode" input)
       {.#Some value}
       {try.#Success value}

       {.#None}
       {try.#Failure "Could not decode Frac"}))))

(def log/2
  (-> Frac Frac)
  (|>> ..log
       (../ (..log +2.0))))

(def double_bias Nat 1023)

(def exponent_mask (//i64.mask ..exponent_size))

(def exponent_offset ..mantissa_size)
(def sign_offset (//nat.+ ..exponent_size ..exponent_offset))

(with_template [<cast> <hex> <name>]
  [(def <name>
     (|> <hex>
         (at //nat.hex decoded)
         try.trusted
         <cast>))]

  [.i64 "FFF8000000000000" not_a_number_bits]
  [.i64 "7FF0000000000000" positive_infinity_bits]
  [.i64 "FFF0000000000000" negative_infinity_bits]
  [.i64 "0000000000000000" positive_zero_bits]
  [.i64 "8000000000000000" negative_zero_bits]
  [.nat "7FF"              special_exponent_bits]
  )

(def smallest_exponent
  (..log/2 ..smallest))

(def .public (bits it)
  (-> Frac I64)
  (.i64 (cond (..not_a_number? it)
              ..not_a_number_bits

              (..= positive_infinity it)
              ..positive_infinity_bits

              (..= negative_infinity it)
              ..negative_infinity_bits

              (..= +0.0 it)
              (let [reciprocal (../ it +1.0)]
                (if (..= positive_infinity reciprocal)
                  ... Positive zero
                  ..positive_zero_bits
                  ... Negative zero
                  ..negative_zero_bits))

              ... else
              (let [sign_bit (if (..< +0.0 it)
                               1
                               0)
                    it (..abs it)
                    exponent (|> it
                                 ..log/2
                                 ..floor
                                 (..min ..max_exponent))
                    min_gap (..- (//int.frac ..min_exponent) exponent)
                    power (|> (//nat.frac ..mantissa_size)
                              (..+ (..min +0.0 min_gap))
                              (..- exponent))
                    max_gap (..- ..max_exponent power)
                    mantissa (|> it
                                 (..* (..pow (..min ..max_exponent power) +2.0))
                                 (..* (if (..> +0.0 max_gap)
                                        (..pow max_gap +2.0)
                                        +1.0)))
                    exponent_bits (|> (if (..< +0.0 min_gap)
                                        (|> (..int exponent)
                                            (//int.- (..int min_gap))
                                            --)
                                        (..int exponent))
                                      (//int.+ (.int ..double_bias))
                                      (//i64.and ..exponent_mask))
                    mantissa_bits (..int mantissa)]
                (all //i64.or
                     (//i64.left_shifted ..sign_offset sign_bit)
                     (//i64.left_shifted ..exponent_offset exponent_bits)
                     (//i64.zero ..mantissa_size mantissa_bits)))
              )))

(with_template [<getter> <size> <offset>]
  [(def <getter>
     (-> (I64 Any) I64)
     (let [mask (|> 1 (//i64.left_shifted <size>) -- (//i64.left_shifted <offset>))]
       (|>> (//i64.and mask) (//i64.right_shifted <offset>) .i64)))]

  [mantissa ..mantissa_size 0]
  [exponent ..exponent_size ..mantissa_size]
  [sign     1               ..sign_offset]
  )

(def .public (of_bits it)
  (-> I64 Frac)
  (case [(is Nat (..exponent it))
         (is Nat (..mantissa it))
         (is Nat (..sign it))]
    (pattern [(static ..special_exponent_bits) 0 0])
    ..positive_infinity

    (pattern [(static ..special_exponent_bits) 0 1])
    ..negative_infinity

    (pattern [(static ..special_exponent_bits) _ _])
    ..not_a_number

    ... Positive zero
    [0 0 0] +0.0
    ... Negative zero
    [0 0 1] (..* -1.0 +0.0)

    [E M S]
    (let [sign (if (//nat.= 0 S)
                 +1.0
                 -1.0)
          [mantissa power] (if (//nat.< ..mantissa_size E)
                             [(if (//nat.= 0 E)
                                M
                                (//i64.one ..mantissa_size M))
                              (|> E
                                  (//nat.- ..double_bias)
                                  .int
                                  (//int.max ..min_exponent)
                                  (//int.- (.int ..mantissa_size)))]
                             [(//i64.one ..mantissa_size M)
                              (|> E (//nat.- ..double_bias) (//nat.- ..mantissa_size) .int)])
          exponent (..pow (//int.frac power) +2.0)]
      (|> (//nat.frac mantissa)
          (..* exponent)
          (..* sign)))))

(def (representation_exponent codec representation)
  (-> (Codec Text Nat) Text (Try [Text Int]))
  (case [("lux text index" 0 "e+" representation)
         ("lux text index" 0 "E+" representation)
         ("lux text index" 0 "e-" representation)
         ("lux text index" 0 "E-" representation)]
    (^.with_template [<factor> <patterns>]
      [<patterns>
       (do try.monad
         [.let [after_offset (//nat.+ 2 split_index)
                after_length (//nat.- after_offset ("lux text size" representation))]
          exponent (|> representation
                       ("lux text clip" after_offset after_length)
                       (at codec decoded))]
         (in [("lux text clip" 0 split_index representation)
              (//int.* <factor> (.int exponent))]))])
    ([+1 (^.or [{.#Some split_index} {.#None} {.#None} {.#None}]
               [{.#None} {.#Some split_index} {.#None} {.#None}])]
     [-1 (^.or [{.#None} {.#None} {.#Some split_index} {.#None}]
               [{.#None} {.#None} {.#None} {.#Some split_index}])])
    
    _
    {try.#Success [representation +0]}))

(with_template [<struct> <nat> <int> <error>]
  [(def .public <struct>
     (Codec Text Frac)
     (implementation
      (def (encoded value)
        (let [bits (..bits value)
              mantissa (..mantissa bits)
              exponent (//int.- (.int ..double_bias) (..exponent bits))
              sign (..sign bits)]
          (all "lux text concat"
               (case (.nat sign)
                 1 "-"
                 0 "+"
                 _ (undefined))
               (at <nat> encoded (.nat mantissa))
               ".0E"
               (at <int> encoded exponent))))

      (def (decoded representation)
        (let [negative? (text.starts_with? "-" representation)
              positive? (text.starts_with? "+" representation)]
          (if (or negative? positive?)
            (do [! try.monad]
              [[mantissa exponent] (..representation_exponent <nat> representation)
               [whole decimal] (case ("lux text index" 0 "." mantissa)
                                 {.#Some split_index}
                                 (do !
                                   [.let [after_offset (++ split_index)
                                          after_length (//nat.- after_offset ("lux text size" mantissa))]
                                    decimal (|> mantissa
                                                ("lux text clip" after_offset after_length)
                                                (at <nat> decoded))]
                                   (in [("lux text clip" 0 split_index mantissa)
                                        decimal]))

                                 {.#None}
                                 {try.#Failure ("lux text concat" <error> representation)})
               .let [whole ("lux text clip" 1 (-- ("lux text size" whole)) whole)]
               mantissa (at <nat> decoded (case decimal
                                            0 whole
                                            _ ("lux text concat" whole (at <nat> encoded decimal))))
               .let [sign (if negative? 1 0)]]
              (in (..of_bits
                   (all //i64.or
                        (//i64.left_shifted ..sign_offset (.i64 sign))
                        (//i64.left_shifted ..mantissa_size (.i64 (//int.+ (.int ..double_bias) exponent)))
                        (//i64.zero ..mantissa_size (.i64 mantissa))))))
            {try.#Failure ("lux text concat" <error> representation)})))))]

  [binary //nat.binary //int.binary "Invalid binary syntax: "]
  [octal //nat.octal //int.octal "Invalid octaladecimal syntax: "]
  [hex //nat.hex //int.hex "Invalid hexadecimal syntax: "]
  )

(def .public hash
  (Hash Frac)
  (implementation
   (def equivalence ..equivalence)
   (def hash ..bits)))

(def .public (approximately? margin_of_error standard value)
  (-> Frac Frac Frac Bit)
  (|> value
      (..- standard)
      ..abs
      (..< margin_of_error)))

(def .public (mod divisor dividend)
  (All (_ m) (-> Frac Frac Frac))
  (let [remainder (..% divisor dividend)]
    (if (or (and (..< +0.0 divisor)
                 (..> +0.0 remainder))
            (and (..> +0.0 divisor)
                 (..< +0.0 remainder)))
      (..+ divisor remainder)
      remainder)))