Skip to content

Add Numbers concept - #711

Open
colinleach wants to merge 12 commits into
exercism:mainfrom
colinleach:numbers
Open

Add Numbers concept#711
colinleach wants to merge 12 commits into
exercism:mainfrom
colinleach:numbers

Conversation

@colinleach

Copy link
Copy Markdown
Contributor

The introduction.md is too long, but we need to pick a concept exercise before deciding what is needed and what can be deleted.

@SleeplessByte SleeplessByte left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah probably worth it to move type conversion etc. to separate concept but fine for now.

Comment thread concepts/numbers/about.md

Integer variables relying on type inference default to `Int`, even on 64-bit machines, but floating point variables default to `Double`.

Other types can, of course, be specified, but there are a few syntactic shortcuts, and big integer literals become `Long` if they would overflow `Int`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to quickly explain what 8-64 bits mean and what overflow means. Many languages do NOT have fixed-width numeric types. In Ruby for example, every number is arbitrary precision.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a paragraph - is this what you wanted?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revised and expanded again.

@SleeplessByte SleeplessByte Sep 2, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better, but assume someone doesn't know what "number can be x bytes" means. I am a strong opponent to "just" explain this, especially in about.md, but also in introduction.md if it's relevant. We could add above line 16 something like:

In Kotlin, like many strongly types languages, numbers take a fixed amount of space in memory, which also means for each numeric type there is a smallest and largest value that fits.
This is generally expressed in bits or bytes.

For numeric types that can go below and above zero, one bit is reserved to indicate the *sign*.
This effectively cuts the amount of integers that fits in the memory in half, and these are called signed integers.
In contrast, there are also unsigned integers which do not reserve a bit for the sign, doubling the maximum value the memory for that number can hold.

You may want to reword in your own words.

Sidenote: if you feel my or kahgoh's contributions are significant, feel free to add us to the contributors array when you see fit.

Comment thread concepts/numbers/about.md Outdated
@colinleach

Copy link
Copy Markdown
Contributor Author

As well as the new section on type size and overflow, I revised the reference codes to conform with your wishes in #712.

This was referenced Aug 31, 2025
```

## Rounding

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colinleach I wasn't quite sure if you meant to remove bits from this introduction as well or just from the exercise's introduction in the comment (ignore this if you meant just from the exercise introduction), but if you're looking for something to cut or separate out, the Cars Assemble exercise dosen't cover rounding and the use of the math library.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make a cut-down version in the exercise, I'll wait till that is merged then copy it back to the concept. Probably easier than trying to keep them in sync as yours goes through review and editing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what to do about the math library. I covered rounding here because some other tracks do (with an exercise that needs it such as python/currency-exchange). If this was a more mathy track (like Julia or R), a separate math concept might be appropriate - probably not for Kotlin.

I think I'll leave it in the About for now. If any of the later exercises need rounding, we can move this section to there.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave it in about, we can remove from introduction as we build the track out. Moving content between concepts is far easier than adding new content!

-8.0 / 3 // => -2.6666666666666665
```

To get a floating point result from division, at least one of the numerator / denominator must be floating point.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this true for any arithmetic operator in Kotlin? For example 3 + 2.0 will give 5.0 and 3 * 2.0 gives 6.0 (both results are doubles).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. However, only integer division causes large rounding errors. I'll try to make the wording clearer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get a floating point result, at least one of the numbers in the calculation must be floating point. This is particularly significant for division, as integer division often leads to truncation.

Is this better?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if it's worth explaining that Kotlin works out the result number types in the calculations. I'll try suggest something in a separate comment.

Comment thread concepts/numbers/introduction.md
Comment thread concepts/numbers/about.md

Integer variables relying on type inference default to `Int`, even on 64-bit machines, but floating point variables default to `Double`.

Other types can, of course, be specified, but there are a few syntactic shortcuts, and big integer literals become `Long` if they would overflow `Int`.

@SleeplessByte SleeplessByte Sep 2, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better, but assume someone doesn't know what "number can be x bytes" means. I am a strong opponent to "just" explain this, especially in about.md, but also in introduction.md if it's relevant. We could add above line 16 something like:

In Kotlin, like many strongly types languages, numbers take a fixed amount of space in memory, which also means for each numeric type there is a smallest and largest value that fits.
This is generally expressed in bits or bytes.

For numeric types that can go below and above zero, one bit is reserved to indicate the *sign*.
This effectively cuts the amount of integers that fits in the memory in half, and these are called signed integers.
In contrast, there are also unsigned integers which do not reserve a bit for the sign, doubling the maximum value the memory for that number can hold.

You may want to reword in your own words.

Sidenote: if you feel my or kahgoh's contributions are significant, feel free to add us to the contributors array when you see fit.

Comment thread concepts/numbers/about.md Outdated
Comment thread concepts/numbers/about.md Outdated
Comment on lines +45 to +47
4 / 3 // => 1 Int / Int always gives an Int
-8 / 3 // => -2 Truncated towards zero
-8.0 / 3 // => -2.6666666666666665

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
4 / 3 // => 1 Int / Int always gives an Int
-8 / 3 // => -2 Truncated towards zero
-8.0 / 3 // => -2.6666666666666665
12 / 4 => 3
```
When dividing an integer (for example an Int) by another integer (for example an Int), the result will also be an integer (in this case an Int):
```kotlin
4 / 3 // => 1 Int / Int always gives an Int
-8 / 3 // => -2 Truncated towards zero
```

Comment thread concepts/numbers/about.md Outdated
Comment on lines +50 to +52
To get a floating point result, at least one of the numbers in the calculation must be floating point.
This is particularly significant for division, as integer division often leads to truncation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To get a floating point result, at least one of the numbers in the calculation must be floating point.
This is particularly significant for division, as integer division often leads to truncation.
To get a floating point result, at least one of the numbers in the calculation must be floating point (ie. a Float or a Double).
This is particularly significant for division, as integer division often leads to truncation.
```kotlin
-8.0 / 3 // => -2.6666666666666665
-8 / 3.0 // => -2.6666666666666665
-8.0 / 3.0 // => -2.6666666666666665
```

Comment thread concepts/numbers/about.md
Comment thread concepts/numbers/about.md

Hexadecimal and binary literals are the same as in many languages: `0x7F` and `0b100101` respectively.
Octal literals are not supported in Kotlin.

@SleeplessByte SleeplessByte Sep 2, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add the Kotlin does not support arbitrary precision arithmetic in its standard library, so a userland solution is necessary for BigInteger and BigDecimal support?

It just doesn't exist for multiplatform, but does exist for JVM...

Comment thread concepts/numbers/about.md Outdated
colinleach and others added 3 commits September 2, 2025 08:55
Co-authored-by: Derk-Jan Karrenbeld <derk-jan+github@karrenbeld.info>
Co-authored-by: Derk-Jan Karrenbeld <derk-jan+github@karrenbeld.info>
Co-authored-by: Kah Goh <villastar@yahoo.com.au>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants