D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13013 - Failed unittests in std.json - does not parse doubles correctly
Summary: Failed unittests in std.json - does not parse doubles correctly
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Windows
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-30 20:21 UTC by Mark Isaacson
Modified: 2014-08-21 18:22 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Mark Isaacson 2014-06-30 20:21:00 UTC
When compiling with -m64 on Windows, std.json fails to pass unittests regarding parsing doubles from string json format into the JSONValue type.

Specifically:
core.exception.AssertError@jsontest.d(1027): 1.44354e-312 should be 0.23

This does not happen when I leave off -m64.
This does not happen when I use -m64 on OSX.

I compile with the line:
dmd -main -unittest -m64 json.d

I downloaded the latest version of std.json to see if this had already been patched; it is still broken. The line number above refers to the version currently on Github.


I will likely spend some time investigating this myself, as I must eventually have a solution to this problem.
Comment 1 David Nadlinger 2014-06-30 20:31:46 UTC
When porting LDC to Windows, I had to deal with a large number of such issues that manifested in a similar way (absurd floating point results). The root cause always was that the MSVC runtime does not support reals ("long double" in C land, i.e. x87 80 bit floats) at all. This leads to situations where D code is calling a function (say snprintf) with some floating point arguments on the x87 stack, but the called function reads them from the XMM registers or the regular stack.

If I remember correctly, DMD also uses 80 bit reals on Win64. This can't work unless it comes with its own string formatting/math/etc. routines. For LDC/MinGW, we just use the MinGW ones which do properly support long doubles, but I'm not sure what the intended solution for DMD is.
Comment 2 Mark Isaacson 2014-06-30 20:38:53 UTC
Thanks for the insight David! I added a:
version (Win64) {
  alias FloatingType = double;
} else {
  alias FloatingType = real;
}

to the top and changed the appropriate union type to use that alias. The unittests ran perfectly.

I doubt that this is the fix we want to ship, but it seems that your analysis is spot-on.
Comment 3 Dmitry Olshansky 2014-06-30 20:45:55 UTC
JavaScript uses double exclusively why use 'real' in JSON to begin with?
Comment 4 Mark Isaacson 2014-06-30 20:53:47 UTC
Dimitry - Just had the same conversation with a co-worker :). Checked the blame log; was unable to quickly find the person who initially coded it up as real to find the rationale.
Comment 5 Dmitry Olshansky 2014-06-30 21:11:21 UTC
(In reply to Mark Isaacson from comment #4)
> Dimitry - Just had the same conversation with a co-worker :). Checked the
> blame log; was unable to quickly find the person who initially coded it up
> as real to find the rationale.

That stuff is old...
I'd pull a patch that fixes it to double without a second thought.

It still leaves the question of ABI violation as something separate. From the bug report I guess that not every test is run on Win64 ATM.
Comment 6 Mark Isaacson 2014-07-02 01:48:54 UTC
Pull request: https://github.com/D-Programming-Language/phobos/pull/2291
Comment 7 github-bugzilla 2014-07-03 08:18:11 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/333e7e734f30067acbb935adfdf6d2ef64f50b8c
Issue 13013 - Converted real to double in std.json.JSONValue

https://github.com/D-Programming-Language/phobos/commit/f518343f3e85c7b0ae0a9d2486dd18390dd592d6
Merge pull request #2291 from markisaa/realToDouble

Issue 13013 - Converted real to double in std.json.JSONValue
Comment 8 github-bugzilla 2014-07-08 01:15:10 UTC
Commit pushed to 2.066 at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/4b98a776cbb4e42cd271c57d238621174d5fb0ff
Merge pull request #2291 from markisaa/realToDouble

Issue 13013 - Converted real to double in std.json.JSONValue
Comment 9 github-bugzilla 2014-08-21 18:22:02 UTC
Commit pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/4b98a776cbb4e42cd271c57d238621174d5fb0ff
Merge pull request #2291 from markisaa/realToDouble