RejectedSoftware Forums

Sign up

REST API and Double representation

Hi,

Is it possible not to loose precision when sending double value ?

In the exemple below I have 44.000083 and the result I get from vibe is 44.0001.
It seems that it use the default to!string to represent double.

It seems to work find when sending values to vibe, I don't loose any precision.

import vibe.d;
import vibe.web.rest;
import std.stdio;

string _routes = "";

shared static this(){

	auto router = new URLRouter;
	router.get("/", &displayRoutes);
	registerRestInterface(router, new MyAPIMock());
	foreach(route; router.getAllRoutes()) {
		_routes = _routes ~ route.to!string();
		_routes = _routes ~ "\r\n";
	}
	auto settings = new HTTPServerSettings;
	settings.port = 8080;
	listenHTTP(settings, router);

}

void displayRoutes(HTTPServerRequest req,
                   HTTPServerResponse res) {
	res.writeBody(_routes);
}

interface MyAPI {
	@property double getDouble();
}

class MyAPIMock : MyAPI {
	public:
		override:
			@property double getDouble()
			{
				double result = 44.000083;
				return result;
			}
}

Re: REST API and Double representation

On Wed, 24 Sep 2014 16:15:18 GMT, Mickaël Bouchaud wrote:

Hi,

Is it possible not to loose precision when sending double value ?

In the exemple below I have 44.000083 and the result I get from vibe is 44.0001.
It seems that it use the default to!string to represent double.

It seems to work find when sending values to vibe, I don't loose any precision.

This is indeed an issue currently. I'll fix the issue using formattedWrite("%.16g", num) for the time being.

There have also been other requests for either caching the FP string representation, or for outputting arbitrary precision, so maybe there should really be a way to customize the output. One possibility would be to let the user define string toJsonFloat() and static T from JsonFloat(string) for custom types (with the requirement that both operate with valid JSON literals).

Re: REST API and Double representation

Commit: c4ae00a

Re: REST API and Double representation

On Thu, 02 Oct 2014 17:28:58 GMT, Sönke Ludwig wrote:

Commit: c4ae00a

I tried to test it and I have issues with dub.
I tried to upgrade dub dependencies with :

dub upgrade --force-remove

but I get this error

Fetching libevent 2.0.1+2.0.16...
Error executing command build: Failed to download http://code.dlang.org/packages/libevent/2.0.1%252B2.0.16.zip: 404 Not >Found

It seems that dub try to download libevent dependency from http://code.dlang.org/packages/libevent/2.0.1%252B2.0.16.zip instead of http://code.dlang.org/packages/libevent/2.0.1+2.0.16.zip

The url seems to have been encoded twice from

http://code.dlang.org/packages/libevent/2.0.1+2.0.16.zip

to

http://code.dlang.org/packages/libevent/2.0.1%2B2.0.16.zip

and to

http://code.dlang.org/packages/libevent/2.0.1%252B2.0.16.zip

I'm wondering how I can fix this or get around this problem. Any idea ?

How I can figure out if the problem is on the dub repository or on my local dub. Is there a discussion, forum or something for dub related problems ?

Thanks

Re: REST API and Double representation

On Thu, 09 Oct 2014 08:14:27 GMT, Mickaël Bouchaud wrote:

On Thu, 02 Oct 2014 17:28:58 GMT, Sönke Ludwig wrote:

Commit: c4ae00a

I tried to test it and I have issues with dub.
I tried to upgrade dub dependencies with :

dub upgrade --force-remove

but I get this error

Fetching libevent 2.0.1+2.0.16...
Error executing command build: Failed to download http://code.dlang.org/packages/libevent/2.0.1%252B2.0.16.zip: 404 Not >Found

It seems that dub try to download libevent dependency from http://code.dlang.org/packages/libevent/2.0.1%252B2.0.16.zip instead of http://code.dlang.org/packages/libevent/2.0.1+2.0.16.zip

The url seems to have been encoded twice from

http://code.dlang.org/packages/libevent/2.0.1+2.0.16.zip
to
http://code.dlang.org/packages/libevent/2.0.1%2B2.0.16.zip
and to
http://code.dlang.org/packages/libevent/2.0.1%252B2.0.16.zip

I'm wondering how I can fix this or get around this problem. Any idea ?

How I can figure out if the problem is on the dub repository or on my local dub. Is there a discussion, forum or something for dub related problems ?

Thanks

Seems that an outdated dub was my problem here.

I updated dub to 0.9.22 and also DMD to 2.066.0.
I'm on vibe.d master branch.

I removed all packages in ~/.dub/packages/, run dub and get this error.

