Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most importantly, using floating point variables (such as float or double in java) is always dangerous due to finite representation of them in binary system. For example:</p> <pre><code>double a = 1.000001; double b = 2.0000001; System.out.println(a+b); </code></pre> <p>will print 3.0000010999999995</p> <p>In your code, expression</p> <pre><code>1/(i+2) </code></pre> <p>is treated as division of integers and 1/3 results in 0. Both expressions 1 and (i+2) are integers. To get more precise result you should know that any operation result will be returned in "the most needed precision". Which is - if one of your expression is of more precise type - such as long, float, double then resulting value will be of most precise type given. For example:</p> <pre><code>(2+3L) - expression result will be of long type (2+2.1) - expression result will be of double type (2.1f+2.0d) - expression result will be of double type </code></pre> <p>(L stands for long type)</p> <p>There are several ways you can express floating point literals in Java:</p> <ol> <li>value of <b>float</b> precision can be expressed with adding suffix 'f' or 'F' i.e. float a=1.2f</li> <li>value of <b>double</b> precision can be expressed by adding suffix 'd' or 'D' i.e. double a=2.3D</li> <li>apart from suffixes value type can be <b>casted</b> to required type using (float) or (double) operator i.e. float a = (float) 1.2;</li> <li><p>another important thing is <b>default representation</b> of value without suffix. it's by default <b>double</b>, so exemplary:</p> <pre><code>float a = 1.2; </code></pre> <p>will not even compile, because such assigment will be concerned as lossy casting and compiler would require clear intent from programmer (this is usually done with adding suffix to values or casting value).</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload