Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made eq attribute of standard objects int and float to meet the standards (IEEE 754) #2554

Merged
merged 21 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
# are not available for the project. For example a lot of
# package name contains capital letter and such names are conventional.
exclude_paths:
- "eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java"
- "eo-runtime/src/test/java/EOorg/EOeolang/EOintTest.java"
- "eo-maven-plugin/src/it/hash_package_layer/src/main/java/EOorg/EOeolang/Heaps.java"
- "eo-maven-plugin/src/it/hash_package_layer/src/main/java/EOorg/EOeolang/package-info.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOfloat$EOeq.java"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 I think we don't need these lines here anymore

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- "eo-runtime/src/main/java/EOorg/EOeolang/EOint$EOeq.java"
17 changes: 1 addition & 16 deletions eo-runtime/src/main/eo/org/eolang/float.eo
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,7 @@

[] > float
# Tests that $ = x
[x] > eq
x > value!
if. > @
and.
eq.
^.as-bytes > self-as-bytes!
00-00-00-00-00-00-00-00 > zero-as-bytes!
eq.
value.as-bytes > x-as-bytes!
zero-as-bytes
eq.
^.neg.as-bytes
value.neg.as-bytes
eq.
self-as-bytes
x-as-bytes
[x] > eq /bool

# Tests that $ < x
[x] > lt
Expand Down
16 changes: 1 addition & 15 deletions eo-runtime/src/main/eo/org/eolang/int.eo
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,7 @@

[] > int
# Tests that $ = x
[x] > eq
if. > @
and.
eq.
^.as-bytes > self-as-bytes!
00-00-00-00-00-00-00-00 > zero-as-bytes!
eq.
x.as-bytes > x-as-bytes!
zero-as-bytes
eq.
^.neg.as-bytes
x.neg.as-bytes
eq.
self-as-bytes
x-as-bytes
[x] > eq /bool

# Tests that $ < x
[x] > lt
Expand Down
67 changes: 67 additions & 0 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOfloat$EOeq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/*
* @checkstyle PackageNameCheck (4 lines)
*/
package EOorg.EOeolang;

import org.eolang.AtComposite;
import org.eolang.AtFree;
import org.eolang.Data;
import org.eolang.Param;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.Versionized;
import org.eolang.XmirObject;

/**
* EQ.
*
* @since 0.33
* @checkstyle TypeNameCheck (5 lines)
*/
@Versionized
@XmirObject(oname = "float.eq")
public class EOfloat$EOeq extends PhDefault {

/**
* Ctor.
* @param sigma Sigma
*/
public EOfloat$EOeq(final Phi sigma) {
super(sigma);
this.add("x", new AtFree());
this.add(
"φ",
new AtComposite(
this,
rho -> new Data.ToPhi(
new Param(rho).asStrictBytes(Double.BYTES).asNumber(Double.class) == (double)
new Param(rho, "x").asStrictBytes(Double.BYTES).asNumber(Double.class)
)
)
);
}
}
67 changes: 67 additions & 0 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOint$EOeq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/*
* @checkstyle PackageNameCheck (4 lines)
*/
package EOorg.EOeolang;

import org.eolang.AtComposite;
import org.eolang.AtFree;
import org.eolang.Data;
import org.eolang.Param;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.Versionized;
import org.eolang.XmirObject;

/**
* EQ.
*
* @since 0.33
* @checkstyle TypeNameCheck (5 lines)
*/
@Versionized
@XmirObject(oname = "int.eq")
public class EOint$EOeq extends PhDefault {

/**
* Ctor.
* @param sigma Sigma
*/
public EOint$EOeq(final Phi sigma) {
super(sigma);
this.add("x", new AtFree());
this.add(
"φ",
new AtComposite(
this,
rho -> new Data.ToPhi(
new Param(rho).asStrictBytes(Long.BYTES).asNumber(Long.class) == (long)
new Param(rho, "x").asStrictBytes(Long.BYTES).asNumber(Long.class)
)
)
);
}
}
21 changes: 21 additions & 0 deletions eo-runtime/src/main/java/org/eolang/Param.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,25 @@ public Bytes asBytes() {
}
return res;
}