WARNING: A deprecated branch based version specification is used for the dependency vibe-d. Please use numbered versions instead. Also note that you can still use the dub.selections.json file to override a certain dependency to use a branch instead.
Building vibe-d ~master configuration "libevent", build type debug.
Running dmd...
Building testdouble ~master configuration "application", build type debug.
Compiling using dmd...
Linking...
Undefined symbols for architecture x86_64:
  "_D4core6thread5Fiber4callMFbZC6Object", referenced from:
      _D4vibe4core4core14VibeDriverCore14resumeCoreTaskMFC4vibe4core4core8CoreTaskC9ExceptionZv in libvibe-d.a(core_32c2_57b.o)
  "_D4core6thread6Thread7getThisFZC4core6thread6Thread", referenced from:
      _D4vibe4core3log6rawLogFNbNfAyaiE4vibe4core3log8LogLevelAyaZ9__lambda6FNeZC4core6thread6Thread in libvibe-d.a(log_343e_26c.o)
      _D4vibe4core4core18_sharedStaticCtor1FZv in libvibe-d.a(core.o)
      _D4vibe4core4core12_staticCtor3FZv in libvibe-d.a(core.o)
      _D4vibe4core4core12_staticDtor4FZv in libvibe-d.a(core.o)
      _D4vibe4core4core14VibeDriverCore14resumeCoreTaskMFC4vibe4core4core8CoreTaskC9ExceptionZv in libvibe-d.a(core_32c2_57b.o)
      _D4vibe4core4core17handleWorkerTasksFZv in libvibe-d.a(core_32ca_6ec.o)
      _D4vibe4core4core8CoreTask4joinMFZv in libvibe-d.a(core_32c1_31c.o)
      ...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
--- errorlevel 1

I copied the verbose version here

Any idea how to fix this problem ?

Re: REST API and Double representation

Am 09.10.2014 17:39, schrieb Mickaël Bouchaud:

Undefined symbols for architecture x86_64:
   "_D4core6thread5Fiber4callMFbZC6Object", referenced from:
       _D4vibe4core4core14VibeDriverCore14resumeCoreTaskMFC4vibe4core4core8CoreTaskC9ExceptionZv in libvibe-d.a(core_32c2_57b.o)
   "_D4core6thread6Thread7getThisFZC4core6thread6Thread", referenced from:
       _D4vibe4core3log6rawLogFNbNfAyaiE4vibe4core3log8LogLevelAyaZ9__lambda6FNeZC4core6thread6Thread in libvibe-d.a(log_343e_26c.o)
       _D4vibe4core4core18_sharedStaticCtor1FZv in libvibe-d.a(core.o)
       _D4vibe4core4core12_staticCtor3FZv in libvibe-d.a(core.o)
       _D4vibe4core4core12_staticDtor4FZv in libvibe-d.a(core.o)
       _D4vibe4core4core14VibeDriverCore14resumeCoreTaskMFC4vibe4core4core8CoreTaskC9ExceptionZv in libvibe-d.a(core_32c2_57b.o)
       _D4vibe4core4core17handleWorkerTasksFZv in libvibe-d.a(core_32ca_6ec.o)
       _D4vibe4core4core8CoreTask4joinMFZv in libvibe-d.a(core_32c1_31c.o)
       ...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
--- errorlevel 1

That looks strange, those should be standard Phobos symbols.
Unfortunately I don't have a Mac available for testing until next week.
I'll see if I can reproduce the issue then.

As a complete shot in the dark, you could try to add "libs": <br>["phobos2"] to the dub.json of the "testdouble" package. As a sanity
check, does invoking dmd on the following produce any errors?

import core.thread;
void main()
{
	auto t = Thread.getThis();
	auto f = new Fiber({});
	f.call();
}

Re: REST API and Double representation

On Thu, 09 Oct 2014 21:00:47 +0200, Sönke Ludwig wrote:

That looks strange, those should be standard Phobos symbols.
Unfortunately I don't have a Mac available for testing until next week.
I'll see if I can reproduce the issue then.

As a complete shot in the dark, you could try to add "libs": <br>["phobos2"] to the dub.json of the "testdouble" package. As a sanity
check, does invoking dmd on the following produce any errors?

import core.thread;
void main()
{
	auto t = Thread.getThis();
	auto f = new Fiber({});
	f.call();
}

I tried this sanity check running dmd 2.066 and get an error message :

Undefined symbols for architecture x86_64:
  "_D4core6thread5Fiber4callMFbZC6Object", referenced from:
      __Dmain in test.o
  "_D4core6thread6Thread7getThisFZC4core6thread6Thread", referenced from:
      __Dmain in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
--- errorlevel 1

So it seems that it's a DMD problem then.

Re: REST API and Double representation

I have tested vibe 0.7.21-rc.3 on ubuntu and everything works fine.

The double representation is now more precise.

Thanks Sönke !