On Thursday, 21 August 2014 at 18:53:08 UTC, nrgyzer wrote:
Hi everyone,

I'm facing a problem with the JSON functions. I've to communicate
with another PC using JSON. Here's a simple snipped which shows
my problem:

import std.json;
import std.stdio;
void main()
{
        double d = 1.23456789;
        JSONValue j = d;
        sendToRemote(toJSON(&j));
}

My problem is that the remote PC receives 1.23457 instead of
1.23456789. But when I add the following:

import std.json;
import std.stdio;
void main()
{
        double d = 1.23456789;
        JSONValue j = d;
        sendToRemote(toJSON(&j));
        writefln("%.8f", j.floating);
}

writefln() shows me 1.23456789. So, the value is correct. I think
the problem is the default representation of any floating point
number to string. Is there any chance I can send 1.23456789
instead of 1.23457 to my remote PC?

Note: I cannot use strings because the other PC expects a numeric
data type.

Hi, my answer will be a bit OT, and is not really an answer but:

FP precision matters "only" during processing. I don't know which is the context but for example if it represents a parameter you could try
- to encode it to a fraction representation like "1/3".
- to encode it to a fixed-point float value.
- mapping the value in the range of -1.0 to 1.0 because by nature, FP values are more accurate in this range. - to use a more accurate type: send a double (more precision), reduce it to a float (your 6 decimals are then granted).

Note 1: still about the context, remember that this is a damn small delta. Does this delta is that much a concern ?

Note 2: singles (we speak about IEEE 754, 32bits right ?) are not always 6 decimals precision, it's 5 >>OR<< 6.

Reply via email to