Jump to page: 1 213  
Page
Thread overview
Printing shortest decimal form of floating point number with Mir
Dec 14
9il
Dec 20
9il
Dec 21
9il
Dec 22
9il
Dec 22
9il
Dec 22
9il
Dec 23
9il
Dec 23
9il
Dec 23
9il
Dec 23
9il
Dec 23
9il
Dec 24
welkam
Dec 24
9il
Jan 03
9il
Jan 03
welkam
Jan 04
9il
Jan 04
ag0aep6g
Jan 04
ag0aep6g
Jan 04
ag0aep6g
Jan 04
jmh530
Jan 04
ag0aep6g
Jan 04
jmh530
Jan 04
ag0aep6g
Jan 05
ag0aep6g
Jan 05
ag0aep6g
Jan 04
jmh530
Jan 04
9il
Jan 05
9il
Jan 05
9il
Jan 06
9il
Jan 04
welkam
Jan 05
welkam
Jan 05
welkam
Jan 04
jmh530
Jan 04
welkam
Jan 04
jmh530
Dec 23
9il
Dec 23
aberba
Dec 23
9il
Dec 23
aberba
Dec 23
jmh530
Jan 05
welkam
Jan 05
welkam
Dec 24
jmh530
Dec 24
9il
December 14
Hi all,

Generic version of Ryu algorithm [1] was ported to D, well optimized, and adopted to mir packages.

It allows printing the shortest (scientific) decimal form of a floating-point number that if it is converted back would produce the same floating-point number.

The update requires mir-algorithm [2] >=3.10.13

@safe pure nothrow unittest
{
    import mir.conv: to;
    assert(12.3.to!string == "1.23e1");
    assert(12.3456789.to!string == "1.23456789e1");

    // CTFE-able
    static assert(12.3456789.to!string == "1.23456789e1");
}

@safe pure @nogc unittest
{
    // @nogc
    import mir.conv: to;
    import mir.small_string;
    assert(12.3.to!(SmallString!32) == "1.23e1");
    assert(12.3456789.to!(SmallString!32) == "1.23456789e1");
}

@safe pure @nogc nothrow unittest
{
    // @nogc
    import mir.format;
    stringBuf buffer;
    auto data = buffer << 12.3 << ", " << 12.3456789 << getData;
    assert(data == "1.23e1, 1.23456789e1");
}

Floating-point numbers can be converted to stack-allocated decimal numbers.

    @safe pure nothrow @nogc
    unittest
    {
        // float and double can be used to construct Decimal of any length
        auto decimal64 = Decimal!1(-1.235e-7);
        assert(decimal64.exponent == -10);
        assert(decimal64.coefficient == -1235);

        // real number may need Decimal at least length of 2
        auto decimal128 = Decimal!2(-1.235e-7L);
        assert(decimal128.exponent == -10);
        assert(decimal128.coefficient == -1235);

        decimal128 = Decimal!2(1234e3f);
        assert(decimal128.exponent == 3);
        assert(decimal128.coefficient == 1234);
    }


Recent releases of ASDF [3] and Mir Ion [4] use this formatting by default. It allows performing JSON serialization without loss of precision.

Note that D's compiler floating-point literals parsing and Phobos floating-point literals parsing are not precise [5,6,7,8]. It is recommended to use Mir's to!double/float/real to convert floating-point numbers from a string.

The work has been sponsored by Symmetry Investments and Kaleidic Associates.

Kind regards,
Ilya

[1] https://github.com/ulfjack/ryu
[2] http://mir-algorithm.libmir.org/
[3] http://asdf.libmir.org/
[4] http://mir-ion.libmir.org/
[5] https://issues.dlang.org/show_bug.cgi?id=20951
[6] https://issues.dlang.org/show_bug.cgi?id=20952
[7] https://issues.dlang.org/show_bug.cgi?id=20953
[8] https://issues.dlang.org/show_bug.cgi?id=20967


December 20
On Monday, 14 December 2020 at 06:47:32 UTC, 9il wrote:
> Hi all,
>
> Generic version of Ryu algorithm [1] was ported to D, well optimized, and adopted to mir packages.
>
> [...]

Default formatting has been reworked to be more human-friendly:
1.23e1 -> 1.23

https://github.com/ulfjack/ryu/tree/master/ryu

Microsoft, Clang++, and others are adopting Ryu as well.
December 20
On 12/13/2020 10:47 PM, 9il wrote:
> Note that D's compiler floating-point literals parsing and Phobos floating-point literals parsing are not precise [5,6,7,8]. It is recommended to use Mir's to!double/float/real to convert floating-point numbers from a string.

Can the improved parsing be added to D? (It would need to be Boost licensed.)
December 21
On Sunday, 20 December 2020 at 22:21:56 UTC, Walter Bright wrote:
> On 12/13/2020 10:47 PM, 9il wrote:
>> Note that D's compiler floating-point literals parsing and Phobos floating-point literals parsing are not precise [5,6,7,8]. It is recommended to use Mir's to!double/float/real to convert floating-point numbers from a string.
>
> Can the improved parsing be added to D? (It would need to be Boost licensed.)

