-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNumber.pen
147 lines (124 loc) · 3.12 KB
/
Number.pen
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
# This module provides common number operations.
import foreign "c" _pen_core_absolute \(number) number
import foreign "c" _pen_core_ceil \(number) number
import foreign "c" _pen_core_convert_number_to_string \(number) string
import foreign "c" _pen_core_epsilon \() number
import foreign "c" _pen_core_exponential \(number) number
import foreign "c" _pen_core_floor \(number) number
import foreign "c" _pen_core_fraction \(number) number
import foreign "c" _pen_core_infinity \() number
import foreign "c" _pen_core_is_nan \(number) boolean
import foreign "c" _pen_core_nan \() number
import foreign "c" _pen_core_parse_number \(string) number | error
import foreign "c" _pen_core_power \(number, number) number
import foreign "c" _pen_core_remainder \(number, number) number
import foreign "c" _pen_core_round \(number) number
import foreign "c" _pen_core_square_root \(number) number
import foreign "c" _pen_core_truncate \(number) number
# Calculate an absolute value.
Absolute = \(x number) number {
_pen_core_absolute(x)
}
# Calculate a ceil value.
Ceil = \(x number) number {
_pen_core_ceil(x)
}
# Machine epsilon
Epsilon = \() number {
_pen_core_epsilon()
}
# Calculate an exponential of a number.
Exponential = \(x number) number {
_pen_core_exponential(x)
}
# Calculate a floor value.
Floor = \(x number) number {
_pen_core_floor(x)
}
# Calculate a fraction value of a number.
Fraction = \(x number) number {
_pen_core_fraction(x)
}
# Infinity
Infinity = \() number {
_pen_core_infinity()
}
# Check if a number is NaN.
IsNan = \(x number) boolean {
_pen_core_is_nan(x)
}
# Calculate a maximum.
Maximum = \(x number, y number) number {
if IsNan(x) | IsNan(y) {
Nan()
} else if x > y {
x
} else {
y
}
}
# Calculate a minimum.
Minimum = \(x number, y number) number {
if IsNan(x) | IsNan(y) {
Nan()
} else if x < y {
x
} else {
y
}
}
# NaN
Nan = \() number {
_pen_core_nan()
}
# Parse a number.
Parse = \(s string) number | error {
_pen_core_parse_number(s)
}
# Calculate a power.
Power = \(base number, exponent number) number {
_pen_core_power(base, exponent)
}
# Create a list of numbers from a minimum to a maximum.
Range = \(minimum number, maximum number) [number] {
if minimum > maximum {
[number]
} else {
[number minimum, ...Range(minimum + 1, maximum)]
}
}
# Calculate a remainder.
Remainder = \(dividend number, divisor number) number {
_pen_core_remainder(dividend, divisor)
}
# Round a number.
Round = \(x number) number {
_pen_core_round(x)
}
# Create a list of numbers from 1 to a maximum value.
Sequence = \(maximum number) [number] {
Range(1, maximum)
}
# Calculate a square root.
SquareRoot = \(x number) number {
_pen_core_square_root(x)
}
# Convert a number into its string representation.
String = \(x number) string {
_pen_core_convert_number_to_string(x)
}
# Sum up numbers.
Sum = \(xs [number]) number {
sumRecursive(xs, 0)
}
sumRecursive = \(xs [number], sum number) number {
if [x, ...xs] = xs {
sumRecursive(xs, sum + x())
} else {
sum
}
}
# Return an integer value of a number.
Truncate = \(x number) number {
_pen_core_truncate(x)
}