Integer division: How do you produce a double?
double num = 5;
That avoids a cast. But you'll find that the cast conversions are well-defined. You don't have to guess, just check the JLS. int to double is a widening conversion. From §5.1.2:
Widening primitive conversions do not
lose information about the overall
magnitude of a numeric value.[...]
Conversion of an int or a long value
to float, or of a long value to
double, may result in loss of
precision-that is, the result may lose
some of the least significant bits of
the value. In this case, the resulting
floating-point value will be a
correctly rounded version of the
integer value, using IEEE 754
round-to-nearest mode (§4.2.4).
5 can be expressed exactly as a double.
Force integer division on a double?
x = java.lang.Math.floor(x/y);
Relying on some Math function is arguably the best choice. "Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer."
If you need the symmetric version (truncation towards zero), you'll have to handle negative quotients:
floor(abs(x/y))*signum(x/y)
Dividing two integers to a double in java
This line here d = w[L] /v[L];
takes place over several steps
d = (int)w[L] / (int)v[L]
d=(int)(w[L]/v[L]) //the integer result is calculated
d=(double)(int)(w[L]/v[L]) //the integer result is cast to double
In other words the precision is already gone before you cast to double, you need to cast to double first, so
d = ((double)w[L]) / (int)v[L];
This forces java to use double maths the whole way through rather than use integer maths and then cast to double at the end
How to divide two ints and return a double in Java
(((double)a/(double)b))
This will give you = 0.57 and then this (int)
this will convert 0.57 to 0 here (int)(((double)a/(double)b));
because integer can only hold whole numbers
hence decimal values will be truncated.
use this to keep decimal values
double correctAnswer = (((double)a/(double)b));
Update : To achieve the decimal result, only one operand need to be cast as double and the optimizations for second parameter will be done by compiler.
Update Credits : @stackoverflowuser2010 and @cricket_007.
double correctAnswer = (double)a/b;
or
This can also be done without explicit
casting with Type Conversion or Type Casting which is done by the compiler.
Example Credits : @Andreas
double correctAnswer = a; // a , will automatically converted to double
correctAnswer /= b;
How can I divide two integers to get a double?
You want to cast the numbers:
double num3 = (double)num1/(double)num2;
Note: If any of the arguments in C# is a double
, a double
divide is used which results in a double
. So, the following would work too:
double num3 = (double)num1/num2;
For more information see:
Dot Net Perls
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;
How to make the division of 2 ints produce a float instead of another int?
Just cast one of the two operands to a float first.
v = (float)s / t;
The cast has higher precedence than the division, so happens before the division.
The other operand will be effectively automatically cast to a float by the compiler because the rules say that if either operand is of floating point type then the operation will be a floating point operation, even if the other operand is integral. Java Language Specification, §4.2.4 and §15.17
Dividing two integers to produce a float result
Cast the operands to floats:
float ans = (float)a / (float)b;
Related Topics
How to Return Datasnapshot Value as a Result of a Method
How to Create Recyclerview With Multiple View Types
Download a File With Android, and Showing the Progress in a Progressdialog
How to Pass an Object from One Activity to Another on Android
Setting Singleton Property Value in Firebase Listener
How to Launch an Activity from Another Application in Android
View'S Getwidth() and Getheight() Returns 0
How to Call a Method After a Delay in Android
Create a File from a Photo Uri on Android
How to Allow All Network Connection Types Http and Https in Android (9) Pie
How to Use Java 8 For Android Development
Firestore Whereequalto, Orderby and Limit(1) Not Working
Can't Start Eclipse - Java Was Started But Returned Exit Code=13
Example Images For Code and Mark-Up Q&As