Java - Implementación Math.pow

 
Vista:

Implementación Math.pow

Publicado por Rakan (43 intervenciones) el 04/11/2018 12:01:23
Math.pow delega el trabajo a StrictMath.pow, que a su vez lo delega a FdLibm.Pow.compute.
Me estaba mirando la implementación y me encuentro en la cabeza del método, tras un si exponente == 0 return 1 lo siguiente:

1
2
3
// +/-NaN return x + y to propagate NaN significands
            if (Double.isNaN(x) || Double.isNaN(y))
                return x + y;
Dentro de este contexto:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static strictfp double compute(final double x, final double y) {
            double z;
            double r, s, t, u, v, w;
            int i, j, k, n;
 
            // y == zero: x**0 = 1
            if (y == 0.0)
                return 1.0;
 
            // +/-NaN return x + y to propagate NaN significands
            if (Double.isNaN(x) || Double.isNaN(y))
                return x + y;
 
//Decenas de líneas haciendo otras cosas
 
}

Porqué si uno de los dos valores que se pasan como parametros no és numerico se retorna la suma de los dos?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Implementación Math.pow

Publicado por Kabuto (1378 intervenciones) el 04/11/2018 13:45:56
En realidad, si uno (o ambos) no son números, pues lo cierto es que no se van a poder sumar...
Así que ese return puede que solo tenga la intención de devolver un valor NaN y ya luego el programador obrará en consecuencia
Desde luego, es imposible que devuelva una suma.

Fijate en esto: StrictMath.pow()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
pow
public static double pow(double a,
                         double b)
Returns the value of the first argument raised to the power of the second argument. Special cases:
If the second argument is positive or negative zero, then the result is 1.0.
If the second argument is 1.0, then the result is the same as the first argument.
If the second argument is NaN, then the result is NaN.
If the first argument is NaN and the second argument is nonzero, then the result is NaN.
If
the absolute value of the first argument is greater than 1 and the second argument is positive infinity, or
the absolute value of the first argument is less than 1 and the second argument is negative infinity,
then the result is positive infinity.
If
the absolute value of the first argument is greater than 1 and the second argument is negative infinity, or
the absolute value of the first argument is less than 1 and the second argument is positive infinity,
then the result is positive zero.
If the absolute value of the first argument equals 1 and the second argument is infinite, then the result is NaN.
If
the first argument is positive zero and the second argument is greater than zero, or
the first argument is positive infinity and the second argument is less than zero,
then the result is positive zero.
If
the first argument is positive zero and the second argument is less than zero, or
the first argument is positive infinity and the second argument is greater than zero,
then the result is positive infinity.
If
the first argument is negative zero and the second argument is greater than zero but not a finite odd integer, or
the first argument is negative infinity and the second argument is less than zero but not a finite odd integer,
then the result is positive zero.
If
the first argument is negative zero and the second argument is a positive finite odd integer, or
the first argument is negative infinity and the second argument is a negative finite odd integer,
then the result is negative zero.
If
the first argument is negative zero and the second argument is less than zero but not a finite odd integer, or
the first argument is negative infinity and the second argument is greater than zero but not a finite odd integer,
then the result is positive infinity.
If
the first argument is negative zero and the second argument is a negative finite odd integer, or
the first argument is negative infinity and the second argument is a positive finite odd integer,
then the result is negative infinity.
If the first argument is finite and less than zero
if the second argument is a finite even integer, the result is equal to the result of raising the absolute value of the first argument to the power of the second argument
if the second argument is a finite odd integer, the result is equal to the negative of the result of raising the absolute value of the first argument to the power of the second argument
if the second argument is finite and not an integer, then the result is NaN.
If both arguments are integers, then the result is exactly equal to the mathematical result of raising the first argument to the power of the second argument if that result can in fact be represented exactly as a double value.
(In the foregoing descriptions, a floating-point value is considered to be an integer if and only if it is finite and a fixed point of the method ceil or, equivalently, a fixed point of the method floor. A value is a fixed point of a one-argument method if and only if the result of applying the method to the value is equal to the value.)
 
Parameters:
a - base.
b - the exponent.
Returns:
the value ab.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Implementación Math.pow

Publicado por Rakan (43 intervenciones) el 04/11/2018 15:13:01
Quizás sume los valores para que en caso de que alguno de estos tenga un formato susceptible a excepciones cause la excepción... Es lo único que se me ocurre haha
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Implementación Math.pow

Publicado por Tom (1831 intervenciones) el 04/11/2018 17:06:53
Efectivamente, ya que el if tiene un OR ... se asegura de devolver NaN incluso si x o y no lo son.

if (Double.isNaN(x) || Double.isNaN(y))
return x + y;

De todos modos no se si sirve de mucho mirar una implementación concreta .. podría variar entre distintas plataformas.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar