bugGNU Octave - Bugs: bug #64960, jsonencode produces different...

 
 

bug #64960: jsonencode produces different results for single inputs than Matlab

Submitter:  Rik <rik5>
Submitted:  Sat 02 Dec 2023 12:34:04 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 05 Dec 2023 05:26:52 AM UTC, comment #2: 

I think there may be easier ways to handle this within Octave, rather than have to hope for an extension of the RapidJSON API.

One way would be to convert the single to a string within Octave, and then write out just a string.  The code for saving ASCII files would probably work.

Another way would be to convert from single to double, and then truncate the double to 9 digits of precision.  There may be an option in the C++ library for this, or Octave has some display code that can do this.

Rik <rik5>
Group administrator
Sun 03 Dec 2023 12:38:30 PM UTC, comment #1: 

Fyi. Below test indicates that jsonencode in Matlab has native support for single-floats which RapidJSON does not. We would pretty much need to extend RapidJSON to include a Writer.Float function which limits the number of decimals it outputs for float values.


MATLAB behaviour:

>> jsonencode(single(1.2))

ans =
    '1.2'

>> jsonencode(double(single(1.2)))

ans =
    '1.2000000476837158'


Btw. Thanks for picking up #64949 - I really appreciate it :-)

Anonymous
Sat 02 Dec 2023 12:34:04 AM UTC, original submission:  

In Matlab,


str = jsonencode (single (50.025))
=>
str = '50.025'


while in Octave


str = jsonencode (single (50.025))
=>
str = '50.025001525878909'


Rik <rik5>
Group administrator

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    No changes have been made to this item

    Back to the top

    Powered by Savane 3.13-54b4.
    Corresponding source code