adamdruppe / arsd

This is a collection of modules that I've released over the years. Most of them stand alone, or have just one or two dependencies in here, so you don't have to download this whole repo.
http://arsd-official.dpldocs.info/arsd.html
531 stars 128 forks source link

sqlite: Improve precision for floating-point types #400

Closed konradh closed 11 months ago

konradh commented 11 months ago

Sqlite is able to store 64 bit floating-point values, but the arsd.sqlite diver does not support this.

This adds support for values of type double when binding values to a query. Prior to this, doubles would be converted to a string and only be stored with 6 digits of precision in the database.

Additionally, floats stored in the database were converted to strings when retrieved and therefore be reduced to 6 digits of precision, too. This behavior is now fixed for floats and doubles.


This unittest checks that doubles now work as expected.

unittest
{
    import arsd.database;
    import arsd.sqlite;
    import std.math : feqrel;
    import std.conv : to;

    double d = 1.0/3.0;
    auto db = new Sqlite(":memory:");
    db.query("CREATE TABLE test(d REAL);");
    db.query("INSERT INTO test VALUES (?)", d);
    auto result = db.query("SELECT d FROM test;").front[0];
    assert(feqrel(d, to!double(result)) >= 52);
}
adamdruppe commented 11 months ago

sweet, thanks, yeah i've actually been slowly starting the process of moving from just pure strings with the boring to!string conversion over to sending the binary data for certain datatypes (That's the idea behind DatabaseDatum instead of plain string) but ive had so many other things to do it has been taking forever to get around to it