-
-
Notifications
You must be signed in to change notification settings - Fork 202
Add Numbers concept #711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add Numbers concept #711
Conversation
There was a problem hiding this comment.
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.
|
||
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`. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revised and expanded again.
There was a problem hiding this comment.
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.
Response to reviewer comments
Clarified maximum and minimum value references for Kotlin types.
As well as the new section on type size and overflow, I revised the reference codes to conform with your wishes in #712. |
typo (spotted by @BNAndras)
Response to comments in exercism#713 (@BNAndras)
``` | ||
|
||
## Rounding | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
||
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`. |
There was a problem hiding this comment.
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.
concepts/numbers/about.md
Outdated
4 / 3 // => 1 Int / Int always gives an Int | ||
-8 / 3 // => -2 Truncated towards zero | ||
-8.0 / 3 // => -2.6666666666666665 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | |
``` |
concepts/numbers/about.md
Outdated
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. | ||
|
There was a problem hiding this comment.
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. | |
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 | |
``` |
|
||
Hexadecimal and binary literals are the same as in many languages: `0x7F` and `0b100101` respectively. | ||
Octal literals are not supported in Kotlin. | ||
|
There was a problem hiding this comment.
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...
Co-authored-by: Derk-Jan Karrenbeld <[email protected]>
Co-authored-by: Derk-Jan Karrenbeld <[email protected]>
Co-authored-by: Kah Goh <[email protected]>
The
introduction.md
is too long, but we need to pick a concept exercise before deciding what is needed and what can be deleted.