Simple division in Java - is this a bug or a feature?
You're using integer division.
Try 7.0/10
instead.
java double number division giving strange output
In the 1st case first division is done so an int divided by an int gives a integer i.e, 11/12=0 Then this integer is converted to double .ie, 0.0. In the 2nd case a double(11.00) is divided by a integer(12).The integer 12 is then automatically type casted to double as division should occur with similar types. This division gives a double value which is exact in reality(0.916666667)
See this link https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
wrong results from simple java method
Change
double average = sum/numbers;
to
double average = (double)sum/numbers;
to force floating point division.
Otherwise, int division (which is the operation that takes place when dividing two variables of int type) will give you 2 when dividing 8/3 (as you do in your example).
Java, 15/6 equation returning 2.0?
double average = 15/6.0;
Division of int by int will return an int. That is why you get 2. Then, as you declared it as a double, it would add the .0 at the end.
If you divide a double by double (15/6.0, 15.0/6, or 15.0/6.0) it will come out as a double.
Java - custom integer division function
In
return dividend/divisor + dividend%divisor == 0 ? 0:1;
The addition of
dividend/divisor + dividend%divisor
is performed and then the result is compared to 0.
You want:
return dividend/divisor + (dividend%divisor == 0 ? 0:1);
or
return dividend/divisor + (dividend%divisor == 0) ? 0:1;
In order that only dividend%divisor
will be compared to 0
.
Looking to have a precise number
Dividing two int
s will use integer division, so 46/45 will yield 1. If you want to ceil the result, you'll need to perform a floating point division:
int pages = (int) Math.ceil(number / 45.0);
Java Double variables have strange values
In IEEE-754 binary double, we need to consider 1.1 and 1.2 in the binary representation:
1.2 = 0b1.001100110011001100110011001100110011001100110011001100110011...
1.1 = 0b1.000110011001100110011001100110011001100110011001100110011001...
note that we need infinitely many bits to represent them exactly in binary. double only has 53 bits of significance, we have to chop off the numbers:
1.2 = 0b1.001100110011001100110011001100110011001100110011001100110011...
1.1 = 0b1.000110011001100110011001100110011001100110011001100110011001...
^ round from here
==>
1.2 ~ 0b1.0011001100110011001100110011001100110011001100110011
(= exactly 1.1999999999999999555910790149937383830547332763671875)
1.1 ~ 0b1.0001100110011001100110011001100110011001100110011010
(= exactly 1.100000000000000088817841970012523233890533447265625)
Hence 1.2 - 1.1 is:
1.2 ~ 0b1.0011001100110011001100110011001100110011001100110011
- 1.1 ~ 0b1.0001100110011001100110011001100110011001100110011010
————————————————————————————————————————————————————————————————
0b0.00011001100110011001100110011001100110011001100110010000
(= exactly 0.09999999999999986677323704498121514916419982910156250000)
We can actually compute 60 / 0.0999999999999998667732370449812151491641998291015625 exactly, which gives
600.0000000000007993605777301137740672368493927467455286920109359612256820927...
^ 16th significant figure
that matches OP's result of
600.0000000000008
Related Topics
What Causes "Unable to Access Jarfile" Error
Scanner Nosuchelementexception
How to Read .Pem File to Get Private and Public Key
How Can Non-Ascii Characters Be Removed from a String
Program Freezes During Thread.Sleep() and with Timer
Spring: How to Inject an Httpservletrequest into a Request-Scoped Bean
How to Map a Postgresql Array with Hibernate
Serving Static Files from Alternate Path in Embedded Jetty
How to Get a Thread and Heap Dump of a Java Process on Windows That's Not Running in a Console
Test If a String Contains Any of the Strings from an Array
When Will a String Be Garbage Collected in Java
Passing Values Between Jframes
Multi-Project Test Dependencies with Gradle
Spring Security Configuration - Httpsecurity VS Websecurity
Java Gui Frameworks. What to Choose? Swing, Swt, Awt, Swingx, Jgoodies, Javafx, Apache Pivot
Use Cases and Examples of Gof Decorator Pattern for Io
What Is the Significance of Url-Pattern in Web.Xml and How to Configure Servlet