If I am correct there is open PR that set DMD to use C’s primitives for literals parsing. So, for compiler itself we don’t need Mir.

If you mean Phobos - one can use Mir instead.
December 21
On 12/14/20 1:47 AM, 9il wrote:
> Hi all,
> 
> Generic version of Ryu algorithm [1] was ported to D, well optimized, and adopted to mir packages.
> ...
> [1] https://github.com/ulfjack/ryu
> [2] http://mir-algorithm.libmir.org/
> [3] http://asdf.libmir.org/
> [4] http://mir-ion.libmir.org/
> [5] https://issues.dlang.org/show_bug.cgi?id=20951
> [6] https://issues.dlang.org/show_bug.cgi?id=20952
> [7] https://issues.dlang.org/show_bug.cgi?id=20953
> [8] https://issues.dlang.org/show_bug.cgi?id=20967
> 
Great work!

This will be very helpful in our scientific computing.

I was going to suggest a PR at repo [1] to add your implementation to their list, but I see you have already done this =)
December 21
On 12/20/2020 9:42 PM, 9il wrote:
> On Sunday, 20 December 2020 at 22:21:56 UTC, Walter Bright wrote:
>> Can the improved parsing be added to D? (It would need to be Boost licensed.)
> If I am correct there is open PR that set DMD to use C’s primitives for literals parsing. So, for compiler itself we don’t need Mir.

That's not correct for the targets that use Digital Mars C.


> If you mean Phobos - one can use Mir instead.

These functions in Phobos would make a great advertisement for Mir.

December 22
On Tuesday, 22 December 2020 at 02:02:24 UTC, Walter Bright wrote:
> On 12/20/2020 9:42 PM, 9il wrote:
>> On Sunday, 20 December 2020 at 22:21:56 UTC, Walter Bright wrote:
>>> Can the improved parsing be added to D? (It would need to be Boost licensed.)
>> If I am correct there is open PR that set DMD to use C’s primitives for literals parsing. So, for compiler itself we don’t need Mir.
>
> That's not correct for the targets that use Digital Mars C.

I thought that DMD is compiled with LDC for release builds, isn't it?

>> If you mean Phobos - one can use Mir instead.
>
> These functions in Phobos would make a great advertisement for Mir.

How this possible? Having them in Mir is already a great advertisement for Mir and not having them in Phobos is an even more great advertisement for Mir.

December 22
On Tuesday, 22 December 2020 at 04:33:55 UTC, 9il wrote:
> On Tuesday, 22 December 2020 at 02:02:24 UTC, Walter Bright wrote:
>> On 12/20/2020 9:42 PM, 9il wrote:
>>> On Sunday, 20 December 2020 at 22:21:56 UTC, Walter Bright wrote:
>>>> Can the improved parsing be added to D? (It would need to be Boost licensed.)
>>> If I am correct there is open PR that set DMD to use C’s primitives for literals parsing. So, for compiler itself we don’t need Mir.
>>
>> That's not correct for the targets that use Digital Mars C.
>
> I thought that DMD is compiled with LDC for release builds, isn't it?
>
>>> If you mean Phobos - one can use Mir instead.
>>
>> These functions in Phobos would make a great advertisement for Mir.
>
> How this possible? Having them in Mir is already a great advertisement for Mir and not having them in Phobos is an even more great advertisement for Mir.

... I just have thought maybe I have missed something and DLF helps Mir with advertising at least a bit, maybe at least with two-three tweets per year? The last time @D_Programming tweeted something about Mir was in 2016.

December 22
On 12/21/2020 8:33 PM, 9il wrote:
>> These functions in Phobos would make a great advertisement for Mir.
> How this possible?

A lot more people will have Phobos than Phobos+Mir. If they are perusing the source code and see Mir contributed excellent floating point formatting code, they may have never heard of Mir but have now. Then they'll be likely to be positively disposed towards using Mir because of the high quality code.

It's the same idea as HBO offering the first episode for free in a miniseries. People watch the first episode, like it, and then subscribe to HBO.


> Having them in Mir is already a great advertisement for Mir

Since they exist in the C standard library (except for DMC :-( ) they by themselves aren't a compelling reason for someone to use Mir.
December 22
On 12/21/2020 8:51 PM, 9il wrote:
> ... I just have thought maybe I have missed something and DLF helps Mir with advertising at least a bit, maybe at least with two-three tweets per year? The last time @D_Programming tweeted something about Mir was in 2016.

I thought anything in D.announce got automatically tweeted. Anyhow, if you have a message that you'd like @D_Programming to tweet, please send it to Mike Parker.
« First   ‹ Prev
1 2 3 4 5 6 7 8 9 10 11