Why is my double or int value is always 0 after division?
Your current calculation only involves integers and so will be affected by integer division (which truncates the result to the nearest integer value).
(currPixel / modifier) * newValue
| |
---------------integer division e.g. 10/3 = 3, not 3.333
The result is then cast to double, but the accuracy is lost before this point.
Consider the following:
#include <iostream>
using namespace std;
int main() {
int val1 = 10;
int val2 = 7;
int val3 = 9;
double outval1 = (val1 / val2) * val3;
double outval2 = ((double)val1 / val2) * val3;
cout << "without cast: " << outval1 << "\nwith cast: "<< outval2 << std::endl;
return 0;
}
The output of this is:
without cast: 9
with cast: 12.8571
See it here
Note that the cast has to be applied in the right place:
(double)(val1 / val2) * val3 == 9.0 //casts result of (val1/val2) after integer division
(val1 / val2) * (double)val3 == 9.0 //promotes result of (val1/val2) after integer division
((double)val1 / val2) * val3 == 12.8571 //promotes val2 before division
(val1 / (double)val2) * val3 == 12.8571 //promotes val1 before division
Due to promotion of the other operands, if in doubt you can just cast everything and the resulting code will be the same:
((double)val1 / (double)val2) * (double)val3 == 12.8571
It is a little more verbose though.
Division in double variable returning always zero
The result of 80/100
(both integers) is always 0.
Change it to 80.0/100.0
c# double - why is the answer always 0?
You are actually performing integer division here because the 2 numbers are whole. That forces the result of the calculation to be int
which truncates the precision so you end up with 0
.
You need to tell the compiler you are actually working with floating-point numbers and not int
's, just adding a floating point to one of the numbers should fix this e.g.
double test = (27096140.0 / 27216140);
Alternatively, you could actually declare the numbers as doubles
double a = 27096140;
double b = 27216140;
double test = (a / b);
Or even cast the number in the calculation
double test = ((double)27096140 / 27216140)
Why does value / 100 return 0?
It's due to integer division. By using a floating value for 100.0 instead of 100, the division is performed using floating point.
Try
int asteriskCount = (int)((aGrade/100.0)*50);
^^^^^
Integer division always zero
You are doing integer division.
Try the following and it will work as expected:
int x = 17;
double result = 1.0 / x;
The type of the 1
in the expression you have above is int
, and the type of x
is int. When you do int / int
, you get an int back. You need at least one of the types involved to be floating point (float
or double
) in order for floating point division to occur.
Unlike in Mathematics, division in C++ can either refer to truncated integer division (what you did) or floating point division (what I did in my example). Be careful of this!
In my example, explicitly what we have is double / int -> double
.
Java: Why is this double variable coming out 0?
Make that:
double check = 3.0 / 4;
and it'll work. You got 0
because 3 / 4
is an integer division, whose value is 0
.
Double value returns 0
That's because 1
and 3
are treated as integers
when you don't specify otherwise, so 1/3
evaluates to the integer
0
which is then cast to the double
0
. To fix it, try (1.0/3)
, or maybe 1D/3
to explicitly state that you're dealing with double values.
Why does 1 / 2 == 0 using double?
It's because of the data type.
When you do 1/2
that is integer division because two operands are integers, hence it resolves to zero (0.5 rounded down to zero).
If you convert any one of them to double, you'll get a double result.
double d = 1d/2;
or
double d = 1/2.0;
Why does dividing two int not yield the right value when assigned to double?
This is because you are using the integer division version of operator/
, which takes 2 int
s and returns an int
. In order to use the double
version, which returns a double
, at least one of the int
s must be explicitly casted to a double
.
c = a/(double)b;
Related Topics
Lifetime of Object Is Over Before Destructor Is Called
Conversion from Int** to Const Int**
How to Load a Bmp on Glut to Use It as a Texture
Erasing Vector::End from Vector
Why Can't We Declare Object of a Class Inside the Same Class
Free Function Versus Member Function
Why Is Iterating 2D Array Row Major Faster Than Column Major
C++ Static_Cast Runtime Overhead
Forward Declaration with Vector of Class Type - Pointer to Incomplete Class Type Not Allowed
Building Boost with Visual Studio 2013 (Express)
How to Tell Pyximport to Use the Cython --Cplus Option
Conversion from Void* to the Pointer of the Base Class
Is It Safe to Reinterpret_Cast an Enum Class Variable to a Reference of the Underlying Type
How to Implement a Map with Different Data Types as Values
What Does -Fpic Mean When Building a Shared Library