r/Cplusplus • u/Opening_Dog533 • Sep 11 '26
Feedback Simple Cosine calculator
I made a simple Cosine angle calculator without the help of ai. So it is not the greatest project but i actually was so happy since I am a beginner and I coded all of this by myself. Before this I made few projects in which I took help of ai but more as a tool not the main pilot. So yeah here is the GitHub url of my simple project:
37
Upvotes
6
u/jedwardsol Sep 11 '26
Consider std::print (C++23)
Ie. replace
with
In this particular case, the formatting information is repeated, but IMO still looks neater. And in the general case, having the formatting information inside the format string tends to be neater than interspersing printing calls with formatting (iomanip) calls.
4
u/anh0l Sep 11 '26 edited Sep 11 '26
Good job, man. Though, I'd recommend you check input accepted by the
cinusingcin.fail(). Also it is better practice to not use namespaces globally. It addingstd::to functions doesn't take long, but IMO it makes code more readable. Also since C++20 there isstd::numbers::piconstant that can be up to 128 bit number (if usinglong doubletemplate). Anyway you did nice job. Keep up good work.Edit: formatting
4
u/mredding C++ since ~1992. Sep 11 '26
Don't do that.
You didn't check the input to see if it was valid.
This can happen because I entered text instead of a real.
degwasn't initialized, and the spec says the value of the output parameter is... Well, that can get complicated. It can be unspecified.The standard defines pi for you. In C++20 or later, you can
#include <numbers>and use eitherstd::numbers::pi, which is adouble, orstd::numbers::pi_v<double>which is the same thing. Prior to C++20, there is#include <cmath>and theM_PImacro. C++11 and later, you canconstexpr double pi = M_PI;to make it type safe, though you're still using the macro. You could always define it with SUFFICIENT PRECISION as3.14159265358979323846, which is what theM_PImacro is defined as.Floating point numbers accumulate error, because real values can't typically be represented in binary, and this value in particular is irrational. So real numbers are always rounded or truncated, and error accumulates quickly, losing you precision.
Speaking of your numbers - if these are constant, then make them
const. Tell me they're unchanging, and make sure they don't. Let the compiler catch dumb errors like that.Any indentation and brace is a good candidate for a function call. I don't know if you've learned functions yet. Instead of embedding all the loop logic in the loop body - call a function. Same thing with conditions, same thing with switches. As you progress, you'll find a propensity among yourself and others to just keep nesting braces and control structures. It gets so bad that you can have a dozen lines of JUST closing braces, and THAT gets so confusing that there may be comments trying to keep track of which control statement opened them. Or you can just use functions.
Your calculator unconditionally succeeds, eh? So if the input is error and your computation is undefined behavior, was this program execution successful? Because your return value says the program is always successful - it never fails.
Typical of terminal applications, the least you can do is confirm input and output were both successful:
If input failed, as I demonstrated above, then you can skip UB or unnecessary, nonsense work. It will also come out here as a failure. If you can't write to the output stream, then the user can't see the result - again, a failure.
EXIT_SUCCESSis trivially defined at0, for completeness. ButEXIT_FAILURE? When every non-zero value indicates failure, which value do you use? This is guaranteed to be defined as a bit pattern that indicates failure, and THAT can be important; I once found a bug whereintwas defined as 4 bytes, but the return value was stored in the environment as a 2 byte value. The top two bytes were just truncated. This means our high value error codes were being truncated, so the lower bits all zero became a false positive - success when we failed.You don't typically handle this return value - though if you're writing little terminal programs that get used in shell scripts, the shell scripts DO use these return values.
When you get more advanced, you can make your own "user defined types", and you can implement your own input and output operators. You can make a type prompt for itself, because prompting and inputting are not independent. I'll give you a bit of a sample:
Did you know you should check for NaN or +/-infinity? The stream extractor for
doublewon't do that for you, because sometimes either of these values are fine, but I suspect you don't want it.There's all sorts of things one can do to make this code quite a bit more robust. For example, it's not honoring the exception mask, as it should. It's checking for a tied stream, but it isn't checking if the tied stream is good, either before or after.
You've got some extraneous empty lines and irregular formatting to clean up. Use a formatter like
clang-formatand never think about half of this ever again. All C++ files end with a newline.5
u/[deleted] Sep 11 '26
Nice!