Skip to content

Commit 0bfe4ef

Browse files
Fix precision when converting to json and add more test cases
1 parent 8591421 commit 0bfe4ef

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

plotly/plotly_aux/Test_m2json.m

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
classdef Test_m2json < matlab.unittest.TestCase
22
methods (Test)
3+
function testLowPrecisionInRange0to10(tc)
4+
values = 1 + (1:5) + 0.234;
5+
expected = "[2.234,3.234,4.234,5.234,6.234]";
6+
tc.verifyEqual(string(m2json(values)), expected);
7+
end
8+
39
function testInRange0to10(tc)
410
values = 1 + (1:5) + 0.23456789;
5-
expected = "[2.235,3.235,4.235,5.235,6.235]";
11+
expected = "[2.23456789,3.23456789,4.23456789,5.23456789," ...
12+
+ "6.23456789]";
613
tc.verifyEqual(string(m2json(values)), expected);
714
end
815

916
function test2dArrayInRange0to10(tc)
10-
values = 1 + (1:5) + (0:1)' + 0.23456789;
11-
expected = "[[2.235,3.235,4.235,5.235,6.235]," ...
12-
+ "[3.235,4.235,5.235,6.235,7.235]]";
17+
values = 1 + (1:5) + (0:1)' + 0.234;
18+
expected = "[[2.234,3.234,4.234,5.234,6.234]," ...
19+
+ "[3.234,4.234,5.234,6.234,7.234]]";
20+
tc.verifyEqual(string(m2json(values)), expected);
21+
end
22+
23+
function testLowPrecisionInRange1e6to1e5(tc)
24+
values = 1e-6 * (1 + (1:5) + 0.234);
25+
expected = "[2.234e-06,3.234e-06,4.234e-06,5.234e-06," ...
26+
+ "6.234e-06]";
1327
tc.verifyEqual(string(m2json(values)), expected);
1428
end
1529

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

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

3751
function testLogScaledVariables(tc)
3852
values = 1e14 + 10.^(1:5) + 0.23456789;
39-
expected = "[1e+14,1.000000000001e+14,1.00000000001e+14," ...
40-
+ "1.0000000001e+14,1.000000001e+14]";
53+
expected = "[100000000000010,100000000000100," ...
54+
+ "100000000001000,100000000010000,100000000100000]";
55+
tc.verifyEqual(string(m2json(values)), expected);
56+
end
57+
58+
function testLowPrecisionInRangeMinus10to0(tc)
59+
values = -(1 + (1:5) + 0.234);
60+
expected = "[-2.234,-3.234,-4.234,-5.234,-6.234]";
4161
tc.verifyEqual(string(m2json(values)), expected);
4262
end
4363

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

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

plotly/plotly_aux/m2json.m

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
valstr = cell2json(val);
66
elseif isa(val, "numeric")
77
sz = size(val);
8-
numDigits = max(arrayfun(@getPrecision, val));
98
if isa(val,"single")
10-
numDigits = min(7, numDigits);
9+
numDigits = 7;
1110
else
12-
numDigits = min(15, numDigits);
11+
numDigits = 15;
1312
end
1413
fmt = sprintf("%%.%ig", numDigits);
1514
if sum(sz>1)>1 % 2D or higher array
@@ -61,7 +60,3 @@
6160
warning("Failed to m2json encode class of type: %s", class(val));
6261
end
6362
end
64-
65-
function numDigits = getPrecision(val)
66-
numDigits = strlength(sprintf("%.15g", val));
67-
end

0 commit comments

Comments
 (0)