Skip to content

Commit 4cb0d56

Browse files
authored
Merge pull request #4959 from eshabakhov/#4950
feat(#4950): moved sqrt from real.eo to a separate object with tests
2 parents c9ed505 + ec89435 commit 4cb0d56

3 files changed

Lines changed: 96 additions & 72 deletions

File tree

eo-runtime/src/main/eo/ms/real.eo

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515
[num] > real
1616
num > @
1717

18-
# Returns the positive square root of a `num` as `org.eolang.number`.
19-
# @todo #4751:15min Move `sqrt` to a separate object with it's tests.
20-
# It's atom, don't forget change `EOreal$EOsqrt`.
21-
# For more information check this ticket:
22-
# https://github.com/objectionary/eo/issues/4751
23-
[] > sqrt ?
24-
2518
# Returns the natural logarithm `e` of a `num` as `org.eolang.number`.
2619
# @todo #4751:15min Move `ln` to a separate object with it's tests.
2720
# It's atom, don't forget change `EOreal$EOln`.
@@ -89,66 +82,6 @@
8982
pow 4 5
9083
1024
9184

92-
# Tests that square root of zero equals zero.
93-
[] +> tests-sqrt-check-zero-input
94-
lt. +> @
95-
abs
96-
real
97-
minus.
98-
0
99-
sqrt.
100-
real 0
101-
0.00000000001
102-
103-
# Tests that square root of negative number returns NaN.
104-
[] +> tests-sqrt-check-negative-input
105-
is-nan. +> @
106-
sqrt.
107-
real -0.1
108-
109-
# Tests that square root calculation works correctly for float input.
110-
[] +> tests-sqrt-check-float-input
111-
lt. +> @
112-
abs
113-
real
114-
minus.
115-
2
116-
sqrt.
117-
real
118-
4
119-
0.00000000001
120-
121-
# Tests that square root calculation works correctly for integer input.
122-
[] +> tests-sqrt-check-int-input
123-
lt. +> @
124-
abs
125-
real
126-
minus.
127-
9
128-
sqrt.
129-
real
130-
81
131-
0.00000000001
132-
133-
# Tests that square root of NaN returns NaN.
134-
[] +> tests-sqrt-check-nan-input
135-
is-nan. +> @
136-
sqrt.
137-
real nan
138-
139-
# Tests that square root of positive infinity equals positive infinity.
140-
[] +> tests-sqrt-check-infinity-n1
141-
eq. +> @
142-
sqrt.
143-
real positive-infinity
144-
positive-infinity
145-
146-
# Tests that square root of negative infinity returns NaN.
147-
[] +> tests-sqrt-check-infinity-n2
148-
is-nan. +> @
149-
sqrt.
150-
real negative-infinity
151-
15285
# Tests that natural logarithm of negative float returns NaN.
15386
[] +> tests-ln-of-negative-float-is-nan
15487
is-nan. +> @

eo-runtime/src/main/eo/ms/sqrt.eo

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
+alias ms.abs
2+
+alias ms.real
3+
+architect yegor256@gmail.com
4+
+home https://github.com/objectionary/eo
5+
+package ms
6+
+rt jvm org.eolang:eo-runtime:0.0.0
7+
+rt node eo2js-runtime:0.0.0
8+
+version 0.0.0
9+
+spdx SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
10+
+spdx SPDX-License-Identifier: MIT
11+
12+
# Returns the positive square root of a `num` as `org.eolang.number`.
13+
[num] > sqrt ?
14+
# Tests that sqrt of zero is zero.
15+
[] +> tests-sqrt-zero
16+
eq. > @
17+
sqrt 0.0
18+
0.0
19+
20+
# Tests that sqrt of one is one.
21+
[] +> tests-sqrt-one
22+
eq. > @
23+
sqrt 1.0
24+
1.0
25+
26+
# Tests that sqrt of a perfect square.
27+
[] +> tests-sqrt-four
28+
eq. > @
29+
sqrt 4.0
30+
2.0
31+
32+
# Tests that sqrt of a non-perfect square.
33+
[] +> tests-sqrt-two
34+
eq. > @
35+
sqrt 2.0
36+
1.4142135623730951
37+
38+
# Tests that sqrt of a fractional number.
39+
[] +> tests-sqrt-point-five
40+
eq. > @
41+
sqrt 0.25
42+
0.5
43+
44+
# Tests that square root of zero equals zero.
45+
[] +> tests-sqrt-check-zero-input
46+
lt. +> @
47+
abs
48+
real
49+
minus.
50+
0
51+
sqrt 0
52+
0.00000000001
53+
54+
# Tests that square root of negative number returns NaN.
55+
[] +> tests-sqrt-check-negative-input
56+
is-nan. +> @
57+
sqrt -0.1
58+
59+
# Tests that square root calculation works correctly for number input.
60+
[] +> tests-sqrt-check-float-input
61+
lt. +> @
62+
abs
63+
real
64+
minus.
65+
2
66+
sqrt 4
67+
0.00000000001
68+
69+
# Tests that square root of NaN returns NaN.
70+
[] +> tests-sqrt-check-nan-input
71+
is-nan. +> @
72+
sqrt nan
73+
74+
# Tests that square root of positive infinity equals positive infinity.
75+
[] +> tests-sqrt-check-infinity-n1
76+
eq. +> @
77+
sqrt positive-infinity
78+
positive-infinity
79+
80+
# Tests that square root of negative infinity returns NaN.
81+
[] +> tests-sqrt-check-infinity-n2
82+
is-nan. +> @
83+
sqrt negative-infinity

eo-runtime/src/main/java/org/eolang/EOms/EOreal$EOsqrt.java renamed to eo-runtime/src/main/java/org/eolang/EOms/EOsqrt.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,33 @@
99
*/
1010
package org.eolang.EOms; // NOPMD
1111

12+
import org.eolang.AtVoid;
1213
import org.eolang.Atom;
1314
import org.eolang.Dataized;
1415
import org.eolang.PhDefault;
1516
import org.eolang.Phi;
1617
import org.eolang.XmirObject;
1718

1819
/**
19-
* Real.sqrt.
20+
* Sqrt.
2021
*
2122
* @since 0.40
2223
* @checkstyle TypeNameCheck (100 lines)
2324
*/
24-
@XmirObject(oname = "real.sqrt")
25-
@SuppressWarnings("PMD.AvoidDollarSigns")
26-
public final class EOreal$EOsqrt extends PhDefault implements Atom {
25+
@XmirObject(oname = "sqrt")
26+
public final class EOsqrt extends PhDefault implements Atom {
27+
/**
28+
* Ctor.
29+
*/
30+
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
31+
public EOsqrt() {
32+
this.add("num", new AtVoid("num"));
33+
}
34+
2735
@Override
2836
public Phi lambda() {
2937
return new ToPhi(
30-
Math.sqrt(new Dataized(this.take(Phi.RHO)).asNumber())
38+
Math.sqrt(new Dataized(this.take("num")).asNumber())
3139
);
3240
}
3341
}

0 commit comments

Comments
 (0)