Skip to content

Commit

Permalink
fix precision issues in test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sasikumar87 committed Jan 10, 2025
1 parent 4a0a218 commit 703012c
Showing 1 changed file with 75 additions and 44 deletions.
119 changes: 75 additions & 44 deletions test/explorer/series_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3202,7 +3202,10 @@ defmodule Explorer.SeriesTest do

series = Series.sin(s)

assert Series.to_list(series) == [0.0, 1.0, 1.2246467991473532e-16, -2.4492935982947064e-16]
expected_series =
Series.from_list([0.0, 1.0, 1.2246467991473532e-16, -2.4492935982947064e-16])

assert all_close?(series, expected_series)
end

test "calculates the sine of all elements in the series for f32 input and outputs f64" do
Expand All @@ -3211,13 +3214,15 @@ defmodule Explorer.SeriesTest do

series = Series.sin(s)

assert Series.to_list(series) == [
0.0,
0.999999999999999,
-8.742278000372475e-8,
1.7484556000744883e-7
]
expected_series =
Series.from_list([
0.0,
0.999999999999999,
-8.742278000372475e-8,
1.7484556000744883e-7
])

assert all_close?(series, expected_series)
assert Series.dtype(series) == {:f, 64}
end
end
Expand All @@ -3228,8 +3233,9 @@ defmodule Explorer.SeriesTest do
s = Explorer.Series.from_list([0, pi / 2, pi, 2 * pi])

series = Series.cos(s)
expected_series = Series.from_list([1.0, 6.123233995736766e-17, -1.0, 1.0])

assert Series.to_list(series) == [1.0, 6.123233995736766e-17, -1.0, 1.0]
assert all_close?(series, expected_series)
end

test "calculates the cosine of all elements in the series for f32 input and outputs f64" do
Expand All @@ -3238,13 +3244,15 @@ defmodule Explorer.SeriesTest do

series = Series.cos(s)

assert Series.to_list(series) == [
1.0,
-4.371139000186241e-8,
-0.9999999999999962,
0.9999999999999847
]
expected_series =
Series.from_list([
1.0,
-4.371139000186241e-8,
-0.9999999999999962,
0.9999999999999847
])

assert all_close?(series, expected_series)
assert Series.dtype(series) == {:f, 64}
end
end
Expand All @@ -3256,12 +3264,15 @@ defmodule Explorer.SeriesTest do

series = Series.tan(s)

assert Series.to_list(series) == [
0.0,
1.633123935319537e16,
-1.2246467991473532e-16,
-2.4492935982947064e-16
]
expected_series =
Series.from_list([
0.0,
1.633123935319537e16,
-1.2246467991473532e-16,
-2.4492935982947064e-16
])

assert all_close?(series, expected_series)
end

test "calculates the tangent of all elements in the series for f32 input and outputs f64" do
Expand All @@ -3270,13 +3281,15 @@ defmodule Explorer.SeriesTest do

series = Series.tan(s)

assert Series.to_list(series) == [
0.0,
-22_877_332.42885646,
8.742278000372508e-8,
1.7484556000745148e-7
]
expected_series =
Series.from_list([
0.0,
-22_877_332.42885646,
8.742278000372508e-8,
1.7484556000745148e-7
])

assert all_close?(series, expected_series)
assert Series.dtype(series) == {:f, 64}
end
end
Expand All @@ -3286,16 +3299,18 @@ defmodule Explorer.SeriesTest do
s = Explorer.Series.from_list([0.0, 1.0])

series = Series.asin(s)
expected_series = Series.from_list([0.0, 1.5707963267948966])

assert Series.to_list(series) == [0.0, 1.5707963267948966]
assert all_close?(series, expected_series)
end

test "calculates the arcsine of all elements in the series for f32 input and outputs f64" do
s = Explorer.Series.from_list([0.0, 1.0], dtype: :f32)

series = Series.asin(s)
expected_series = Series.from_list([0.0, 1.5707963267948966])

assert Series.to_list(series) == [0.0, 1.5707963267948966]
assert all_close?(series, expected_series)
assert Series.dtype(series) == {:f, 64}
end
end
Expand All @@ -3305,16 +3320,18 @@ defmodule Explorer.SeriesTest do
s = Explorer.Series.from_list([0.0, 1.0])

