Skip to content

Commit 12ae22a

Browse files
authored
Merge pull request #268 from onflow/supun/document-fix128
Document `Fix128` type
2 parents 9549a2e + d97ded1 commit 12ae22a

2 files changed

Lines changed: 136 additions & 17 deletions

File tree

.idea/workspace.xml

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/language/values-and-types/fixed-point-nums-ints.md

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,48 @@ sidebar_position: 2
77

88
:::warning[🚧 Status]
99

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.
1112

1213
:::
1314

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.
1517

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.
1721

1822
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).
1923

2024
Signed fixed-point number types have the prefix `Fix`, have the following factors, and can represent values in the following ranges:
2125

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`
2328

2429
Unsigned fixed-point number types have the prefix `UFix`, have the following factors, and can represent values in the following ranges:
2530

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+
```
2752

2853
### Fixed-point number functions
2954

@@ -37,9 +62,13 @@ Fixed-Point numbers have multiple built-in functions you can use:
3762
Returns the string representation of the fixed-point number.
3863

3964
```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"
4372
```
4473
-
4574
```cadence
@@ -49,9 +78,13 @@ Fixed-Point numbers have multiple built-in functions you can use:
4978
Returns the byte array representation (`[UInt8]`) in big-endian order of the fixed-point number.
5079

5180
```cadence
52-
let fix = 1.23
53-
54-
fix.toBigEndianBytes() // is `[0, 0, 0, 0, 7, 84, 212, 192]`
81+
// For `Fix64`
82+
let fix64: Fix64 = 1.23
83+
fix64.toBigEndianBytes() // is `[0, 0, 0, 0, 7, 84, 212, 192]`
84+
85+
// For `Fix128`
86+
let fix128: Fix128 = 1.23
87+
fix128.toBigEndianBytes() // is `[0, 0, 0, 0, 0, 1, 4, 118, 111, 0, 236, 179, 164, 192, 0, 0]`
5588
```
5689

5790
All fixed-point types support the following functions:
@@ -71,9 +104,9 @@ All fixed-point types support the following functions:
71104
- they don't fit in the target type.
72105
- they're missing a decimal or fractional component. For example, both `0.` and `.1` are invalid strings, but `0.1` is accepted.
73106

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.
75108

76-
For unsigned types like `UFix64`, sign prefices are not allowed.
109+
For unsigned types like `UFix64` and `UFix128`, sign prefixes are not allowed.
77110

78111
Examples:
79112

@@ -95,7 +128,8 @@ All fixed-point types support the following functions:
95128
```cadence
96129
view fun T.fromBigEndianBytes(_ bytes: [UInt8]): T?
97130
```
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.
99133

100134
For a given integer `n` of type `T`, `T.fromBigEndianBytes(n.toBigEndianBytes())` is equivalent to wrapping `n` up in an [optional].
101135

@@ -120,7 +154,8 @@ All fixed-point types support the following functions:
120154

121155
Casting between number types (e.g. `Int` to `UInt`, `Fix64` to `Int`) using the [casting operators] (`as`, `as?` and `as!`) is not supported.
122156

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.
124159

125160
```cadence
126161
let value: UInt8 = 1
@@ -132,7 +167,10 @@ let validInt: Int = Int(value)
132167
// validInt is `1` and has type `Int`
133168
```
134169

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.
136174

137175
```cadence
138176
let intValue: Int16 = 256
@@ -147,7 +185,8 @@ let largerIntValue: Int = Int(intValue)
147185
// largerIntValue is `256` and has type `Int`
148186
```
149187

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.
151190

152191
```cadence
153192
let intValue: Int = -1

0 commit comments

Comments
 (0)