diff --git a/plotly/plotly_aux/Test_m2json.m b/plotly/plotly_aux/Test_m2json.m index 11f08fd0..7a5254c6 100644 --- a/plotly/plotly_aux/Test_m2json.m +++ b/plotly/plotly_aux/Test_m2json.m @@ -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 @@ -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 diff --git a/plotly/plotly_aux/m2json.m b/plotly/plotly_aux/m2json.m index faa68089..1aaa85de 100644 --- a/plotly/plotly_aux/m2json.m +++ b/plotly/plotly_aux/m2json.m @@ -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 @@ -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