We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Incorrect output of `cupy.logaddexp()`
For this case, mathematically we should get `inf`, but CuPy returns `nan`:
```python
>>> np.logaddexp(np.inf, np.inf)
inf
>>>
>>> cp.logaddexp(np.inf, np.inf)
array(nan)

```
The reason is `in0-in1` gives `nan` when both are `inf`, and it propagates all the way out:
https://github.com/cupy/cupy/blob/4469fae998df33c72ff40ef954cb08b8f0004b18/cupy/_math/explog.py#L73



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `cupy/_math/explog.py`
Content:
```
1 from cupy import _core
2 from cupy._math import ufunc
3 
4 
5 exp = ufunc.create_math_ufunc(
6     'exp', 1, 'cupy_exp',
7     '''Elementwise exponential function.
8 
9     .. seealso:: :data:`numpy.exp`
10 
11     ''')
12 
13 
14 expm1 = ufunc.create_math_ufunc(
15     'expm1', 1, 'cupy_expm1',
16     '''Computes ``exp(x) - 1`` elementwise.
17 
18     .. seealso:: :data:`numpy.expm1`
19 
20     ''')
21 
22 
23 exp2 = _core.create_ufunc(
24     'cupy_exp2',
25     ('e->e', 'f->f', 'd->d', 'F->F', 'D->D'),
26     'out0 = pow(in0_type(2), in0)',
27     doc='''Elementwise exponentiation with base 2.
28 
29     .. seealso:: :data:`numpy.exp2`
30 
31     ''')
32 
33 
34 log = ufunc.create_math_ufunc(
35     'log', 1, 'cupy_log',
36     '''Elementwise natural logarithm function.
37 
38     .. seealso:: :data:`numpy.log`
39 
40     ''')
41 
42 
43 log10 = ufunc.create_math_ufunc(
44     'log10', 1, 'cupy_log10',
45     '''Elementwise common logarithm function.
46 
47     .. seealso:: :data:`numpy.log10`
48 
49     ''')
50 
51 
52 log2 = ufunc.create_math_ufunc(
53     'log2', 1, 'cupy_log2',
54     '''Elementwise binary logarithm function.
55 
56     .. seealso:: :data:`numpy.log2`
57 
58     ''')
59 
60 
61 log1p = ufunc.create_math_ufunc(
62     'log1p', 1, 'cupy_log1p',
63     '''Computes ``log(1 + x)`` elementwise.
64 
65     .. seealso:: :data:`numpy.log1p`
66 
67     ''')
68 
69 
70 logaddexp = _core.create_ufunc(
71     'cupy_logaddexp',
72     ('ee->e', 'ff->f', 'dd->d'),
73     'out0 = fmax(in0, in1) + log1p(exp(-fabs(in0 - in1)))',
74     doc='''Computes ``log(exp(x1) + exp(x2))`` elementwise.
75 
76     .. seealso:: :data:`numpy.logaddexp`
77 
78     ''')
79 
80 
81 logaddexp2 = _core.create_ufunc(
82     'cupy_logaddexp2',
83     ('ee->e', 'ff->f', 'dd->d'),
84     'out0 = fmax(in0, in1) + log2(1 + exp2(-fabs(in0 - in1)))',
85     doc='''Computes ``log2(exp2(x1) + exp2(x2))`` elementwise.
86 
87     .. seealso:: :data:`numpy.logaddexp2`
88 
89     ''')
90
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/cupy/_math/explog.py b/cupy/_math/explog.py
--- a/cupy/_math/explog.py
+++ b/cupy/_math/explog.py
@@ -70,7 +70,14 @@
 logaddexp = _core.create_ufunc(
     'cupy_logaddexp',
     ('ee->e', 'ff->f', 'dd->d'),
-    'out0 = fmax(in0, in1) + log1p(exp(-fabs(in0 - in1)))',
+    '''
+    if (in0 == in1) {
+        /* Handles infinities of the same sign */
+        out0 = in0 + log(2.0);
+    } else {
+        out0 = fmax(in0, in1) + log1p(exp(-fabs(in0 - in1)));
+    }
+    ''',
     doc='''Computes ``log(exp(x1) + exp(x2))`` elementwise.
 
     .. seealso:: :data:`numpy.logaddexp`
@@ -81,7 +88,14 @@
 logaddexp2 = _core.create_ufunc(
     'cupy_logaddexp2',
     ('ee->e', 'ff->f', 'dd->d'),
-    'out0 = fmax(in0, in1) + log2(1 + exp2(-fabs(in0 - in1)))',
+    '''
+    if (in0 == in1) {
+        /* Handles infinities of the same sign */
+        out0 = in0 + 1.0;
+    } else {
+        out0 = fmax(in0, in1) + log2(1 + exp2(-fabs(in0 - in1)));
+    }
+    ''',
     doc='''Computes ``log2(exp2(x1) + exp2(x2))`` elementwise.
 
     .. seealso:: :data:`numpy.logaddexp2`

{"golden_diff": "diff --git a/cupy/_math/explog.py b/cupy/_math/explog.py\n--- a/cupy/_math/explog.py\n+++ b/cupy/_math/explog.py\n@@ -70,7 +70,14 @@\n logaddexp = _core.create_ufunc(\n     'cupy_logaddexp',\n     ('ee->e', 'ff->f', 'dd->d'),\n-    'out0 = fmax(in0, in1) + log1p(exp(-fabs(in0 - in1)))',\n+    '''\n+    if (in0 == in1) {\n+        /* Handles infinities of the same sign */\n+        out0 = in0 + log(2.0);\n+    } else {\n+        out0 = fmax(in0, in1) + log1p(exp(-fabs(in0 - in1)));\n+    }\n+    ''',\n     doc='''Computes ``log(exp(x1) + exp(x2))`` elementwise.\n \n     .. seealso:: :data:`numpy.logaddexp`\n@@ -81,7 +88,14 @@\n logaddexp2 = _core.create_ufunc(\n     'cupy_logaddexp2',\n     ('ee->e', 'ff->f', 'dd->d'),\n-    'out0 = fmax(in0, in1) + log2(1 + exp2(-fabs(in0 - in1)))',\n+    '''\n+    if (in0 == in1) {\n+        /* Handles infinities of the same sign */\n+        out0 = in0 + 1.0;\n+    } else {\n+        out0 = fmax(in0, in1) + log2(1 + exp2(-fabs(in0 - in1)));\n+    }\n+    ''',\n     doc='''Computes ``log2(exp2(x1) + exp2(x2))`` elementwise.\n \n     .. seealso:: :data:`numpy.logaddexp2`\n", "issue": "Incorrect output of `cupy.logaddexp()`\nFor this case, mathematically we should get `inf`, but CuPy returns `nan`:\r\n```python\r\n>>> np.logaddexp(np.inf, np.inf)\r\ninf\r\n>>>\r\n>>> cp.logaddexp(np.inf, np.inf)\r\narray(nan)\r\n\r\n```\r\nThe reason is `in0-in1` gives `nan` when both are `inf`, and it propagates all the way out:\r\nhttps://github.com/cupy/cupy/blob/4469fae998df33c72ff40ef954cb08b8f0004b18/cupy/_math/explog.py#L73\r\n\r\n\n", "before_files": [{"content": "from cupy import _core\nfrom cupy._math import ufunc\n\n\nexp = ufunc.create_math_ufunc(\n    'exp', 1, 'cupy_exp',\n    '''Elementwise exponential function.\n\n    .. seealso:: :data:`numpy.exp`\n\n    ''')\n\n\nexpm1 = ufunc.create_math_ufunc(\n    'expm1', 1, 'cupy_expm1',\n    '''Computes ``exp(x) - 1`` elementwise.\n\n    .. seealso:: :data:`numpy.expm1`\n\n    ''')\n\n\nexp2 = _core.create_ufunc(\n    'cupy_exp2',\n    ('e->e', 'f->f', 'd->d', 'F->F', 'D->D'),\n    'out0 = pow(in0_type(2), in0)',\n    doc='''Elementwise exponentiation with base 2.\n\n    .. seealso:: :data:`numpy.exp2`\n\n    ''')\n\n\nlog = ufunc.create_math_ufunc(\n    'log', 1, 'cupy_log',\n    '''Elementwise natural logarithm function.\n\n    .. seealso:: :data:`numpy.log`\n\n    ''')\n\n\nlog10 = ufunc.create_math_ufunc(\n    'log10', 1, 'cupy_log10',\n    '''Elementwise common logarithm function.\n\n    .. seealso:: :data:`numpy.log10`\n\n    ''')\n\n\nlog2 = ufunc.create_math_ufunc(\n    'log2', 1, 'cupy_log2',\n    '''Elementwise binary logarithm function.\n\n    .. seealso:: :data:`numpy.log2`\n\n    ''')\n\n\nlog1p = ufunc.create_math_ufunc(\n    'log1p', 1, 'cupy_log1p',\n    '''Computes ``log(1 + x)`` elementwise.\n\n    .. seealso:: :data:`numpy.log1p`\n\n    ''')\n\n\nlogaddexp = _core.create_ufunc(\n    'cupy_logaddexp',\n    ('ee->e', 'ff->f', 'dd->d'),\n    'out0 = fmax(in0, in1) + log1p(exp(-fabs(in0 - in1)))',\n    doc='''Computes ``log(exp(x1) + exp(x2))`` elementwise.\n\n    .. seealso:: :data:`numpy.logaddexp`\n\n    ''')\n\n\nlogaddexp2 = _core.create_ufunc(\n    'cupy_logaddexp2',\n    ('ee->e', 'ff->f', 'dd->d'),\n    'out0 = fmax(in0, in1) + log2(1 + exp2(-fabs(in0 - in1)))',\n    doc='''Computes ``log2(exp2(x1) + exp2(x2))`` elementwise.\n\n    .. seealso:: :data:`numpy.logaddexp2`\n\n    ''')\n", "path": "cupy/_math/explog.py"}], "after_files": [{"content": "from cupy import _core\nfrom cupy._math import ufunc\n\n\nexp = ufunc.create_math_ufunc(\n    'exp', 1, 'cupy_exp',\n    '''Elementwise exponential function.\n\n    .. seealso:: :data:`numpy.exp`\n\n    ''')\n\n\nexpm1 = ufunc.create_math_ufunc(\n    'expm1', 1, 'cupy_expm1',\n    '''Computes ``exp(x) - 1`` elementwise.\n\n    .. seealso:: :data:`numpy.expm1`\n\n    ''')\n\n\nexp2 = _core.create_ufunc(\n    'cupy_exp2',\n    ('e->e', 'f->f', 'd->d', 'F->F', 'D->D'),\n    'out0 = pow(in0_type(2), in0)',\n    doc='''Elementwise exponentiation with base 2.\n\n    .. seealso:: :data:`numpy.exp2`\n\n    ''')\n\n\nlog = ufunc.create_math_ufunc(\n    'log', 1, 'cupy_log',\n    '''Elementwise natural logarithm function.\n\n    .. seealso:: :data:`numpy.log`\n\n    ''')\n\n\nlog10 = ufunc.create_math_ufunc(\n    'log10', 1, 'cupy_log10',\n    '''Elementwise common logarithm function.\n\n    .. seealso:: :data:`numpy.log10`\n\n    ''')\n\n\nlog2 = ufunc.create_math_ufunc(\n    'log2', 1, 'cupy_log2',\n    '''Elementwise binary logarithm function.\n\n    .. seealso:: :data:`numpy.log2`\n\n    ''')\n\n\nlog1p = ufunc.create_math_ufunc(\n    'log1p', 1, 'cupy_log1p',\n    '''Computes ``log(1 + x)`` elementwise.\n\n    .. seealso:: :data:`numpy.log1p`\n\n    ''')\n\n\nlogaddexp = _core.create_ufunc(\n    'cupy_logaddexp',\n    ('ee->e', 'ff->f', 'dd->d'),\n    '''\n    if (in0 == in1) {\n        /* Handles infinities of the same sign */\n        out0 = in0 + log(2.0);\n    } else {\n        out0 = fmax(in0, in1) + log1p(exp(-fabs(in0 - in1)));\n    }\n    ''',\n    doc='''Computes ``log(exp(x1) + exp(x2))`` elementwise.\n\n    .. seealso:: :data:`numpy.logaddexp`\n\n    ''')\n\n\nlogaddexp2 = _core.create_ufunc(\n    'cupy_logaddexp2',\n    ('ee->e', 'ff->f', 'dd->d'),\n    '''\n    if (in0 == in1) {\n        /* Handles infinities of the same sign */\n        out0 = in0 + 1.0;\n    } else {\n        out0 = fmax(in0, in1) + log2(1 + exp2(-fabs(in0 - in1)));\n    }\n    ''',\n    doc='''Computes ``log2(exp2(x1) + exp2(x2))`` elementwise.\n\n    .. seealso:: :data:`numpy.logaddexp2`\n\n    ''')\n", "path": "cupy/_math/explog.py"}]}