series = Series.acos(s)
expected_series = Series.from_list([1.5707963267948966, 0.0])

assert Series.to_list(series) == [1.5707963267948966, 0.0]
assert all_close?(series, expected_series)
end

test "calculates the arccosine of all elements in the series for f32 input and outputs f64" do
s = Explorer.Series.from_list([0.0, 1.0], dtype: :f32)

series = Series.acos(s)
expected_series = Series.from_list([1.5707963267948966, 0.0])

assert Series.to_list(series) == [1.5707963267948966, 0.0]
assert all_close?(series, expected_series)
assert Series.dtype(series) == {:f, 64}
end
end
Expand All @@ -3324,16 +3341,18 @@ defmodule Explorer.SeriesTest do
s = Explorer.Series.from_list([0.0, 1.0])

series = Series.atan(s)
expected_series = Series.from_list([0.0, 0.7853981633974483])

assert Series.to_list(series) == [0.0, 0.7853981633974483]
assert all_close?(series, expected_series)
end

test "calculates the arctangent of all elements in the series for f32 input and outputs f64" do
s = Explorer.Series.from_list([0.0, 1.0], dtype: :f32)

series = Series.atan(s)
expected_series = Series.from_list([0.0, 0.7853981633974483])

assert Series.to_list(series) == [0.0, 0.7853981633974483]
assert all_close?(series, expected_series)
assert Series.dtype(series) == {:f, 64}
end
end
Expand All @@ -3344,8 +3363,9 @@ defmodule Explorer.SeriesTest do
s = Explorer.Series.from_list([-2 * pi, -pi, -pi / 2, 0, pi / 2, pi, 2 * pi])

series = Series.degrees(s)
expected_series = Series.from_list([-360.0, -180.0, -90.0, 0.0, 90.0, 180.0, 360.0])

assert Series.to_list(series) == [-360.0, -180.0, -90.0, 0.0, 90.0, 180.0, 360.0]
assert all_close?(series, expected_series)
end

test "converts the given series of radians to degrees for f32 input and outputs f64" do
Expand All @@ -3354,16 +3374,18 @@ defmodule Explorer.SeriesTest do

series = Series.degrees(s)

assert Series.to_list(series) == [
-360.00001001791264,
-180.00000500895632,
-90.00000250447816,
0.0,
90.00000250447816,
180.00000500895632,
360.00001001791264
]
expected_series =
Series.from_list([
-360.00001001791264,
-180.00000500895632,
-90.00000250447816,
0.0,
90.00000250447816,
180.00000500895632,
360.00001001791264
])

assert all_close?(series, expected_series)
assert Series.dtype(series) == {:f, 64}
end
end
Expand All @@ -3374,17 +3396,19 @@ defmodule Explorer.SeriesTest do
s = Explorer.Series.from_list([-360.0, -180.0, -90.0, 0.0, 90.0, 180.0, 360.0])

series = Series.radians(s)
expected_series = Series.from_list([-2 * pi, -pi, -pi / 2, 0, pi / 2, pi, 2 * pi])

assert Series.to_list(series) == [-2 * pi, -pi, -pi / 2, 0, pi / 2, pi, 2 * pi]
assert all_close?(series, expected_series)
end

test "converts the given series of degrees to radians for f32 input and outputs f64" do
pi = :math.pi()
s = Explorer.Series.from_list([-360.0, -180.0, -90.0, 0.0, 90.0, 180.0, 360.0], dtype: :f32)

series = Series.radians(s)
expected_series = Series.from_list([-2 * pi, -pi, -pi / 2, 0, pi / 2, pi, 2 * pi])

assert Series.to_list(series) == [-2 * pi, -pi, -pi / 2, 0, pi / 2, pi, 2 * pi]
assert all_close?(series, expected_series)
assert Series.dtype(series) == {:f, 64}
end
end
Expand Down Expand Up @@ -6728,4 +6752,11 @@ defmodule Explorer.SeriesTest do
assert Series.to_list(sj) == [%{"n" => 1.0}]
end
end

defp all_close?(a, b, tol \\ 1.0e-8) do
Series.subtract(a, b)
|> Series.abs()
|> Series.less_equal(tol)
|> Series.all?()
end
end

0 comments on commit 703012c

Please sign in to comment.