C++ double cant be equal to two int division [duplicate] - c++

This question already has answers here:
C++ wrong result of mathematical expression [closed]
(4 answers)
Closed 6 years ago.
I am a bit stuck with this problem:
int a = 5, b = 2;
double c = a / b;
cout << c;
This outputs:
2
Why ?
I can by pass this by using:
double aa = a,
bb = b;
c = aa / bb;
This outputs:
2.5
Help ! :(

In C++ language, any arithmetic operation between two integer values will return an int value. Said differently, the integer division is the Euclidian division. And only then that integer value is casted to a double.
If you want a double operation, you must force the division to operate on double values, either by casting one operand to double, or by multiplying it by 1.0 which is a double constant :
double c = 1.0 * a / b;
or
double c = static_cast<double>(a) / b;

You have to at least cast one of the ints to a double:
double c = a/(double)b;

Related

Why does c++ not print out 2/3 as 0.666667? [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 3 years ago.
I wrote
double r = 2/3;
cout << "r = " << r << endl;
Then it returns
r = 0
I would like it to show that r = 0.666667 or something like that instead of a 0. Especially I need to use that r = 2/3 to do calculation in my algorithm. How can I fix that?
Because both 2 and 3 are literals of type int, and integer division yields an integer result in C++. What you did is perform integer division first, then promote the integer result to double.
Instead, you want to cast at least one of the operands to float or double (a fractional floating point-type), then the other operand will be promoted to the same type and the resulting value will also be float or double.
Either of these will work:
double r = 2.0/3.0;
double r = 2.0/3;
double r = 2/3.0;

Print 2 decimals from float value [duplicate]

This question already has answers here:
C++ program converts fahrenheit to celsius
(8 answers)
Closed 7 years ago.
First of all, I want to say sorry because I think the doubt is so trivial... but I'm new programming in C++.
I have the following code:
int a = 234;
int b = 16;
float c = b/a;
I want to print the result from float c with 2 decimals (the result should be 0.06) but I don't get the expected result.
Can anyone can help me? I tried using CvRound() and setPrecision() but nothing works like I expect or, in my case, I don't know how to do them working.
The problem is actually blindingly simple. And has NOTHING whatsoever do do with settings such as precision.
a and b are of type int, so b/a is also computed to be of type int. Which involves rounding toward zero. For your values, the result will be zero. That value is then converted to be float. An int with value zero, when converted to float, gives a result of 0.0. No matter what precision you use to output that, it will still output a zero value.
To change that behaviour convert one of the values to float BEFORE doing the division.
float c = b/(float)a;
or
float c = (float)b/a;
The compiler, when it sees a float and and an int both participating in a division, converts the int to float first, then does a division of floats.
int a = 234;
int b = 16;
float c = b/(float)a;
float rounded_down = floorf(c * 100) / 100; /* floor value upto two decimal places */
float nearest = roundf(c * 100) / 100; /* round value upto two decimal places */
float rounded_up = ceilf(c * 100) / 100; /* ceiling value upto two decimal places */
If you just want to print the result, you can use a printf() formatting string to round:
printf("c = %.2f\n", number, pointer);
Otherwise, if you need c to calculate another value, you shouldn't round the original value, only the one to print.
try this:
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int a = 234;
int b = 16;
float c = float(b)/a;
cout<<fixed<<setprecision(2)<<c;
return 0;
}
Previously when c = b/a since a and b are integers so by integer division we were getting answer as 0 as per your program.
But if we typecast one of the variable(a or b) to float we will obtain decimal answer.
Number of digits after decimal may or may not be 2. To ensure this we can use fixed and setprecision. fixed will ensure that number of digits after decimal will be fixed. setprecision will set how many digits to be there after decimal.

double value subtraction without precision [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
int base = 12;
double number = 12.2112;
double c = number - base;
//// c=0.211199999999999983
this is c++ code,
How could I get the outcome: c= 0.2112,
0.2112 cannot be exactly represented in Floating Point Notation: https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
If it's extremely important to you that 0.2112 be represented exactly, then the usual solution looks a bit like this:
int base = 120000;
int number = 122112;
int c = number - base;
cout << "Value of c: " << (c / 10000.0) << endl;
Know, however, that what we're doing here is implementing fixed-point numbers. If you need fixed-point numbers in your implementation, it may be worthwhile to research and implement a full fixed-point class that does everything you're trying to accomplish here.

Problems while dividing by 100 [duplicate]

This question already has answers here:
Division in C++ not working as expected
(6 answers)
Closed 8 years ago.
Helo, I'm new to programming and run into an issue, I have an integer, for example 158, and I divide it by 100 that i get is 1, but I want 1.58 instead
It is probably known issue, but sorry, I'm noob, for now :)
Just cast this to float number
int i = 158;
float f = (float)i / 100; //less precision
double d = (double)i / 100; //more precision
//other way
int i = 158;
float f = i / 100.0; //less precision
double d = i / 100.0; //more precision
What you are doing is dividing integer from integer, in this case result always integer, to get floating point number at least one of two operand has to be floating point number.
You need to divide by 100.0 rather than 100
Dividing by an integer in C++ is always going to give you an integer, so it will never be completely accurate. That being said, it was mentioned above that you can divide by a double or long to get the accurate decimal number that you desire.

Rounding numbers in C++ error - expression must have integral or enum type [duplicate]

This question already has answers here:
round() for float in C++
(23 answers)
Closed 9 years ago.
I have a function which is designed to round a number down to the nearest even number.
double round(double d)
{
floor(d + 0.5);
if(d % 2 == 1)
{
d = d-1;
}
return d;
}
However, this returns the error "expression must have integral or enum type" when I try to compile the code. The error is reported from the same line as the if statement.
Can anyone point me in the right direction?
The % operator is only defined for integers. You wanna use the fmod() function.
Bill is right about a proper implementation of round(double x).
The floor() function returns a double:
double floor (double x);
which is a floating point type, not an 'integral type', like int or char. Instead of calling floor(d + 0.5); which rounds d and discards the result, you'd want something like:
int i = static_cast<int>(floor(d + 0.5));
return floor(d/2 + 0.5) * 2;
Of course doubles are an approximation. For 10^50 you won't get even numbers probably.
floor doesn't work in place, it returns the floor-ed value. Also % applies for integers so you can't re-use d. What you want is:
int i = floor(d + 0.5);
if(i % 2 == 1)
{
i = i-1;
}
return i;
This version will do what you ask, returning an int.
If the parameter d is outside the range of an int, it returns 0 instead.
(maybe you want to throw an OutOfRange exception or something)
int roundDown2Even(double d)
{
return (INT_MIN <= d && d <= INT_MAX)? ((int)d) & ~1 : 0;
}