Automatic casting

I need to write a program that receives the number n from the user, and then calculates the sum: s = 1/1 + 1/2 + ... + 1 / n.

I wrote this code:

 import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner unos = new Scanner(System.in); System.out.println("n=?"); int n = unos.nextInt(); double s = 0.0; for (int i = 1; i <= n; i++) { s = s + (1.0 / i); } System.out.println("s=" + s); } } 

How Java decides to convert the value of int i to double in this expression:

 s = s + (1.0 / i); 
+6
source share
6 answers

The rules that determine which type is converted / promoted to some other type are defined in the Java Language Spec Chapter 5 - Conversions and Promotions .

In particular, for most arithmetic operations, see the Binary Numeric Promotion section.

When an operator applies binary numeric advancement to a pair of operands, each of which must denote a value of a numeric type, the following rules apply to use the expanding transform (§5.1.2) to transform the operands as necessary

  • If one of the operands is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.

In your case, 1.0 is double, so i converted to double (expanding conversion). Since s already double, no further conversion is required.

+8
source

if there is no match between the types (and separating the double from int is not a match), he chooses one of the following values: (1) highest priority:

 (1) identity conversion (2) a widening primitive conversion (3) a widening reference conversion (4) a boxing conversion optionally followed by a widening reference conversion (5) an unboxing conversion optionally followed by a widening primitive conversion. 

in this case, he chose (2), expanding the primitives. It does not change double to int, because double-> int is not an identity and does not expand, so the only left choice is to extend int to double

Additional information: http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#184206

+5
source

When performing arithmetic, unlike types, Java tends to extend the types used to avoid loss of information.

If one of the variables is double, then java treats both variables as doubles.

This departure will publish all your doubts:

+3
source

It simply pushes i to the nearest double before division.

For more information, see "Java Language Specification" 5.1.2 "Extending Primitive Conversion" .

The rule of breaking the connection, which is even less biased, from half to even, namely:

If the fraction of y is 0.5, then q is an even integer to y.

Thus, for example, +23.5 becomes +24, +22.5 becomes +22, -22.5 becomes -22, and -23.5 becomes -24.

This method also considers positive and negative values ​​symmetrically and, therefore, are free of general bias if the original numbers are positive or negative with equal probability. In addition, for most reasonable distributions of y values, the expected (average) value of the rounded numbers is essentially the same as the original numbers, even if the latter are all positive (or all negative). However, this rule will still introduce a positive offset for even numbers (including zero) and a negative offset for odd numbers.

This round option to the closest method is also called unbiased rounding (ambiguous, and slightly offensive), convergent rounding, statistical rounding, Dutch rounding, Gaussian rounding, or banker rounding. It is widely used in bookkeeping.

This is the default rounding mode used by IEEE 754 computational functions and operators.

Source: Wikipedia

+1
source

see, when we perform some operation in which different data types are involved, then a smaller type value will always be converted to values ​​of a higher type, so that as a result the values ​​are not lost, and also the values ​​are compatible with each other.

+1
source

The rule is very simple

  • At first it looks if any of the operands is double, if true then it will convert a non-double operand to a double range
  • If the first statement is false, then it checks that the operands are of type float, if this is true, then it will convert the float operand to a range of the float.
  • If none of the above, then java saves the type as it is

See the code below for more details.

 public static void main(String[] args) { int int_val = 20; double doub_val = 20.0; float flo_val = 20.0f; /* lets see the first statement of my answer is true */ //## 1.0 I'm dividing double / float System.out.println(((Object)(doub_val/flo_val)).getClass().getName()); //## 1.1 I'm dividing double / int System.out.println(((Object)(doub_val/int_val)).getClass().getName()); //## 2.0 I'm dividing float / int System.out.println(((Object)(flo_val/10)).getClass().getName()); //## 3.0 I'm dividing int / int System.out.println(((Object)(int_val/10)).getClass().getName()); //## 3.1 I'm dividing double / double System.out.println(((Object)(doub_val/10.0)).getClass().getName()); //## 3.2 I'm dividing float / float System.out.println(((Object)(flo_val/10.0f)).getClass().getName()); } 
0
source

Source: https://habr.com/ru/post/895056/


All Articles