Skip to content

Commit 7e28729

Browse files
author
Colin Leach
committed
revised overflow section and reference format
1 parent 083d2e1 commit 7e28729

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

concepts/numbers/about.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
## Numerical types
44

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.
66
Each comes in various "sizes", meaning how many bits it needs in memory.
77

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

1116
- Integers can be `Byte`, `Short`, `Int` or `Long`, respectively 8, 16, 32 and 64 bits (1, 2 4, 8 bytes).
1217
- Unsigned integers have a `U` prefix: `UByte`, `UShort`, `UInt` or `ULong`.
@@ -61,7 +66,7 @@ The modulo operator `%` gives the remainder from integer division:
6166
```
6267

6368
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`.
6570

6671
```kotlin
6772
2.0.pow(3) // => 8.0
@@ -70,7 +75,7 @@ We need to use the [`pow()`][pow] function from the [`math`][math] library, and
7075

7176
## Rounding
7277

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

7580
_Did the last line sound slightly odd?_
7681
When we say "a nearby integer", there are two questions:
@@ -79,16 +84,16 @@ When we say "a nearby integer", there are two questions:
7984
- How are ties rounded? Does `4.5` round to `4` or `5`?
8085

8186
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.
8388

8489
### `round()`, `floor()`, `ceil()`, `truncate()`
8590

8691
These four functions all return the same floating-point type as the input, but differ in how they round.
8792

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
9297

9398
```kotlin
9499
round(4.7) // => 5.0 (nearest integer)
@@ -138,17 +143,17 @@ val n = 42
138143
n.toDouble() // => 42.0
139144
```
140145

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

143-
[numbers]: https://kotlinlang.org/docs/numbers.html
148+
[ref-numbers]: https://kotlinlang.org/docs/numbers.html
144149
[wiki-IEEE]: https://en.wikipedia.org/wiki/IEEE_754
145-
[conversions]: https://kotlinlang.org/docs/numbers.html#explicit-number-conversions
146-
[pow]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/pow.html
147-
[math]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/
148-
[round]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/round.html
149-
[floor]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/floor.html
150-
[ceil]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/ceil.html
151-
[truncate]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/truncate.html
150+
[ref-conversions]: https://kotlinlang.org/docs/numbers.html#explicit-number-conversions
151+
[ref-pow]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/pow.html
152+
[ref-math]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/
153+
[ref-round]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/round.html
154+
[ref-floor]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/floor.html
155+
[ref-ceil]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/ceil.html
156+
[ref-truncate]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/truncate.html
152157
[wiki-overflow]: https://en.wikipedia.org/wiki/Integer_overflow
153-
[max_value]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/-companion/#-244053257%2FProperties%2F-956074838
154-
[min_value]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/-companion/#-1907397559%2FProperties%2F-956074838
158+
[ref-max_value]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/-companion/#-244053257%2FProperties%2F-956074838
159+
[ref-min_value]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/-companion/#-1907397559%2FProperties%2F-956074838

0 commit comments

Comments
 (0)