You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/language/values-and-types/fixed-point-nums-ints.md
+56-17Lines changed: 56 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,23 +7,48 @@ sidebar_position: 2
7
7
8
8
:::warning[🚧 Status]
9
9
10
-
Currently only the 64-bit wide `Fix64` and `UFix64` types are available. More fixed-point number types will be added in a future release.
10
+
Currently only the 64-bit wide (`Fix64`, `UFix64`), and 128-bit wide (`Fix128`, `UFix128`) types are available.
11
+
More fixed-point number types will be added in a future release.
11
12
12
13
:::
13
14
14
-
Fixed-point numbers are useful for representing fractional values. They have a fixed number of digits after a decimal point.
15
+
Fixed-point numbers are useful for representing fractional values.
16
+
They have a fixed number of digits after a decimal point.
15
17
16
-
They are essentially integers which are scaled by a factor. For example, the value 1.23 can be represented as 1230 with a scaling factor of 1/1000. The scaling factor is the same for all values of the same type and stays the same during calculations.
18
+
They are essentially integers which are scaled by a factor.
19
+
For example, the value 1.23 can be represented as 1230 with a scaling factor of 1/1000.
20
+
The scaling factor is the same for all values of the same type and stays the same during calculations.
17
21
18
22
Fixed-point numbers in Cadence have a scaling factor with a power of 10, instead of a power of 2 (i.e., they are decimal, not binary).
19
23
20
24
Signed fixed-point number types have the prefix `Fix`, have the following factors, and can represent values in the following ranges:
21
25
22
-
-**`Fix64`**: Factor 1/100,000,000; -92233720368.54775808 through 92233720368.54775807
26
+
-**`Fix64`**: Factor 1e-8; `-92233720368.54775808` through `92233720368.54775807`
27
+
-**`Fix128`**: Factor 1e-24; `-170141183460469.231731687303715884105728` through `170141183460469.231731687303715884105727`
23
28
24
29
Unsigned fixed-point number types have the prefix `UFix`, have the following factors, and can represent values in the following ranges:
25
30
26
-
-**`UFix64`**: Factor 1/100,000,000; 0.0 through 184467440737.09551615
31
+
-**`UFix64`**: Factor 1e-8; `0.0` through `184467440737.09551615`
32
+
-**`UFix128`**: Factor 1e-24; `0.0` through `340282366920938.463463374607431768211455`
33
+
34
+
### Fixed-point type inference
35
+
36
+
An untyped positive fixed-point literal is assumed to be of type `UFix64`, whereas an untyped negative fixed-point
37
+
literal is assumed to be of type `Fix64`.
38
+
39
+
```cadence
40
+
var v1 = 1.23 // v1 would have the type `UFix64`
41
+
42
+
var v2 = -1.23 // v2 would have the type `Fix64`
43
+
```
44
+
45
+
Type annotations can be used to construct a fixed-point value belong to a specific type. e.g:
46
+
47
+
```cadence
48
+
var v1: Fix64 = 1.23 // v1 would have the type `Fix64`
49
+
50
+
var v2: Fix128 = -1.23 // v2 would have the type `Fix128`
51
+
```
27
52
28
53
### Fixed-point number functions
29
54
@@ -37,9 +62,13 @@ Fixed-Point numbers have multiple built-in functions you can use:
37
62
Returns the string representation of the fixed-point number.
38
63
39
64
```cadence
40
-
let fix = 1.23
41
-
42
-
fix.toString() // is "1.23000000"
65
+
// For `Fix64`
66
+
let fix64: Fix64 = 1.23
67
+
fix64.toString() // is "1.23000000"
68
+
69
+
// For `Fix128`
70
+
let fix128: Fix128 = 1.23
71
+
fix128.toString() // is "1.230000000000000000000000"
43
72
```
44
73
-
45
74
```cadence
@@ -49,9 +78,13 @@ Fixed-Point numbers have multiple built-in functions you can use:
49
78
Returns the byte array representation (`[UInt8]`) in big-endian order of the fixed-point number.
All fixed-point types support the following functions:
@@ -71,9 +104,9 @@ All fixed-point types support the following functions:
71
104
- they don't fit in the target type.
72
105
- they're missing a decimal or fractional component. For example, both `0.` and `.1` are invalid strings, but `0.1` is accepted.
73
106
74
-
For signed types like `Fix64`, the string may optionally begin with a `+` or `-` sign prefix.
107
+
For signed types like `Fix64` and `Fix128`, the string may optionally begin with a `+` or `-` sign prefix.
75
108
76
-
For unsigned types like `UFix64`, sign prefices are not allowed.
109
+
For unsigned types like `UFix64` and `UFix128`, sign prefixes are not allowed.
77
110
78
111
Examples:
79
112
@@ -95,7 +128,8 @@ All fixed-point types support the following functions:
95
128
```cadence
96
129
view fun T.fromBigEndianBytes(_ bytes: [UInt8]): T?
97
130
```
98
-
Attempts to parse an integer value from a byte array representation (`[UInt8]`) in big-endian order, returning `nil` if the input bytes are invalid.
131
+
Attempts to parse an integer value from a byte array representation (`[UInt8]`) in big-endian order, returning `nil`
132
+
if the input bytes are invalid.
99
133
100
134
For a given integer `n` of type `T`, `T.fromBigEndianBytes(n.toBigEndianBytes())` is equivalent to wrapping `n` up in an [optional].
101
135
@@ -120,7 +154,8 @@ All fixed-point types support the following functions:
120
154
121
155
Casting between number types (e.g. `Int` to `UInt`, `Fix64` to `Int`) using the [casting operators] (`as`, `as?` and `as!`) is not supported.
122
156
123
-
To convert between number types, the conversion functions ((e.g. `UInt(_)`)) must be used. These conversion functions have the same name as the desired type.
157
+
To convert between number types, the conversion functions ((e.g. `UInt(_)`)) must be used.
158
+
These conversion functions have the same name as the desired type.
124
159
125
160
```cadence
126
161
let value: UInt8 = 1
@@ -132,7 +167,10 @@ let validInt: Int = Int(value)
132
167
// validInt is `1` and has type `Int`
133
168
```
134
169
135
-
When converting from a larger number type to a smaller one (narrowing), the conversion will succeed if the value can be represented in the smaller type. If it cannot an error will be thrown indicating overflow or underflow. Converting to a larger number type will always succeed.
170
+
When converting from a larger number type to a smaller one (narrowing), the conversion will succeed if the value can be
171
+
represented in the smaller type.
172
+
If it cannot an error will be thrown indicating overflow or underflow.
173
+
Converting to a larger number type will always succeed.
136
174
137
175
```cadence
138
176
let intValue: Int16 = 256
@@ -147,7 +185,8 @@ let largerIntValue: Int = Int(intValue)
147
185
// largerIntValue is `256` and has type `Int`
148
186
```
149
187
150
-
Converting from integer types to fixed point types and vice versa is supported by calling the conversion functions as well. The same conditions as narrowing applies, an error will be thrown if the value cannot be represented in the range.
188
+
Converting from integer types to fixed point types and vice versa is supported by calling the conversion functions as well.
189
+
The same conditions as narrowing applies, an error will be thrown if the value cannot be represented in the range.
0 commit comments