/**
* Extracts BYTES of any type and checks if they have the appropriate size.
* @param size The appropriate size.
* @return Bytes.
*/
public Bytes asStrictBytes(final int size) {
final Bytes bytes = this.asBytes();
final int length = bytes.take().length;
if (length != size) {
throw new ExFailure(
String.format(
"The size of argument '.%s' is %d bytes, not '%d' as expected",
this.attr,
length,
size
)
);
}
return bytes;
}
}
56 changes: 37 additions & 19 deletions eo-runtime/src/test/eo/org/eolang/float-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@
0.0
TRUE

[] > zero-not-equal-to-neg-zero
[] > zero-equal-to-neg-zero
eq. > @
0.0.eq -0.0
FALSE
TRUE

[] > neg-zero-not-equal-to-zero
[] > neg-zero-equal-to-zero
eq. > @
-0.0.eq 0.0
FALSE
TRUE

[] > zero-not-greater-that-neg-zero
eq. > @
Expand All @@ -155,32 +155,30 @@
-0.0.lt 0.0
FALSE

[] > zero-not-gte-neg-zero
[] > zero-gte-neg-zero
eq. > @
0.0.gte -0.0
FALSE
TRUE

[] > neg-zero-not-gte-zero
[] > neg-zero-gte-zero
eq. > @
-0.0.gte 0.0
FALSE
TRUE

[] > zero-not-lte-neg-zero
[] > zero-lte-neg-zero
eq. > @
0.0.lte -0.0
FALSE
TRUE

[] > neg-zero-not-lte-zero
[] > neg-zero-lte-zero
eq. > @
-0.0.lte 0.0
FALSE
TRUE

[] > float-zero-not-eq-to-int-zero
[] > float-zero-eq-to-int-zero
eq. > @
eq.
0.0
0
FALSE
0.0
0

[] > eq-true
eq. > @
Expand All @@ -192,9 +190,29 @@
123.5.eq 42.4
FALSE

[] > compares-two-different-types
[] > compares-two-different-types-wrong-size-less
eq. > @
try
[]
3.14.eq "Hello" > @
[e]
e > @
nop
"The size of argument '.x' is 5 bytes, not '8' as expected"

[] > compares-two-different-types-wrong-bigger
eq. > @
try
[]
2.71.eq "123456789" > @
[e]
e > @
nop
"The size of argument '.x' is 9 bytes, not '8' as expected"

[] > compares-two-different-types-right-size
eq. > @
3.14.eq "Hello"
2.71.eq "12345678"
FALSE

[] > times-by-zero
Expand Down
34 changes: 25 additions & 9 deletions eo-runtime/src/test/eo/org/eolang/int-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@
0
TRUE

[] > int-zero-not-eq-to-float-zero
[] > int-zero-eq-to-float-zero
eq. > @
eq.
0
0.0
FALSE
TRUE

[] > eq-true
eq. > @
Expand All @@ -170,14 +170,30 @@
TRUE
"check int.eq.not"

[] > compares-two-different-types
[] > compares-two-different-types-wrong-size-less
eq. > @
not.
eq.
42
"Hello!"
TRUE
"check int.eq.not string"
try
[]
42.eq "Hello" > @
[e]
e > @
nop
"The size of argument '.x' is 5 bytes, not '8' as expected"

[] > compares-two-different-types-wrong-size-bigger
eq. > @
try
[]
414.eq "123456789" > @
[e]
e > @
nop
"The size of argument '.x' is 9 bytes, not '8' as expected"

[] > compares-two-different-types-right-size
eq. > @
68.eq "12345678"
FALSE

[] > calculates-fibonacci-number-with-recursion
[n] > fibo
Expand Down
Loading