Home java What is the difference between Float and Double in Java?

What is the difference between Float and Double in Java?

Author

Date

Category

As far as I remember, float and double differ in the ranges, which leads to different accuracy and speed of calculations. However, today noticed that with different numbers rounding occurs in these data types in different ways.
The screenshots see that

  • in the first case float wreshes the value, and double rounds
    (EXAMPLE1 )
  • second case: float rounded, double engagement
    value (EXAMPLE2 )

What else there is a difference between data types float and double or I do not see something in the stop?


Answer 1, Authority 100%

The principal nuance here is that you operate with decimal numbers, and Float and Double & NBSP; – binary. Those numbers that you share and get as a result, failure of in the binary number system without periodic fractions. Read more:

Calculations on floating point numbers do not work

What you watch is not rounding and enveloping, and computer attempts to impack the inequate and pretend that everything is really fine. Since the number of bits at the Mantissa and the exponential types of Float and Double are different, then rounding and “wrapping” will occur from your point of view “unpredictable”. This situation is also aggravated by the fact that the accuracy of calculations on the conscience of the processor, which, generally speaking, is not obliged to issue accuracy to the last sign. On another computer, you can see other results – if you do not turn on the special “predictable” mode, which is slower. But even in this case, you will only get the same, but “unpredictable” results on different computers.

When you work with Float and Double, please accept as a measure that they are always with the error.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions