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.)
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:
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:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 5 years ago.
I think the title says everything. I want to define a variable i as the fraction 1/12. However, i is 0.
double i = 1/12;
std::cout << i; // Output: 0
Or, more specific, I want to calculate a power of something:
im_ = std::pow((1 + i), (1/12)) - 1;
However, the compile evaluates (1/12) as 0 and thus the result is wrong.
Simple because 1/12 is evaluated as integer math, not floating point math.
1/12 becomes 0 because integer math does not take into account the decimal fractions.
To get the expected result you will need to write down the numbers as a floating point literal, like this: 1.0/12.0.
More details can be found here: Why can't I return a double from two ints being divided
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;