Mercurial > octave
diff libinterp/corefcn/jsonencode.cc @ 32524:d2eabc48cf80
maint: code cleanup in jsonencode.cc
* jsonencode.cc (encode_numeric): Avoid unnecessary call to scalar_value()
if octave_value is really a boolean. Use fabs() to avoid two comparisons
two positive and negative value of limit.
* jsonencode_BIST.tst: Fix commenting to use Matlab-compatible style.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 02 Dec 2023 18:03:12 -0800 |
parents | dc06bb3bf1ed |
children | 2f3b54f81947 |
line wrap: on
line diff
--- a/libinterp/corefcn/jsonencode.cc Fri Dec 01 16:37:40 2023 -0800 +++ b/libinterp/corefcn/jsonencode.cc Sat Dec 02 18:03:12 2023 -0800 @@ -61,22 +61,26 @@ encode_numeric (T& writer, const octave_value& obj, const bool& ConvertInfAndNaN) { - double value = obj.scalar_value (); + if (obj.isfloat ()) + { + double value = obj.scalar_value (); - if (obj.is_bool_scalar ()) + // Any numeric input from the interpreter will be in double type so in + // order to detect ints, we will check if the floor of the input and the + // input are equal using fabs (A - B) < epsilon method as it is more + // accurate. If value > 999999, MATLAB will encode it in scientific + // notation (double). + if (fabs (floor (value) - value) < std::numeric_limits<double>::epsilon () + && fabs (value) <= 999999) + writer.Int64 (value); + // Possibly write NULL for non-finite values (-Inf, Inf, NaN, NA) + else if (ConvertInfAndNaN && ! octave::math::isfinite (value)) + writer.Null (); + else + writer.Double (value); + } + else if (obj.is_bool_scalar ()) writer.Bool (obj.bool_value ()); - // Any numeric input from the interpreter will be in double type so in order - // to detect ints, we will check if the floor of the input and the input are - // equal using fabs (A - B) < epsilon method as it is more accurate. - // If value > 999999, MATLAB will encode it in scientific notation (double) - else if (fabs (floor (value) - value) < std::numeric_limits<double>::epsilon () - && value <= 999999 && value >= -999999) - writer.Int64 (value); - // Possibly write NULL for non-finite values (-Inf, Inf, NaN, NA) - else if (ConvertInfAndNaN && ! octave::math::isfinite (value)) - writer.Null (); - else if (obj.isfloat ()) - writer.Double (value); else error ("jsonencode: unsupported type"); }