This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Incorrect floating point math?
Using C++ console app in visual studio.
float percentCorrect;
percentCorrect = ( 7 / 5 );
printf("%f", percentCorrect);
output is 1.0000
OK so how do I get the correct decimal output??
7 and 5 are integers, so the compiler treats them as such and does integer division.
float percentCorrect;
percentCorrect = (7.0/5.0);
printf("%f", percentCorrect);
The above should print out the expected value.
You need to include a decimal: 7.0/5.0
7 and 5 are integer literals, so 7 / 5 performs integer division.
You need to have at least one floating literal for floating-point division to be performed:
float percentCorrect = 7.0 / 5.0;
Related
This question already has answers here:
Fastest way to get a positive modulo in C/C++
(9 answers)
How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers
(16 answers)
Closed 2 years ago.
In a code written by me, I have used both below functions to calculate mod of displayed negative number.
fmod(-10,11)
(-10, 11)
Though the correct answer is 1. It always displays the answer -10 in c++. How I can solve it?
From cppreference.com:
double fmod (double numer, double denom);
The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where n is x/y with its fractional part truncated.
The returned value has the same sign as x and is less than y in magnitude.
In your case it is -10 - (-10)/11 * 11 = -10 - 0 * 11 = -10, which is correct for that implementation of fmod. If you need another answer, you should implement your own version, as modulo is defined in different ways for negative numbers.
This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 3 years ago.
I want to write 4/3 as a fraction in C++. I know that 4/3 is equal to 1.33333. But when I write 4/3 in the code it outputs it's quotient which is 1. Can anyobody tell me how to write this number as fraction?
The code:
double vol_sphere(double radiusS){
return (4/3) * pi * pow(radiusS, 3);
}
Because 4 and 3 are both integers, when you perform division of two integers, the result will be also integer, so 1.333333333 will be only 1.
This question already has answers here:
Assigning a lower precision number to a higher precision in Fortran90
(1 answer)
Numerical Precision in Fortran 95:
(2 answers)
Closed 4 years ago.
I'm trying to understand the difference between these two:
REAL*8 X
X=1.5D0
WRITE(6,*) (0.6D0 + (2*x)/5.)
WRITE(6,*) (0.6 + (2*x)/5.D0)
I would expect them to give identical results, but I get instead
1.2000000000000000
1.2000000238418580
Why is 0.6 not cast to double precision in the second case even if it is supposed to be summed to a value in double precision? What is happening?
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 4 years ago.
I was calculating the volume of a sphere and after tons of research I found that I cannot use:
float sphereRadius = 2.33;
float volSphere = 0;
volSphere = (4/3) * (M_PI) * std::pow(sphereRadius, 3);
But must add the 3.0 instead to get the right answer.
volSphere = (4/3.0) * (M_PI) * std::pow(sphereRadius, 3);
Why must a decimal be added to get the correct calculation?
(4/3) is one integer divided by another integer, which results in another integer. An integer can't be 1.33 or anything like that, so it gets truncated to 1. With the decimal, you're telling it to be a double instead, and dividing an integer by a double results in a double, which supports fractions.
This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Closed 9 years ago.
I am doing the following
float years = (1/31536000) * 883102.00;
and I get years = 0000000
while the actual answer is 0.0.28
Any suggestion on what might be going wrong ?
You should instead do
float years = (1.0/31536000.0) * 883102.00;
or
float years = (1.0/31536000) * 883102.00;
might work as well. Your very first number is treated as integer.
Just do
float years = 883102.00/31536000;
This will save computation.
Since you are diving 1 by something and then multiplying.
Alternatively just set 1 to 1.00
1 is an int. The compiler then assumes that you are interested in working in ints and then 1/3153600 becomes 0. Just add a .0 to the 1 and your calc should work.
float years = (1.0/31536000) * 883102.00;
That first term is being cast as an int, and thus is being rounded to 0. Try this:
float years = (1.00 / 31536000.00) * 883102.00
(1/31536000) will yield 0 that multiplied by any number would be 0.
Make atleast one of numerator or denominator float (like 1.0 or 31536000.0)
883102.0 / 31536000 will do just what you want.
Because of the Integer-Division
(1/31536000)
the fractional digits get truncate and the result is "zero".
You have to add a dot:
(1.0/31536000.0) or (1./31536000.)