-
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
Changes from 7 commits
28b78e6
6895c90
bca068e
284e872
63c6fbf
55828c9
91469eb
5f5a502
fbf7010
771c271
b64ce3b
238ec91
d4980c3
36eddff
244a7b9
18612ae
f4e5351
fb20ea2
757c3b5
392672e
c5bf18e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,69 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
--- | ||||||||||||||||||||||||||||||||||||||||||||||||
Title: 'round()' | ||||||||||||||||||||||||||||||||||||||||||||||||
Description: 'Returns the integer that is nearest to the argument, with halfway cases rounded away from zero.' | ||||||||||||||||||||||||||||||||||||||||||||||||
Subjects: | ||||||||||||||||||||||||||||||||||||||||||||||||
- 'Computer Science' | ||||||||||||||||||||||||||||||||||||||||||||||||
Tags: | ||||||||||||||||||||||||||||||||||||||||||||||||
- 'Functions' | ||||||||||||||||||||||||||||||||||||||||||||||||
- 'Arithmetic' | ||||||||||||||||||||||||||||||||||||||||||||||||
- 'Methods' | ||||||||||||||||||||||||||||||||||||||||||||||||
CatalogContent: | ||||||||||||||||||||||||||||||||||||||||||||||||
- 'learn-c-plus-plus' | ||||||||||||||||||||||||||||||||||||||||||||||||
- '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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
## Syntax | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
```pseudo | ||||||||||||||||||||||||||||||||||||||||||||||||
round(num); | ||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
The `num` parameter must be a `double`,`float`, or `long double`. The return value will be an integer. | ||||||||||||||||||||||||||||||||||||||||||||||||
Dusch4593 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
## 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 commentThe 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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
## Example 2 | ||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
Dusch4593 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||
```cpp | ||||||||||||||||||||||||||||||||||||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should include the |
||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
Dusch4593 marked this conversation as resolved.
Show resolved
Hide resolved
Dusch4593 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||
## Codebyte Example | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
The following example is runnable and rounds the halfway case away from zero: | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
```codebyte/cpp | ||||||||||||||||||||||||||||||||||||||||||||||||
#include <iostream> | ||||||||||||||||||||||||||||||||||||||||||||||||
#include <cmath> | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
int main() { | ||||||||||||||||||||||||||||||||||||||||||||||||
double num = 10.89; | ||||||||||||||||||||||||||||||||||||||||||||||||
double result; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
result = std::round(num); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for output comments in Codebyte snippets. |
||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
``` |
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.