-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
[New Term] C++ Math Functions: round()
#1043
[New Term] C++ Math Functions: round()
#1043
Conversation
@tanishq-singh-2301 Thanks for doing that, much appreciated! 😄 After I'm finished with review I'll tag you again so you can implement the feedback. |
Sure, |
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.
@tanishq-singh-2301 Left you some comments/suggestions to look at. Tag when when you're finished with implementing the feedback and I'll take another look. 😄
result = std::round(num); | ||
``` | ||
|
||
## Codecademy Example |
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.
## Codecademy Example | |
## Codebyte Example |
- 'paths/computer-science' | ||
--- | ||
|
||
The `round()` function returns the integral value that is nearest to the argument, with halfway cases rounded away from zero. |
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.
The `round()` function returns the integral value that is nearest to the argument, with halfway cases rounded away from zero. | |
The `round()` function returns the integer that is nearest to the argument, with halfway cases rounded away from zero. |
@@ -0,0 +1,52 @@ | |||
--- | |||
Title: 'round()' | |||
Description: 'Returns the integral value that is nearest to the argument, with halfway cases rounded away from zero.' |
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.
Description: 'Returns the integral value that is nearest to the argument, with halfway cases rounded away from zero.' | |
Description: 'Returns the integer that is nearest to the argument, with halfway cases rounded away from zero.' |
|
||
The `cmath` library must be added at the top of the file. | ||
|
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.
The `cmath` library must be added at the top of the file. |
I would omit this line since this point is covered in the parent math functions entry.
The `cmath` library must be added at the top of the file. | ||
|
||
```cpp | ||
std::round(double num); |
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.
std::round(double num); | |
round(num); |
std::round(double num); | ||
``` | ||
|
||
Argument must be a `double`/`float`/`long double`, and the return value will be the type of `integer`. |
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.
Argument must be a `double`/`float`/`long double`, and the return value will be the type of `integer`. | |
The `num` parameter must be a `double`,`float`, or `long double`. The return value will be an integer. |
Done. Thanks |
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.
@tanishq-singh-2301 Just a few more comments from me 😄
- 'paths/computer-science' | ||
--- | ||
|
||
The `round()` function returns the integer that is nearest to the argument, with halfway cases rounded away from zero. |
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.
The `round()` function returns the integer that is nearest to the argument, with halfway cases rounded away from zero. | |
The **`round()`** function returns the integer that closest to the argument, with halfway cases rounded away from zero. |
|
||
## Codebyte Example | ||
|
||
Use `round()` to round-off `10.89`: |
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.
Use `round()` to round-off `10.89`: | |
The following example is runnable and rounds the halfway case away from zero: |
Done. Thanks |
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.
@tanishq-singh-2301 Just looked over your latest batch of changes. I left some final comments/suggestions for my review. After you implement this feedback, we will proceed to one more review of this entry. 😄
The `num` parameter must be a `double`,`float`, or `long double`. The return value will be an integer. | ||
In `halfway case` the number get round-off to the next closest integer. |
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.
The `num` parameter must be a `double`,`float`, or `long double`. The return value will be an integer. | |
In `halfway case` the number get round-off to the next closest integer. | |
The `num` parameter must be a `double`,`float`, or `long double`. The return value will be an integer. If `num` is a halfway case where the decimal place is `.5` or higher, then the closest integer above `num` is returned. |
double num = 9.23; | ||
double result; | ||
|
||
result = std::round(num); | ||
|
||
std::cout << "The result is " << result << "!\n"; | ||
// Output: The result is 9! |
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.
double num = 9.23; | |
double result; | |
result = std::round(num); | |
std::cout << "The result is " << result << "!\n"; | |
// Output: The result is 9! | |
#include <iostream> | |
#include <cmath> | |
int main() { | |
double num1 = 9.23; | |
double result1; | |
result1 = std::round(num1); | |
std::cout << "The result of round(9.23) is " << result1 << "!\n"; | |
double num2 = 4.5; | |
double result2; | |
result2 = std::round(num2); | |
std::cout << "The result of round(4.5) is " << result2 << "!\n"; | |
} |
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 think we should include the #includes
statements along with the code being nested in a main()
method so that if a reader copy/pastes this example they can run it. 😄
## Example 1 | ||
|
||
```cpp | ||
double num = 4.5; | ||
double result; | ||
|
||
result = std::round(num); | ||
|
||
std::cout << "The result is " << result << "!\n"; | ||
// Output: The result is 5! | ||
|
||
``` | ||
|
||
|
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.
Let's keep the entry to just one "Example" and one "Codebyte Example". My suggestion here would be to combine the code from both of these snippets.
## Example 1 | |
```cpp | |
double num = 4.5; | |
double result; | |
result = std::round(num); | |
std::cout << "The result is " << result << "!\n"; | |
// Output: The result is 5! | |
``` |
``` | ||
|
||
|
||
## Example 2 |
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.
## Example 2 | |
## Example |
std::cout << "The result is " << result << "!\n"; | ||
// Output: The result is 11! |
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.
std::cout << "The result is " << result << "!\n"; | |
// Output: The result is 11! | |
std::cout << "The result is " << result << "!\n"; |
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.
No need for output comments in Codebyte snippets.
@@ -0,0 +1,69 @@ | |||
--- | |||
Title: 'round()' | |||
Description: 'Returns the integer that is nearest to the argument, with halfway cases rounded away from zero.' |
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.
Description: 'Returns the integer that is nearest to the argument, with halfway cases rounded away from zero.' | |
Description: 'Returns the integer that is nearest to the argument, with halfway cases rounded away from the ending zero.' |
- 'paths/computer-science' | ||
--- | ||
|
||
The **`round()`** function returns the integer that is closest to the argument, with halfway cases rounded away from zero. |
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.
The **`round()`** function returns the integer that is closest to the argument, with halfway cases rounded away from zero. | |
The **`round()`** function returns the integer that is closest to the argument, with halfway cases rounded away from the ending zero. |
working on it! |
Done, please review and tell me if any more changes is required? Thanks |
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.
LGTM!
Okay |
any update? @HishamT |
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.
LGTM!
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 had one additional comment before merge. @tanishq-singh-2301
round(num); | ||
``` | ||
|
||
The `num` parameter must be a `double`,`float`, or `long double`. The return value will be an integer. If the decimal in num is `0.5` or higher, the closest integer greater than `num` is returned. |
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.
The `num` parameter must be a `double`,`float`, or `long double`. The return value will be an integer. If the decimal in num is `0.5` or higher, the closest integer greater than `num` is returned. | |
- The `num` parameter must be a `double`,`float`, or `long double`. | |
- The return value will be an integer. | |
- If the decimal in num is `0.5` or higher, the closest integer greater than `num` is returned. |
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.
LGTM
👋 @tanishq-singh-2301 |
Description
Closes #986
Type of Change
Checklist
main
branch.