aboutsummaryrefslogtreecommitdiff
path: root/module/text/big-numbers.scm
blob: 325cc2f30abf1587e6349e002fd05b9786eb4625 (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
(define-module (text big-numbers)
  :use-module ((hnh util) :select (->>))
  :use-module (hnh util type)
  :export (number->exponential-form
           power-of-10?
           subscript supscript))

(define (integer-string-ref str n)
  (typecheck str string?)
  (typecheck n (and exact? integer?))
  (->> n
       number->string
       string->list
       (map char->integer)
       (map (lambda (x) (- x (char->integer #\0))))
       (map (lambda (i) (string-ref str i)))
       list->string))

(define (subscript n)
  (integer-string-ref "₀₁₂₃₄₅₆₇₈₉" n))

(define (supscript n)
  (integer-string-ref "⁰¹²³⁴⁵⁶⁷⁸⁹" n))

;;; Is the number a power of 10?
(define (power-of-10? x)
  (integer? (log10 (exact->inexact x))))

(define (number->exponential-form n)
  (typecheck n (and exact? integer? power-of-10?))
  (string-append "10" (supscript (inexact->exact (round (log10 n))))))