Skip to content

Fix precision when converting to json and add more test cases #508

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

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 32 additions & 11 deletions plotly/plotly_aux/Test_m2json.m
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
classdef Test_m2json < matlab.unittest.TestCase
methods (Test)
function testLowPrecisionInRange0to10(tc)
values = 1 + (1:5) + 0.234;
expected = "[2.234,3.234,4.234,5.234,6.234]";
tc.verifyEqual(string(m2json(values)), expected);
end

function testInRange0to10(tc)
values = 1 + (1:5) + 0.23456789;
expected = "[2.235,3.235,4.235,5.235,6.235]";
expected = "[2.23456789,3.23456789,4.23456789,5.23456789," ...
+ "6.23456789]";
tc.verifyEqual(string(m2json(values)), expected);
end

function test2dArrayInRange0to10(tc)
values = 1 + (1:5) + (0:1)' + 0.23456789;
expected = "[[2.235,3.235,4.235,5.235,6.235]," ...
+ "[3.235,4.235,5.235,6.235,7.235]]";
values = 1 + (1:5) + (0:1)' + 0.234;
expected = "[[2.234,3.234,4.234,5.234,6.234]," ...
+ "[3.234,4.234,5.234,6.234,7.234]]";
tc.verifyEqual(string(m2json(values)), expected);
end

function testLowPrecisionInRange1e6to1e5(tc)
values = 1e-6 * (1 + (1:5) + 0.234);
expected = "[2.234e-06,3.234e-06,4.234e-06,5.234e-06," ...
+ "6.234e-06]";
tc.verifyEqual(string(m2json(values)), expected);
end

function testInRange1e6to1e5(tc)
values = 1e-6 * (1 + (1:5) + 0.23456789);
expected = "[2.235e-06,3.235e-06,4.235e-06,5.235e-06," ...
+ "6.235e-06]";
expected = "[2.23456789e-06,3.23456789e-06,4.23456789e-06," ...
+ "5.23456789e-06,6.23456789e-06]";
tc.verifyEqual(string(m2json(values)), expected);
end

Expand All @@ -36,21 +50,28 @@ function testInRange1e14Plus1e7Plus0to1(tc)

function testLogScaledVariables(tc)
values = 1e14 + 10.^(1:5) + 0.23456789;
expected = "[1e+14,1.000000000001e+14,1.00000000001e+14," ...
+ "1.0000000001e+14,1.000000001e+14]";
expected = "[100000000000010,100000000000100," ...
+ "100000000001000,100000000010000,100000000100000]";
tc.verifyEqual(string(m2json(values)), expected);
end

function testLowPrecisionInRangeMinus10to0(tc)
values = -(1 + (1:5) + 0.234);
expected = "[-2.234,-3.234,-4.234,-5.234,-6.234]";
tc.verifyEqual(string(m2json(values)), expected);
end

function testInRangeMinus10to0(tc)
values = -(1 + (1:5) + 0.23456789);
expected = "[-2.235,-3.235,-4.235,-5.235,-6.235]";
expected = "[-2.23456789,-3.23456789,-4.23456789," ...
+ "-5.23456789,-6.23456789]";
tc.verifyEqual(string(m2json(values)), expected);
end

function testInRangeMinus1e5toMinus1e6(tc)
values = -1e-6 * (1 + (1:5) + 0.23456789);
expected = "[-2.235e-06,-3.235e-06,-4.235e-06,-5.235e-06," ...
+ "-6.235e-06]";
expected = "[-2.23456789e-06,-3.23456789e-06," ...
+ "-4.23456789e-06,-5.23456789e-06,-6.23456789e-06]";
tc.verifyEqual(string(m2json(values)), expected);
end

Expand Down
9 changes: 2 additions & 7 deletions plotly/plotly_aux/m2json.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
valstr = cell2json(val);
elseif isa(val, "numeric")
sz = size(val);
numDigits = max(arrayfun(@getPrecision, val));
if isa(val,"single")
numDigits = min(7, numDigits);
numDigits = 7;
else
numDigits = min(15, numDigits);
numDigits = 15;
end
fmt = sprintf("%%.%ig", numDigits);
if sum(sz>1)>1 % 2D or higher array
Expand Down Expand Up @@ -61,7 +60,3 @@
warning("Failed to m2json encode class of type: %s", class(val));
end
end

function numDigits = getPrecision(val)
numDigits = strlength(sprintf("%.15g", val));
end