(C++)trying to convert double to integer (explaining) [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
saw this in an example
double x = 50.2
cout << (double)(int)(x);
this works fine , and also it works without the (double) ,
so why adding the (double) there?

You don't have to add (Double) there, why indeed. You are converting 50.2 to int wich evaluates to 50, then you are converting 50 back to double wich stays at 50 If you print it. The only reason i can think to do this is If it's inside some function with a return double value or to extract the integer part of a Double value, but it's not a good practice at all.

Related

Why is Visual studio returning only integers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I just upgraded my computer and had to reinstall Visual Studio 2019, and I realized for some reason I can't work with float values.
For example if I do a cout << (3/2); , it will return 1.
I've tried looking into the float.h file but I don't know what to change.
I guess it's a simple fix, I tried to google it but I didn't find a solution yet
You did integer division, so you got an integer back. If you tried std::cout << (3.0 / 2.0);, you'd get a double.
If you want floats: std::cout << (3.0f / 2.0f); gets you there.
How you type your literals matters in C++.

writing c++ program "fraction of double" [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I wanna write a c++ program that read a double value and after reading it print only the fraction of that number, for example if the input was 14.25 then the output must be 0.25
should include iomanip to it?
One approach would be to
Round the number down to the nearest integer - you can use the floor() function (http://www.cplusplus.com/reference/cmath/floor/)
Subtract that integer from the original number to get the result

Taking input using delimiter in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
After taking those values I have to store the values in two different arrays (say x and y)
So the only error I can immediately see is this
float* a = NULL;
a = new(nothrow)float;
which should be
float* a = new float[count];
Your version only allocates enough space for a single float when you really need space for count floats.
When you have code that isn't working, and you want to ask a question about it you really should say exactly what happens when you run the code. Doing this will help get you better answers.

Division Console output with a comma not rounded [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Is there a way for the console output to be a decimal number with a comma my output is always rounded and i can barely us it like this.
Int a = 90 ;
Int b = 7;
Int c = a/b;
This out puts 13 not the actual value.
It is advice rewritten from my comment:
Please check any elementary C++ tutorial :) For your information there isn't bult-in type Int in C++, so it seems that you want to ask about int type division. The answer could be: store your result in some floating point type as float :)
You're welcome!
use float or double instead. int will never provide you with decimals.
Example:
float a=90,b=7;
float c = a/b;
printf("%.2lf",c);
The code avobe mentioned will write 2 decimals. If you want more, just change the 2 here %.2lf for your amount of decimals required.

How to write this mathematical formula in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a simple formula and I want to use it in C++ application.
Not sure how to rewrite in C++.
logn(5, abs((a*b - d*c) / (tan(c) + sin(d))))
where logn is:
double logn(double base, double x) {
return log(x) / log(base);
}
and the header cmath is included for the other functions.
You can do this, be careful with libs, namespaces, and radian arguments
z = a*b - d*c;
s = tan(c) + sin(d);
num = abs(z/s);
log(num)/log(5);
http://www.cplusplus.com/reference/cmath/