Skip to content
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

output of json encode/decode of structs containing f64 arrays is not equivalent to the input #22860

Open
holder66 opened this issue Nov 14, 2024 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. Status: Confirmed This bug has been confirmed to be valid by a contributor.

Comments

@holder66
Copy link

holder66 commented Nov 14, 2024

Describe the bug

Applying json.encode and then json.decode to a struct containing an f64 array generates an output which is no longer equivalent to the input.
It appears to be related to how and where rounding is done.

Reproduction Steps

module main

import json

struct Test {
mut:
	arr []f64
}

fn main() {
	mut a := Test{}
	a.arr = [0.9716157205240175, 0.9336099585062241]
	println(a)
	mut enc := json.encode(a)
	println(enc)
	println(json.decode(Test, enc)!)
	println(a == json.decode(Test, enc)!)
}

Expected Behavior

the input should be equivalent to the output

Current Behavior

Test{
    arr: [0.9716157205240175, 0.9336099585062241]
}
{"arr":[0.97161572052401746,0.933609958506224]}
Test{
    arr: [0.9716157205240175, 0.933609958506224]
}
false

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 df51e84, timestamp: 2024-11-14 13:58:50 +0200

Environment details (OS name and version, etc.)

V full version: V 0.4.8 dad8e3c.df51e84
OS: macos, macOS, 15.0.1, 24A348
Processor: 16 cpus, 64bit, little endian, Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz

getwd: /Users/henryolders
vexe: /Users/henryolders/v/v
vexe mtime: 2024-11-14 15:14:24

vroot: OK, value: /Users/henryolders/v
VMODULES: OK, value: /Users/henryolders/.vmodules
VTMP: OK, value: /tmp/v_507

env VFLAGS: "-use-os-system-to-run"

Git version: git version 2.47.0
Git vroot status: weekly.2024.06-1635-gdf51e840
.git/config present: true

CC version: Apple clang version 16.0.0 (clang-1600.0.26.4)
emcc version: N/A
thirdparty/tcc status: thirdparty-macos-amd64 975f1ad8

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@holder66 holder66 added the Bug This tag is applied to issues which reports bugs. label Nov 14, 2024
@JalonSolov JalonSolov added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Nov 14, 2024
@felipensp felipensp added the Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. label Nov 14, 2024
@kbkpbot
Copy link
Contributor

kbkpbot commented Nov 18, 2024

This is a bug in V it self, not in json

A simple test show that V has some bug in double value print?

module main

fn main() {
	val := f64(0.97161572052401746)
	println('20 => ${val:0.20g}')
	println('19 => ${val:0.19g}')
	println('18 => ${val:0.18g}')
	println('17 => ${val:0.17g}')
	println('16 => ${val:0.16g}')
	println('15 => ${val:0.15g}')
	println('14 => ${val:0.14g}')
	println('13 => ${val:0.13g}')
}

will output :

20 => 0.9716157205240175
19 => 0.9716157205240175
18 => 0.9716157205240175
17 => 0.9716157205240175
16 => 0.9716157205240175
15 => 0.971615720524018
14 => 0.97161572052402
13 => 0.971615720524

But a C version will output different result:

#include <stdio.h>
#include <stdlib.h>

void main() {
	double val = 0.97161572052401746;
	printf("20 => %0.20g\n",val);
	printf("19 => %0.19g\n",val);
	printf("18 => %0.18g\n",val);
	printf("17 => %0.17g\n",val);
	printf("16 => %0.16g\n",val);
	printf("15 => %0.15g\n",val);
	printf("14 => %0.14g\n",val);
	printf("13 => %0.13g\n",val);
}
20 => 0.97161572052401745658
19 => 0.9716157205240174566
18 => 0.971615720524017457
17 => 0.97161572052401746
16 => 0.9716157205240175
15 => 0.971615720524017
14 => 0.97161572052402
13 => 0.971615720524

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. Status: Confirmed This bug has been confirmed to be valid by a contributor.
Projects
None yet
Development

No branches or pull requests

4 participants