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: concepts/numbers/about.md
+26-21Lines changed: 26 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,16 @@
2
2
3
3
## Numerical types
4
4
5
-
[Numbers][numbers] can be integer, unsigned integer, or floating point types.
5
+
[Numbers][ref-numbers] can be integer, unsigned integer, or floating point types.
6
6
Each comes in various "sizes", meaning how many bits it needs in memory.
7
7
8
-
Unlike some scripting languages (Ruby, recent versions of Python), each type in Kotlin has a maximum ([`MAX_VALUE`][max_value]) and minimum ([`MIN_VALUE`][min_value]) value it can store.
9
-
Assigning larger values will cause ["overflow"][wiki-overflow], causing either an exception (_bad_) or corrupted data (_worse_).
8
+
Unlike some scripting languages (Ruby, recent versions of Python), each numeric type in Kotlin has a maximum ([`MAX_VALUE`][ref-max_value]) and minimum ([`MIN_VALUE`][ref-min_value]) value it can store.
9
+
Assigning larger values will cause ["overflow"][wiki-overflow], resulting in either an exception (_bad_) or corrupted data (_worse_).
10
+
11
+
```kotlin
12
+
Int.MAX_VALUE// => 2147483647 (maximum for a 32-bit signed integer)
13
+
Int.MAX_VALUE+1// -2147483648 (overflow gives a negative result!)
14
+
```
10
15
11
16
- Integers can be `Byte`, `Short`, `Int` or `Long`, respectively 8, 16, 32 and 64 bits (1, 2 4, 8 bytes).
12
17
- Unsigned integers have a `U` prefix: `UByte`, `UShort`, `UInt` or `ULong`.
@@ -61,7 +66,7 @@ The modulo operator `%` gives the remainder from integer division:
61
66
```
62
67
63
68
Kotlin, like other JVM languages, has no exponentiation operator (_this annoys scientists and engineers_).
64
-
We need to use the [`pow()`][pow] function from the [`math`][math] library, and the number being raised to some power must be `Float` or `Double`.
69
+
We need to use the [`pow()`][ref-pow] function from the [`math`][ref-math] library, and the number being raised to some power must be `Float` or `Double`.
65
70
66
71
```kotlin
67
72
2.0.pow(3) // => 8.0
@@ -70,7 +75,7 @@ We need to use the [`pow()`][pow] function from the [`math`][math] library, and
70
75
71
76
## Rounding
72
77
73
-
The [`math`][math] library contains several functions to round floating point numbers to a nearby integer.
78
+
The [`math`][ref-math] library contains several functions to round floating point numbers to a nearby integer.
74
79
75
80
_Did the last line sound slightly odd?_
76
81
When we say "a nearby integer", there are two questions:
@@ -79,16 +84,16 @@ When we say "a nearby integer", there are two questions:
79
84
- How are ties rounded? Does `4.5` round to `4` or `5`?
80
85
81
86
It is not Kotlin's fault if this seems complicated.
82
-
Mathematicians have been arguing about this for centuries.
87
+
Mathematicians have been arguing about it for centuries.
83
88
84
89
### `round()`, `floor()`, `ceil()`, `truncate()`
85
90
86
91
These four functions all return the same floating-point type as the input, but differ in how they round.
87
92
88
-
-[`round()`][round] gives the _nearest_ integer if this is unambiguous, or the _nearest even_ integer when tied.
89
-
-[`floor()`][floor] rounds towards negative infinity.
90
-
-[`ceil()`][ceil] rounds towards positive infinity
91
-
-[`truncate()`][truncate] rounds towards zero
93
+
-[`round()`][ref-round] gives the _nearest_ integer if this is unambiguous, or the _nearest even_ integer when tied.
94
+
-[`floor()`][ref-floor] rounds towards negative infinity.
95
+
-[`ceil()`][ref-ceil] rounds towards positive infinity
96
+
-[`truncate()`][ref-truncate] rounds towards zero
92
97
93
98
```kotlin
94
99
round(4.7) // => 5.0 (nearest integer)
@@ -138,17 +143,17 @@ val n = 42
138
143
n.toDouble() // => 42.0
139
144
```
140
145
141
-
See the [manual][conversions] for the full list of `toX()` methods.
146
+
See the [manual][ref-conversions] for the full list of `toX()` methods.
0 commit comments