aboutsummaryrefslogtreecommitdiff
path: root/format.go
blob: 94e21d68a80693499d1bac24e8fc94a0672f3504 (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
package main

import (
	"fmt"
	"math"
)

type S string      // string
type I int64       // int
type F float64     // float
type C complex128  // complex
type A interface{} // any

type Monad func(complex128) complex128
type Dyad func(complex128, complex128) complex128

func (s S) String() string {
	return string(s)
}

func (i I) String() string {
	pre := ""
	if i < 0 {
		pre = "¯"
		i = -i
	}
	return fmt.Sprint(pre, int64(i))
}

func (f F) String() string {
	pre := ""
	if f < 0 {
		pre = "¯"
		f = -f
	}
	return fmt.Sprint(pre, float64(f))
}

func (c C) String() string {
	return fmt.Sprint(F(real(c)), "J", F(imag(c)))
}

func IsInt(c complex128) bool {
	return imag(c) == 0 && math.Trunc(real(c)) == real(c)
}

func IsFloat(c complex128) bool {
	return imag(c) == 0 && math.Trunc(real(c)) != real(c)
}

func IsComplex(c complex128) bool {
	return imag(c) != 0
}